Reputation: 3207
I need to modify some annotation of a plot, so I have to save the ggplot2 plot and do
ggplot_build(plot.obj)
and then plot it with
plot(ggplot_gtable(ggplot_build(plot.obj)))
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()
the resulting pdf has a blank page before the plot page... How can I avoid this?
Check 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()
The question would be how to make a pdf with ggplot_gtable without that blank page?
Upvotes: 2
Views: 2808
Reputation: 17648
Simply do:
plot(ggplot_gtable(box2))
ggsave(filename = "my_plot.pdf")
Upvotes: 1
Reputation: 6325
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()
Upvotes: 3