2017-06-15 76 views
0

我使用grid.arrange()绘制一列四个折线图。如果尺寸很大,则将文件保存为.png或.pdf时,grobs看起来不错。但是,当我缩小剧情的高度时,顶部的grob会被压缩。使用autoplot预防grid.arrange压缩grobs

如何防止grid.arrange压缩grobs?

一些丑陋的代码:

(a<-autoplot(mars.prcp1yrs) + labs(y="", x="") +theme_light()+ylim(60,210)+ 
    theme(text=element_text(size=8), 
     axis.text.y=element_text(size=8),axis.text.x=element_blank(), 
     axis.title.y=element_blank(), 
     axis.ticks.x=element_blank(), 
     plot.margin=unit(c(0.1,0.1,0.1,0.1),"in"))) 

(b<-autoplot(jupiter.prcp1yrs) + labs(y="",x="")+ theme_light()+ylim(60,210)+ 
    theme(text=element_text(size=8),axis.text.y=element_text(size=8), 
     axis.text.x=element_blank(),axis.title.y=element_blank(), 
     axis.ticks.x=element_blank(),plot.margin=unit(c(-0.3,0.1,0.1,0.1),"in"))) 

(c<-autoplot(saturn.prcp1yrs) +labs(y="",x="") + theme_light()+ylim(60,210)+ 
    theme(text=element_text(size=8), 
     axis.text=element_text(size=8), 
     axis.text.x=element_blank(),axis.title.y=element_blank(), 
     axis.ticks.x=element_blank(),plot.margin=unit(c(-0.3,0.1,0.1,0.1),"in"))) 

(d<-autoplot(earth.prcp1yrs) +labs(y="",x="") +theme_light()+ylim(60,210)+ 
    theme(text=element_text(size=8),axis.text=element_text(size=8), 
     axis.ticks.x=element_blank(),axis.title.y=element_blank(), 
     plot.margin=unit(c(-0.3,0.1,0.1,0.1),"in"))) 


prcp.grid<-grid.arrange(a,b,c,d, ncol=1) 


png("plot.png",width=3740,height=1000,res=500) 
old.par <- par(mfrow=c(2, 2)) 
grid.arrange(prcp.grid, ncol=2) 
par(old.par) 
dev.off() 

这里是输出 (I使用该纵横比只是戏剧化顶端GROB的压缩。): enter image description here

+0

**可再现的例子**。 'fl.idaho.prcp1yrs'这是什么?使用'dput()' – Masoud

+0

这是一个拥有80年日常降水量数据的动物园对象。有没有办法减少输出量? – SoilSciGuy

+0

请阅读[如何在R中创建一个很好的重现示例](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – Masoud

回答

0

我发现,它是最好使用fortify()将动物园对象转换为数据框。

因此,按照上面的例子中,我首先转化各个动物园对象的数据帧:

mars.prcp.1yr<-fortify(mars.prcp1yrs) 
jupiter.prcp.1yr<-fortify(jupiter.prcp1yrs) 
saturn.prcp.1yr<-fortify(saturn.prcp1yrs) 
earth.prcp.1yr<-fortify(earth.prcp1yrs) 

然后我说的公共变量(行星),以每个数据帧:

#add planet as variable 
mars.prcp.1yr$planet <- "Mars" 
jupiter.prcp.1yr$planet <- "Jupiter" 
saturn.prcp.1yr$planet <- "Saturn" 
earth.prcp.1yr$planet <- "Earth" 

变化动物园对象名称更好地表示该变量:

#change to common colnames 
    mars.prcp.1yr <- rename(mars.prcp.1yr, Precipitation = mars.prcp1yrs) 
    jupiter.prcp.1yr <- rename(jupiter.prcp.1yr, Precipitation = jupiter.prcp1yrs) 
    saturn.prcp.1yr <- rename(saturn.prcp.1yr, Precipitation = saturn.prcp1yrs) 
    earth.prcp.1yr <- rename(earth.prcp.1yr, Precipitation = earth.prcp1yrs) 

然后,行绑定所有数据帧i n要单个数据帧:

prcp.1yr <- bind_rows(mars.prcp.1yr,jupiter.prcp.1yr,saturn.prcp.1yr,earth.prcp.1yr) 

然后使用在标准一个ggplot()呼叫的facet_grid(planet~.)参数。