Hi R Users, I was struggling to overlay two graphs created from the two different datasets using ggplot2.I could not overlay two figures. I wanted to plot second graph using second Y axis. but there was no provision. Furthermore, I could not join means of the box plots. I tried this way but did not work. Any suggestions? dat1<-structure(list(site = c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), layer = structure(c(2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 1L), .Label = c("bottom", "top"), class = "factor"), Present = c(120L, 125L, 123L, 23L, 21L, 19L, 131L, 124L, 127L, 24L, 27L, 25L, 145L, 143L, 184L, 29L, 14L, 17L, 38L)), .Names = c("site", "layer", "Present"), row.names = c(NA, 19L), class = "data.frame") dat1 dat2<-structure(list(site = 1:3, present = c(-3L, 2L, 5L)), .Names = c("site", "present"), row.names = c(NA, 3L), class = "data.frame") dat2 library("plyr") library(ggplot2) A<-ggplot(dat1, aes(x = factor(site), y = Present, colour = layer, fill=layer)) + geom_boxplot(outlier.shape = 16, outlier.size = 1) + theme_bw()+ ylim(0,185) # Here I wanted to join the means of the boxplots among the sites, but I could not join it. B<-ggplot(dat2, aes(x=factor(site),y= present, colour="blue") + geom_line() + geom_point()) # wanted to plot it using second y axis. A+B Thanks for your help. KG [[alternative HTML version deleted]]
Hi Kristi, There is a separate ggplot2 mailing list at https://groups.google.com/forum/#!forum/ggplot2, please post future ggplot2 questions there. On Mon, Jan 27, 2014 at 11:52 AM, Kristi Glover <kristi.glover at hotmail.com> wrote:> Hi R Users, > I was struggling to overlay two graphs created from the two different datasets using ggplot2.I could not overlay two figures. I wanted to plot second graph using second Y axis. but there was no provision. > Furthermore, I could not join means of the box plots. > > I tried this way but did not work. Any suggestions? > dat1<-structure(list(site = c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, > 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), layer = structure(c(2L, > 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, > 1L, 1L), .Label = c("bottom", "top"), class = "factor"), Present = c(120L, > 125L, 123L, 23L, 21L, 19L, 131L, 124L, 127L, 24L, 27L, 25L, 145L, > 143L, 184L, 29L, 14L, 17L, 38L)), .Names = c("site", "layer", > "Present"), row.names = c(NA, 19L), class = "data.frame") > dat1 > dat2<-structure(list(site = 1:3, present = c(-3L, 2L, 5L)), .Names = c("site", > "present"), row.names = c(NA, 3L), class = "data.frame") > dat2 > library("plyr") > library(ggplot2) > A<-ggplot(dat1, aes(x = factor(site), y = Present, colour = layer, fill=layer)) + > geom_boxplot(outlier.shape = 16, outlier.size = 1) + theme_bw()+ ylim(0,185) > # Here I wanted to join the means of the boxplots among the sites, but I could not join it.It would have been useful to show us what you tried, but here is one way: A + geom_line(aes( group = layer # ignore the fact that the x-axis is categorical ), position = position_dodge(width = 0.75), # match position of boxes stat="summary", # summarize raw data fun.y = mean, # by taking the mean color="black")> B<-ggplot(dat2, aes(x=factor(site),y= present, colour="blue") + > geom_line() + geom_point()) # wanted to plot it using second y axis.ggplot2 doesn't really support multiple y axes. You can fake it (check the ggplot2 mailing list and/or stackoverflow for examples) but it's not easy. Best, Ista> A+B > Thanks for your help. > KG > > > [[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.
On 01/28/2014 03:52 AM, Kristi Glover wrote:> Hi R Users, > I was struggling to overlay two graphs created from the two different datasets using ggplot2.I could not overlay two figures. I wanted to plot second graph using second Y axis. but there was no provision. > Furthermore, I could not join means of the box plots. > > I tried this way but did not work. Any suggestions? > dat1<-structure(list(site = c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, > 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), layer = structure(c(2L, > 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, > 1L, 1L), .Label = c("bottom", "top"), class = "factor"), Present = c(120L, > 125L, 123L, 23L, 21L, 19L, 131L, 124L, 127L, 24L, 27L, 25L, 145L, > 143L, 184L, 29L, 14L, 17L, 38L)), .Names = c("site", "layer", > "Present"), row.names = c(NA, 19L), class = "data.frame") > dat1 > dat2<-structure(list(site = 1:3, present = c(-3L, 2L, 5L)), .Names = c("site", > "present"), row.names = c(NA, 3L), class = "data.frame") > dat2 > library("plyr") > library(ggplot2) > A<-ggplot(dat1, aes(x = factor(site), y = Present, colour = layer, fill=layer)) + > geom_boxplot(outlier.shape = 16, outlier.size = 1) + theme_bw()+ ylim(0,185) > # Here I wanted to join the means of the boxplots among the sites, but I could not join it. > B<-ggplot(dat2, aes(x=factor(site),y= present, colour="blue") + > geom_line() + geom_point()) # wanted to plot it using second y axis. > A+BHi Kristi, It's not ggplot, but this might help. par(mar=c(5,4,4,4)) boxplot(Present~layer+site,data=dat1,staplewex=0, col=c(2,3,2,3,2,3),border=c(2,3,2,3,2,3), ylab="Present",xlab="factor(site)",xaxt="n") boxat<-c(1.5,3.3,5.5) axis(1,at=boxat,labels=1:3) library(plotrix) # or library(prettyR) newpresent<-rescale(c(-5,dat2$present),c(50,100))[-1] points(boxat,newpresent,type="b",col="blue") axis(4,at=c(50,75,100),labels=c(-5,0,5),col="blue") mtext("Means of sites",side=4,at=150,line=0.5,col="blue") legend(1,170,c("Top","Bottom"),fill=c("green","red")) par(mar=c(5,4,4,2)) Jim