Home > front end >  how to combine imported image with other R plots?
how to combine imported image with other R plots?

Time:09-27

I have imported the image below and I would like to combine with some heatmap and ggplot2 plots. But got errors while combing the image with other plots May I get your help please

my_image <- magick::image_read("Capture.PNG")
test  <- matrix(rnorm(200), 20, 10)
mfs  <- pheatmap::pheatmap(test)

library(ggplot2)
library(dplyr)
 plot_LM <-  test %>% as.data.frame() %>%
    ggplot(aes(x=V1, y=V2))  
    geom_point(color="blue")   
    geom_smooth(method=lm, se=FALSE, fullrange=TRUE,
                linetype="dashed",
                color="darkred", fill="blue")

gridExtra::grid.arrange(grobs=list(mfs$gtable, plot_LM, my_image ), 
                         ncol= 3, labels=LETTERS[1:3])

Best, Amare

CodePudding user response:

You can create multiple ggplot2 objects and then combine them using patchwork:

library(dplyr)
library(ggplot2) 
library(magick)
#> Linking to ImageMagick 6.9.10.23
#> Enabled features: fontconfig, freetype, fftw, lcms, pango, webp, x11
#> Disabled features: cairo, ghostscript, heic, raw, rsvg
#> Using 12 threads
library(patchwork)

plt1 <- image_read("https://bellard.org/bpg/2.png") %>%
  image_ggplot()
plt2 <- qplot(Sepal.Length, Sepal.Width, data = iris)

plt1 | plt2

  •  Tags:  
  • r
  • Related