2017-06-15 49 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~.)參數。