Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
597 views
in Technique[技术] by (71.8m points)

r - Multiple graphs over multiple pages using ggplot

I am doing an exploratory analysis of my data and need to plot multiple graphics using ggplot. The amount of graphics is really huge (206 Stations), and I wanted to plot them in 1 column vs. 8 rows per page over the so many pages needed. I am aware of functions like viewport or grid.arrange, but I am not managing to make them work in this case. I have already noticed that layout() nor par(mfrow=c(8,1)) do not work with ggplot, but I send the part of the code where I am stuck bellow. Any help would be much appreciated!

pdf('test.pdf', width=21, height=27)
par(mfrow=c(8,1))
for(i in levels(tab$Station))
{

print(ggplot(tab[tab$Station==i], aes(x=Date)) +
  geom_line(aes(y=Tmin), col="blue", size=0.1) + 
  geom_line(aes(y=Tmax), col="red", size=0.1) + 
  geom_text(aes(x=as.Date('2010-01-01'), y=45), label=i) +
  ylim(0, 45) + 
  scale_x_date(labels = date_format("%Y")) +
  theme_bw() +
  theme(
    plot.background = element_blank()
    ,panel.grid.major = element_blank()
    ,panel.grid.minor = element_blank()
    ,panel.border = element_rect(color = 'black')
    ,panel.background = element_blank()

  )
)

}

dev.off()
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
library(plyr)
library(gridExtra)

p = ggplot(tab, aes(x=Date)) +
       geom_line(aes(y=Tmin), col="blue", size=0.1)

plots = dlply(tab , "Station", `%+%`, e1 = p)
ml = do.call(marrangeGrob, c(plots, list(nrow=8, ncol=1)))
ggsave("multipage.pdf", ml)

untested.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...