使用ggplot在多个页面上创建多个图表 [英] Multiple graphs over multiple pages using ggplot

查看:113
本文介绍了使用ggplot在多个页面上创建多个图表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在对我的数据进行探索性分析,并需要使用ggplot绘制多个图形。图形的数量真的很大(206站),并且我想要在他们需要的很多页面上将它们绘制成1列而每页8行。我知道像viewport或grid.arrange这样的函数,但我并没有设法让它们在这种情况下工作。我已经注意到layout()和par(mfrow = c(8,1))不能与ggplot一起工作,但是我发送了一部分代码,使其陷入了低谷处。任何帮助将非常感激!

  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 (a =(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()+
主题(
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()


解决方案

  library(plyr)
library(gridExtra)

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

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

未经测试。

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

解决方案

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.

这篇关于使用ggplot在多个页面上创建多个图表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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