[R] Arranging two different types of ggplot2 plots with axes lined up
dwinsemius at comcast.net
Fri Apr 19 21:58:15 CEST 2013
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.
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
> Any other ideas on how to make this work?
> On Thu, Apr 18, 2013 at 2:22 PM, Andrés Aragón Martínez <armandres at gmail.com
>> Hi Saalem,
>> Check the following:
>> 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
>>> of plots.
>>> Below is my test case:
>>> #generate test precipitation data
>>> yp<-data.frame(year, precip)
>>> #generate test fecal coliform data
>>> 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(),
>>> the x-axes are still off. I'd really appreciate any help I can get.
>>> [[alternative HTML version deleted]]
>>> R-help at r-project.org mailing list
>>> PLEASE do read the posting guide
>>> and provide commented, minimal, self-contained, reproducible code.
> [[alternative HTML version deleted]]
> R-help at r-project.org mailing list
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
Alameda, CA, USA
More information about the R-help