问题:
当我运行这个程序时,出现了以下错误:
我收到以下警告信息,但是图形没有形成:
我想使用PNG或JPEG图像制作多面板图。 这些图像不是在R中创建的,但我想要在R中将它们拼合成一个图形。 所有图像的大小/尺寸都相同。
我尝试过的方法:library(png)
img1 <- readPNG("filepath/img1.png")
img2 <- readPNG("filepath/img2.png")
library(patchwork)
patch <- img1 + img2
patch
当我运行这个程序时,出现了以下错误:
[ reached getOption("max.print") -- omitted 3 matrix slice(s) ]
我多次增加了最大打印次数(增加到非常高的数字):
options(maxprint = 1000000000000)
但是仍然得到相同的错误。
接着我尝试使用以下代码将每个图像转换为一个 ggplot(不带点):
library(ggplot2)
img1plot <- ggplot() +
background_image(img1) +
theme(plot.margin = margin(t=1, l=1, r=1, b=1, unit = "cm"))
这会返回以下错误:
Error in background_image(d311) :
could not find function "background_image"
有没有其他方法在R中拼接图片以制作一张图?
编辑:
根据@davidnortes的评论,我尝试了以下操作:
p1 <- ggplot2::annotation_custom(grid::rasterGrob(img1,
width=ggplot2::unit(1,"npc"),
height=ggplot2::unit(1,"npc")),
-Inf, Inf, -Inf, Inf)
p2 <- ggplot2::annotation_custom(grid::rasterGrob(img2,
width=ggplot2::unit(1,"npc"),
height=ggplot2::unit(1,"npc")),
-Inf, Inf, -Inf, Inf)
library(cowplot)
plots <- plot_grid(
p1, p2,
labels = c('A', 'B'),
align="hv"
)
plots
我收到以下警告信息,但是图形没有形成:
Warning messages:
1: In as_grob.default(plot) :
Cannot convert object of class LayerInstanceLayerggprotogg into a grob.
2: In as_grob.default(plot) :
Cannot convert object of class LayerInstanceLayerggprotogg into a grob.
library(patchwork)
,你可以这样做:patch <- wrap_elements(img1) + wrap_elements(img2)
。虽然不能直接查看它,但ggsave
可以正常工作。此外,请确保在readPNG
中设置native = TRUE
。 - mths