我如何创建图表和图像以显示在 R 的同一面板上
Posted
技术标签:
【中文标题】我如何创建图表和图像以显示在 R 的同一面板上【英文标题】:How do i create graphs and images to show on the same panel in 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 手动插入图像。
谢谢,
【问题讨论】:
【参考方案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)
【讨论】:
以上是关于我如何创建图表和图像以显示在 R 的同一面板上的主要内容,如果未能解决你的问题,请参考以下文章