library(grid)
library(gridExtra)
library(png)
library(ggplot2)
library(RCurl)
PNG_1 <- readPNG(getURLContent("https://i.ibb.co/MVx1QsQ/A.png"))
PNG_2 <- readPNG(getURLContent("https://i.ibb.co/kHVGNfQ/B.png"))
PNG_3 <- readPNG(getURLContent("https://i.ibb.co/yVf3Hjg/C.png"))
grid <- grid.arrange(rasterGrob(PNG_1), rasterGrob(PNG_2), rasterGrob(PNG_3), ncol=3)
ggsave(grid,filename="output.png")
Tried to set the output dimensions manually, but to no avail.
Simply wish to remove the large top and bottom margins. Thanks.
As plots adapts to device size it's not simple to get part size. I think the easiest way is to compute aspect ratio from dimension of PNGs and provide device size to ggsave.
asp <- (ncol(PNG_1)+ncol(PNG_2)+ncol(PNG_3))/max(nrow(PNG_1),nrow(PNG_2),nrow(PNG_3))
ggsave(grid, filename= ..., width=5, height=5/asp)
to stitch those PNG you can use this code that will keep original pixel count.
We can't use cbind/rbind
function as readPNG
returns arrays and not matrices. Fortunately the abind
package provide a function to do it, abind
(
along=2
means cbind
, along=1
for rbind
)
library(abind)
PNG <- abind(PNG_1,PNG_2,PNG_3,along=2)
writePNG(PNG,"output.png")