0

I use grid.arrange() to plot 4 line charts with one column. The grobs look OK when saving the file as a .png or .pdf, if the dimensions are large. However, when I shrink the height of the plot, the top grob gets compressed.

How can I prevent grid.arrange from compressing grobs?

Some ugly code:

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

Here is the output (I used this aspect ratio just to dramatize compression of the top grob.): enter image description here

derelict
  • 3,657
  • 3
  • 24
  • 29
  • **Reproducible example**. `fl.idaho.prcp1yrs` what is it? use `dput()` – M-- Jun 15 '17 at 22:21
  • It's a zoo object with 80 years of daily precipitation data. Is there a way to reduce dput output? – derelict Jun 15 '17 at 22:58
  • Please read [How to make a great reproducible example in R?](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – M-- Jun 15 '17 at 23:01
  • After making the minimal example of your dataset check it to see if it gives the same output (error). – M-- Jun 15 '17 at 23:02
  • Maybe make the y-axis "free"? – IRTFM Jun 16 '17 at 01:00

1 Answers1

0

I've found that it is best to convert the zoo object into a data frame using fortify().

So following the above example, I first converted each zoo object to a data frame:

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

Then I added a common variable (planet) to each data frame:

#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 the zoo object name to better represent the variable:

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

Then, row bind all data frames into a single data frame:

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

Then use the facet_grid(planet~.) argument in standard a ggplot() call.

derelict
  • 3,657
  • 3
  • 24
  • 29