【发布时间】:2013-05-02 17:50:27
【问题描述】:
我将带有 ggplot 的绘图保存为 .png。背景必须是黑色的,但总是有一个小的白色边距(只有顶部,在左边;不是右边)。
我怎样才能去掉这个边距?
谢谢!
这是我的代码
library(ggplot2)
require(grid)
dat <- data.frame("xvar"=runif(500, 1, 10),
"yvar"=runif(500, 1, 10))
n <- 1
for(i in 1:n){
png(file=paste("green", i, ".png", sep=""), width=400, height=400)
x <- sample(500, 50)
i <- ggplot(data=dat[x,], aes(x=xvar, y=yvar))+
geom_point(col="green", size=3,shape=15)+
theme(panel.background=element_rect(fill="black"), panel.grid.minor=element_blank(),
panel.grid.major=element_blank(), axis.text.x=element_blank(), axis.text.y=
element_blank(), axis.title.x=element_blank(), axis.title.y=element_blank(),
axis.ticks=element_blank(), plot.background=element_rect(fill="black"),
panel.margin = unit(c(0,0,0,0), "cm"), plot.margin = unit(c(0,0,0,0), "cm"))+
scale_x_continuous()
print(i)
dev.off() }
【问题讨论】:
-
我没看到;您是否尝试过其他设备(例如 pdf、cairo_png、quartz_png 等?)
-
+1 表示关于 SO 的第一个问题,包括可重现的数据和代码。在 Windows 7 x64 上,我还在 png 图像的左侧和底部看到使用 R 2.15.3 的白边距。但不确定是什么原因造成的。
-
边距非常小(~ 1 pix)。我在所有设备上都有同样的问题。
-
我使用的是 win 7 x64、R 3.0.0 和 R-Studio 0.97.449。
-
此修复适用于我的设置:
plot.background=element_rect(fill="black", colour=NA)