R ggplot2:使用pdf()和ggplot_gtable()/ggplot_build()进行绘图前的空白页 [英] R ggplot2: Blank page before plot with pdf() and ggplot_gtable() / ggplot_build()

查看:85
本文介绍了R ggplot2:使用pdf()和ggplot_gtable()/ggplot_build()进行绘图前的空白页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要修改绘图的一些注释,所以我必须保存ggplot2绘图并执行

I need to modify some annotation of a plot, so I have to save the ggplot2 plot and do

ggplot_build(plot.obj)

然后用

plot(ggplot_gtable(ggplot_build(plot.obj)))

问题是,当我将其保存在这样的pdf文件中

the problem is, when I save it in a pdf like this

pdf(file="test.pdf")
  plot(ggplot_gtable(ggplot_build(plot.obj)))
dev.off()

生成的pdf在绘图页之前有一个空白页...如何避免这种情况?

the resulting pdf has a blank page before the plot page... How can I avoid this?

检查此MWE

data(iris)
library(ggplot2)
box <- ggplot(data=iris, aes(x=Species, y=Sepal.Length)) +
    geom_boxplot(aes(fill=Species)) +
    ylab("Sepal Length") + ggtitle("Iris Boxplot") +
    stat_summary(fun.y=mean, geom="point", shape=5, size=4) 
box2 <- ggplot_build(box)
#I do stuff here
pdf(file="test.pdf")
  plot(ggplot_gtable(box2))
dev.off()

问题是如何使用ggplot_gtable制作没有空白页的pdf文件?

The question would be how to make a pdf with ggplot_gtable without that blank page?

推荐答案

此参数 onefile = FALSE 可以解决此问题!

this argument onefile=FALSE fixes this!

data(iris)
library(ggplot2)
box <- ggplot(data=iris, aes(x=Species, y=Sepal.Length)) +
  geom_boxplot(aes(fill=Species)) +
  ylab("Sepal Length") + ggtitle("Iris Boxplot") +
  stat_summary(fun.y=mean, geom="point", shape=5, size=4) 
box2 <- ggplot_build(box)
#I do stuff here
pdf.options(reset = TRUE, onefile = FALSE)
pdf(file="test.pdf")
my_plot <- plot(ggplot_gtable(box2))
#ggsave("test1.png", plot = my_plot,dev = 'png')
print(my_plot)
dev.off()

这篇关于R ggplot2:使用pdf()和ggplot_gtable()/ggplot_build()进行绘图前的空白页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆