保存png的ggplot2对象没有边距

时间:2015-09-28 00:10:07

标签: r ggplot2

我正在编写一个Web应用程序,它需要精确的数字维度。我决定用ggplot2制作数字,因为它们需要来自R的专门文本。我希望创建的数字没有边距,因为它们将通过JavaScript旋转。我使用此页面了解如何减少边距:https://kohske.wordpress.com/2010/12/25/drawing-on-full-region-in-ggplot2/但无法打印到没有边框的.png文件。这是示例代码。

library(ggplot2)
library(gtable)

circle <- function(center = c(0,0),diameter = 1, npoints = 100){
  r = diameter / 2
  tt <- seq(0,2*pi,length.out = npoints)
  xx <- center[1] + r * cos(tt)
  yy <- center[2] + r * sin(tt)
  data.frame(x = xx, y = yy)
}



dat <- circle(c(0,0),1,npoints = 1000)

plot1 <- ggplot(dat,aes(x,y)) + 
  geom_path() +
  theme(axis.text.y=element_blank(),
        axis.text.x=element_blank(),
        axis.ticks=element_blank(),
        axis.ticks.length = unit(0,"null"),
        axis.ticks.margin = unit(0,"null"),
        axis.title.x=element_blank(),
        axis.title.y=element_blank(),
        legend.position="none",
        panel.background = element_blank(),
        panel.grid = element_blank(),
        title = element_blank(),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.margin = unit(0,"null"),
        plot.margin = rep(unit(0,"null"),4),
        axis.ticks.length = unit(0,"cm"),
        axis.ticks.margin = unit(0,"cm"))

png("plot.png", width=434, height=434)
print(plot1)
dev.off()

这会输出一个具有合适大小边框的圆圈。让我准确一点,我想要的是一个434x434像素的png,一个没有边框的圆圈(例如圆的直径是434px)。我可以创建一个更大的文件然后裁剪下来,但我将制作约50个这些图形。谢谢你的帮助!

4 个答案:

答案 0 :(得分:2)

你也可以这样做:

scale_x_continuous(expand=c(0,0)) +
scale_y_continuous(expand=c(0,0)) +
labs(x=NULL, y=NULL, title=NULL) +

答案 1 :(得分:1)

您可以通过将plot.margin更改为此来删除保证金:

plot.margin = unit(rep(-1.25,4),"lines"),

答案 2 :(得分:0)

library(ggplot2)
library(gtable)

circle <- function(center = c(0,0),diameter = 1, npoints = 100){
  r = diameter / 2
  tt <- seq(0,2*pi,length.out = npoints)
  xx <- center[1] + r * cos(tt)
  yy <- center[2] + r * sin(tt)
  data.frame(x = xx, y = yy)
}

dat <- circle(c(0,0),0.5,npoints = 1000)

plot1 <- par(mar=c(0,0,0,0))
plot1 <- ggplot(dat,aes(x,y)) + 
  geom_path() +
  theme(axis.text.y=element_blank(),panel.margin = unit(c(0,0,0,0), "lines"),
        axis.text.x=element_blank(),
        axis.ticks=element_blank(),
        axis.ticks.length = unit(0,"null"),
        axis.ticks.margin = unit(0,"null"),
        axis.title.x=element_blank(),
        axis.title.y=element_blank(),
        legend.position="none",
        panel.background = element_blank(),
        panel.grid = element_blank(),
        title = element_blank(),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.margin = unit(0,"null"),
        plot.margin = rep(unit(0,"null"),4),
        axis.ticks.length = unit(0,"cm"),
        axis.ticks.margin = unit(0,"cm")) + scale_x_continuous(expand=c(0,0)) +
        scale_y_continuous(expand=c(0,0)) + labs(x=NULL, y=NULL, title=NULL)


png("plot.png", width=434, height=434)
print(plot1)
dev.off()

答案 3 :(得分:0)

这是另一种选择,

library(ggplot2)
library(grid)
p <- qplot(1,1, geom="blank")+annotation_custom(circleGrob())
png("test.png", width=434, height=434)
grid.draw(ggplotGrob(p)[3,4])
dev.off()

enter image description here