2017-09-22 3 views
0

Ich brauche eine Anmerkung von einem Grundstück zu ändern, so habe ich das ggplot2 Grundstück zu speichern und zu tunR ggplot2: Leerseite vor Grundstück mit pdf() und ggplot_gtable()/ggplot_build()

ggplot_build(plot.obj) 

und dann plotten mit

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

das Problem ist, wenn ich es in einem pdf wie diese

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

die resultierende pDF speichern hat eine leere pag e vor der Plot-Seite ... Wie kann ich das vermeiden?

prüfen diese 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() 

Die Frage wäre, wie ohne diese leere Seite ein PDF mit ggplot_gtable zu machen?

Antwort

1

Dieses Argument onefile=FALSE behebt dieses Problem!

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() 
+0

In Ordnung! Vielen Dank!! – DaniCee

+0

Hey, das funktioniert nur für eine Seite PDF, einige Hack für mehrseitige erforderlich! – amrrs

1

einfach tun:

plot(ggplot_gtable(box2)) 
ggsave(filename = "my_plot.pdf")