Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何创建图形和图像以显示在R中的同一面板上_R_Ggplot2_Ggpubr - Fatal编程技术网

如何创建图形和图像以显示在R中的同一面板上

如何创建图形和图像以显示在R中的同一面板上,r,ggplot2,ggpubr,R,Ggplot2,Ggpubr,我是R新手,尝试在同一页上用R显示图形和图像。 我尝试使用library(ggpubr)和ggarrange()函数 要导入图像,我使用library(png)和readPNG()导入图像 我的最终目标是这样的: 我用来创建面板的代码是: library(ggpubr) library(png) data("ToothGrowth") bxp <- ggboxplot(ToothGrowth, x = "dose", y = "len", color

我是R新手,尝试在同一页上用R显示图形和图像。 我尝试使用
library(ggpubr)
ggarrange()
函数

要导入图像,我使用
library(png)
readPNG()
导入图像

我的最终目标是这样的:

我用来创建面板的代码是:

library(ggpubr)
library(png)

data("ToothGrowth")

bxp <- ggboxplot(ToothGrowth, x = "dose", y = "len",
                 color = "dose", palette = "jco")
dp <- ggdotplot(ToothGrowth, x = "dose", y = "len",
                color = "dose", palette = "jco", binwidth = 1)

img1 <- readPNG("image1.png")
img2 <- readPNG("image2.png")

im_A <- ggplot() + background_image(img1) # tried to insert the image as background.. there must be a better way
im_B <- ggplot() + background_image(img2) 

ggarrange(im_A, im_B, dp, bxp, 
          labels = c("A", "B", "C", "D"),
          ncol = 2, nrow = 2)
库(ggpubr)
图书馆(png)
数据(“增长”)

bxp我想你的代码就快到了。如果使用
主题
功能添加一些边距,您可以得到如下结果:

代码如下。唯一增加的是两幅图像的主题(plot.margin=margin(t=1,l=1,r=1,b=1,unit=“cm”)

library(ggpubr)
library(png)

data("ToothGrowth")

bxp <- ggboxplot(ToothGrowth, x = "dose", y = "len",
                 color = "dose", palette = "jco")
dp <- ggdotplot(ToothGrowth, x = "dose", y = "len",
                color = "dose", palette = "jco", binwidth = 1)

img1 <- readPNG("~/Personal/Wallpapers/375501.png")
img2 <- readPNG("~/Personal/Wallpapers/665150.png")

im_A <- ggplot() + 
    background_image(img1) +
    # This ensures that the image leaves some space at the edges
    theme(plot.margin = margin(t=1, l=1, r=1, b=1, unit = "cm"))

im_B <- ggplot() + background_image(img2) + 
    theme(plot.margin = margin(t=1, l=1, r=1, b=1, unit = "cm"))

ggarrange(im_A, im_B, dp, bxp, 
          labels = c("A", "B", "C", "D"),
          ncol = 2, nrow = 2)
库(ggpubr)
图书馆(png)
数据(“增长”)
bxp