如何创建图形和图像以在R中的同一面板上显示 [英] How do i create graphs and images to show on the same panel in R

查看:53
本文介绍了如何创建图形和图像以在R中的同一面板上显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm new to R and trying to show graphs and images on the same page with R. I tried to use library(ggpubr) and ggarrange() function.

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

to import the images I used library(png) and readPNG() to import my images.

我想要的最终结果是这样的:

The end result i'm aiming for is something like this:

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

The code i used to create the panel is:

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)

出于诅咒,我使用Powerpoint手动插入了图像.

of curse i inserted the images manually using power-point.

谢谢

推荐答案

我认为您的代码已经差不多了.如果使用 theme 功能添加一些边距,则可以得到如下内容:

I think your code is almost there. If you use the theme function to add some margins you can get something like this:

下面的代码.唯一的添加是两个图像的 theme(plot.margin = margin(t = 1,l = 1,r = 1,b = 1,unit ="cm")).

Code below. The only addition is theme(plot.margin = margin(t=1, l=1, r=1, b=1, unit = "cm")) for both images.

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)

这篇关于如何创建图形和图像以在R中的同一面板上显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆