[R] Arranging two different types of ggplot2 plots with axes lined up
David Winsemius
dwinsemius at comcast.net
Sat Apr 20 00:14:21 CEST 2013
On Apr 19, 2013, at 2:06 PM, Saalem Adera wrote:
> Thanks! I really appreciate everyone's help. I was able to get the x-axes ticks very close (close enough!) to lining up on the test data using both Dennis and David's code. So, I've got a work-around. However, I need to do this same operation for many other sets of plots, with varied data record lengths, meaning that using these methods would require a lot of trial and error.
>
> So, I am now wondering - does anyone know of a way to automate the "snapping" of two x-axes to each other so that they automatically line up the way they do when facetting is used? Or, better yet, is there a way to change the regular ggplot2 facetting code so that different plot types can be specified?
I can tell you that I attempted to "transplant" the 'scales' value in the lower plot into the scales node of the upper plot and that failed to do anything (good or bad). I suspect it because the settings were on "auto" and then got computed on the fly for the particular geom settings.
--
David.
>
> Thanks again,
> Saalem
>
>
> On Fri, Apr 19, 2013 at 3:58 PM, David Winsemius <dwinsemius at comcast.net> wrote:
> Adding this to the first plot object seems to come pretty close:
>
> yp_plot + xlim(1999.6, 2004.4)
>
> I also tried adding and subtracting 0.5. It does not appear that 0.4 is an exact solution.
>
> --
> David
> On Apr 19, 2013, at 8:49 AM, Saalem Adera 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.
> >>
> >>
> >
> > [[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.
>
> David Winsemius
> Alameda, CA, USA
>
>
>
>
>
David Winsemius
Alameda, CA, USA
More information about the R-help
mailing list