问题描述
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.
推荐答案
由于绘图会适应设备尺寸,因此获取零件尺寸并不容易.我认为最简单的方法是根据 PNG 的尺寸计算纵横比,并将设备大小提供给 ggsave.
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)
要拼接那些 PNG,您可以使用此代码来保持原始像素数.我们不能使用 cbind/rbind
函数,因为 readPNG
返回数组而不是矩阵.幸运的是,abind
包提供了一个函数来完成它,abind
(along=2
表示 cbind
,along=1
表示 rbind
)
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")
这篇关于从 PNG 文件的 grid.arrange 裁剪 ggsave 的顶部和底部输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!