Saalem Adera
2013-Apr-18 14:47 UTC
[R] Arranging two different types of ggplot2 plots with axes lined up
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]]
Andrés Aragón Martínez
2013-Apr-18 18:22 UTC
[R] Arranging two different types of ggplot2 plots with axes lined up
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.