我如何创建图形和图像以显示在 R 中的同一面板上
How do i create graphs and images to show on the same panel in R
我是 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)
of curse 我使用 power-point 手动插入图像。
谢谢,
我想你的代码就差不多了。如果您使用 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 的新手,正在尝试使用 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)
of curse 我使用 power-point 手动插入图像。
谢谢,
我想你的代码就差不多了。如果您使用 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)