使用 grid.arrange,您如何减少合并的 .png 文件周围的边距?
With grid.arrange, how do you reduce margins around combined .png files?
有几十对 .png 图像要组合(并作为带有 knitr
和 LaTeX
的图形包含在 PDF 文件中),我发现 grid.arrange
留下了很大的边距在图像的顶部和下方。图像的默认大小为 480 x 480 像素。我怎样才能减少白色 space?
library(png)
library(grid)
img <- readPNG(system.file("img", "Rlogo.png", package="png"))
g1 <- rasterGrob(img, interpolate=TRUE, width = .3, height=.3)
g2 <- rasterGrob(img, interpolate=TRUE,width = .6, height=.6)
grid.arrange(g1, g2, nrow=1) # displays in the RStudio plot window
dev.copy(png,'r logos.png') # creates .png file in working directory
dev.off() # inserts arranged image; 53KB; 480 x 480 pixels
请注意此文本与 .png 图像开头之间的白色 space。
此文本标记 .png 图像的底部。
这个问题ggplot margin, not grid.arrange不相关。
在您的代码中,您将 grobs 的大小设置为视口的 30% 或 60%,因此空白 space 是正常的。根据您的问题描述,您可能希望使用物理单位(例如英寸或厘米),并设置设备大小以匹配总大小。例如,
library(png)
library(grid)
library(gridExtra)
img <- readPNG(system.file("img", "Rlogo.png", package="png"))
g1 <- rasterGrob(img, interpolate=TRUE, width = unit(1,"in"), height=unit(1,"in"))
g2 <- rasterGrob(img, interpolate=TRUE,width = unit(2,"in"), height=unit(2,"in"))
png("fit.png",width=720,height=480,res = 480/2)
grid.arrange(g1, g2, nrow=1,widths=c(1,2))
dev.off()
生产
有几十对 .png 图像要组合(并作为带有 knitr
和 LaTeX
的图形包含在 PDF 文件中),我发现 grid.arrange
留下了很大的边距在图像的顶部和下方。图像的默认大小为 480 x 480 像素。我怎样才能减少白色 space?
library(png)
library(grid)
img <- readPNG(system.file("img", "Rlogo.png", package="png"))
g1 <- rasterGrob(img, interpolate=TRUE, width = .3, height=.3)
g2 <- rasterGrob(img, interpolate=TRUE,width = .6, height=.6)
grid.arrange(g1, g2, nrow=1) # displays in the RStudio plot window
dev.copy(png,'r logos.png') # creates .png file in working directory
dev.off() # inserts arranged image; 53KB; 480 x 480 pixels
请注意此文本与 .png 图像开头之间的白色 space。
此文本标记 .png 图像的底部。
这个问题ggplot margin, not grid.arrange不相关。
在您的代码中,您将 grobs 的大小设置为视口的 30% 或 60%,因此空白 space 是正常的。根据您的问题描述,您可能希望使用物理单位(例如英寸或厘米),并设置设备大小以匹配总大小。例如,
library(png)
library(grid)
library(gridExtra)
img <- readPNG(system.file("img", "Rlogo.png", package="png"))
g1 <- rasterGrob(img, interpolate=TRUE, width = unit(1,"in"), height=unit(1,"in"))
g2 <- rasterGrob(img, interpolate=TRUE,width = unit(2,"in"), height=unit(2,"in"))
png("fit.png",width=720,height=480,res = 480/2)
grid.arrange(g1, g2, nrow=1,widths=c(1,2))
dev.off()
生产