[R] Arranging two different types of ggplot2 plots with axes lined up

Dennis Murphy djmuser at gmail.com
Fri Apr 19 20:00:43 CEST 2013


library(ggplot2)
library(gridExtra)

#generate test precipitation data
year<-c(2000,2001,2002,2003,2004)
precip<-c(46,100,80,74,20)
yp<-data.frame(year, precip)

#generate test fecal coliform data
year2<-c(2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,
2001,2001,2001,2001,2001,2001,2001,2001,2001,2001,
2002,2002,2002,2002,2002,2002,2002,2002,2002,2002,
2003,2003,2003,2003,2003,2003,2003,2003,2003,2003,
2004,2004,2004,2004,2004,2004,2004,2004,2004,2004)
fc<-sample(1:1000, 50)
yfc<-data.frame(year2, fc)

#make test precipitation plot
yp_plot<-ggplot(yp, aes(factor(year), y=precip)) + geom_point() +
         geom_line(aes(group = 1)) +
         labs(title = "Site X \n ", x = "",
              y = "Annual Precipitation (in.) \n ") +
         theme(axis.text.x = element_text(angle=45, hjust=1, vjust=1),
               axis.text.y = element_text(angle = 90, hjust = 0.5, vjust = 0.5))

#make test fecal coliform plot
yfc_plot<-ggplot(yfc) + geom_boxplot() + aes(x=factor(year2), y=fc) +
          theme(axis.text.x = element_text(angle=45, hjust=1, vjust=1),
                axis.text.y = element_text(angle = 90, hjust = 0.5,
vjust = 0.5)) +
          xlab(" \n Date") + ylab("Fecal coliforms (cfu/100 mL) \n ") +
          geom_smooth(stat='smooth', aes(group=1), size=1.5) +
          scale_y_log10()

#arrange plots together
grid.arrange(yp_plot, yfc_plot, ncol=1)

On Fri, Apr 19, 2013 at 8:49 AM, Saalem Adera <saalemadera at gmail.com> wrote:
> Hi all,
>
> Thanks for the quick replies.
>
> Dennis - I tried rotating the y-axis tick labels 90 degrees and while the
> x-axes became the same width, the x-axis values still didn't line up with
> each other.  So maybe I need to be more clear - how can I get the x-axis
> tick values to line up?  For example, I want the "2000" x-axis tick in the
> upper and lower plots to be on top of each other (along with all the other
> x-axis ticks).
>
> Andrés - I tried the multiplot() function that you suggested, with the same
> result as when I used the grid.arrange() function - the x-axis tick values
> still don't line up.
>
> The reason I want the x-axis tick values to line up is so that with a quick
> look at the plots, a viewer will be able to understand the relationship
> between the data depicted by the boxplots and the data depicted by the line
> plot.
>
> Any other ideas on how to make this work?
>
> Thanks,
> Saalem
>
>
>
> On Thu, Apr 18, 2013 at 2:22 PM, Andrés Aragón Martínez
> <armandres at gmail.com> wrote:
>>
>> Hi Saalem,
>>
>> Check the following:
>>
>> http://www.cookbook-r.com/Graphs/Multiple_graphs_on_one_page_(ggplot2)/
>>
>>
>> Regards,
>>
>>
>> Andrés AM
>>
>> El 18/04/2013, a las 09:47, Saalem Adera <saalemadera at gmail.com> escribió:
>>
>> > Hi all,
>> >
>> > I want to arrange two ggplot2 plots on the same page with their x-axes
>> > lined up - even though one is a boxplot and the other is a line plot.
>> > Is
>> > there a simple way to do this?  I know I could do this using facetting
>> > if
>> > they were both the same type of plot (for example, if they were both
>> > boxplots), but I haven't been able to figure it out for two different
>> > types
>> > of plots.
>> >
>> > Below is my test case:
>> >
>> > library(ggplot2)
>> > library(gridExtra)
>> >
>> > #generate test precipitation data
>> > year<-c(2000,2001,2002,2003,2004)
>> > precip<-c(46,100,80,74,20)
>> > yp<-data.frame(year, precip)
>> >
>> > #generate test fecal coliform data
>> > year2<-c(2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,
>> > 2001,2001,2001,2001,2001,2001,2001,2001,2001,2001,
>> > 2002,2002,2002,2002,2002,2002,2002,2002,2002,2002,
>> > 2003,2003,2003,2003,2003,2003,2003,2003,2003,2003,
>> > 2004,2004,2004,2004,2004,2004,2004,2004,2004,2004)
>> > fc<-sample(1:1000, 50)
>> > yfc<-data.frame(year2, fc)
>> >
>> > #make test precipitation plot
>> > yp_plot<-ggplot(yp) + geom_point() + geom_line() + aes(year, y=precip) +
>> > opts(title="Site X \n ", axis.text.x=theme_text(angle=45, hjust=1,
>> > vjust=1)) +
>> > ylab("Annual Precipitation (in.) \n ") + xlab("")
>> >
>> > #make test fecal coliform plot
>> > yfc_plot<-ggplot(yfc) + geom_boxplot() + aes(x=as.factor(year2), y=fc) +
>> > opts(axis.text.x=theme_text(angle=45, hjust=1, vjust=1)) +
>> > xlab(" \n Date") + ylab("Fecal coliforms (cfu/100 mL) \n ") +
>> > geom_smooth(stat='smooth', aes(group=1), size=1.5) + scale_y_log10()
>> >
>> > #arrange plots together
>> > grid.arrange(yp_plot, yfc_plot, ncol=1)
>> >
>> >
>> > You can see that I got the plot areas to line up using grid.arrange(),
>> > but
>> > the x-axes are still off.  I'd really appreciate any help I can get.
>> >
>> > Thanks,
>> > Saalem
>> >
>> >       [[alternative HTML version deleted]]
>> >
>> > ______________________________________________
>> > R-help at r-project.org mailing list
>> > https://stat.ethz.ch/mailman/listinfo/r-help
>> > PLEASE do read the posting guide
>> > http://www.R-project.org/posting-guide.html
>> > and provide commented, minimal, self-contained, reproducible code.
>>
>
>
>



More information about the R-help mailing list