Problem:
I want to make a multi-panel figure using PNG or JPEG images. The images were not created in R but I want to patch them together in R to form one figure. All the images are the same size/dimensions.
What I've tried:
library(png)
img1 <- readPNG("filepath/img1.png")
img2 <- readPNG("filepath/img2.png")
library(patchwork)
patch <- img1 + img2
patch
When I run this, I get the following error:
[ reached getOption("max.print") -- omitted 3 matrix slice(s) ]
I increased the max print multiple times (to ridiculously high numbers):
options(maxprint = 1000000000000)
But still get the same error.
I then tried making each image into a ggplot (without the points) using:
library(ggplot2)
img1plot <- ggplot() +
background_image(img1) +
theme(plot.margin = margin(t=1, l=1, r=1, b=1, unit = "cm"))
Which returns the following error:
Error in background_image(d311) :
could not find function "background_image"
Is there another way to patch images together in R to make a figure?
Edit:
Based on the comment from @davidnortes, I tried the following:
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
I get the following warning messages and the plot doesn't form:
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)
you can dopatch <- wrap_elements(img1) + wrap_elements(img2)
. You can't directly view it, butggsave
works fine. Also, make sure to setnative = TRUE
in thereadPNG
. – Plumb