【问题标题】:How do i create graphs and images to show on the same panel in R我如何创建图表和图像以显示在 R 的同一面板上
【发布时间】:2018-05-31 01:53:50
【问题描述】:

我是 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)

诅咒我使用 power-point 手动插入图像。

谢谢,

【问题讨论】:

    标签: r ggplot2 ggpubr


    【解决方案1】:

    我认为您的代码几乎就在那里。如果您使用theme 函数添加一些边距,您可以得到类似这样的结果:

    代码如下。两个图像的唯一添加是theme(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)
    

    【讨论】:

      猜你喜欢
      • 2011-05-24
      • 1970-01-01
      • 2010-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-20
      • 1970-01-01
      • 2012-08-28
      相关资源
      最近更新 更多