Hello- I'm new to R, coding and stats. (Oh no.) Anyway, I have about 12000 data points in a data.frame (dealing with dimensions and geological stage information for fossil protists) and have plotted them in a basic scatter plot. I also added a boxplot to overlay these points. Each worked fine independently, but when I attempt to superimpose them with add=true, I get a different scale for the boxplot, which results in it being shifted back to the left relative to the scatter plot. How do I force the boxplot to use the scatterplot's scale or something to that effect? My code. I'm not sure how to include the file I'm using as a dataframe: #imports data emdata<-read.table(file="#a file in my working directory", sep=",", header=T) #specifies a dataframe foram<-data.frame(emdata) #calculates actual dimensions in mm foram$actual_l<-foram$length/foram$magnification foram$actual_w<-foram$width/foram$magnification foram$actual_h<-foram$height/foram$magnification #takes logs of all dimensions foram$logl<-log10(foram$actual_l) foram$logw<-log10(foram$actual_w) foram$logh<-log10(foram$actual_h) #Generates scatterplot plot(foram$stage, foram$logl, ylab="log max size", xlab="stage", cex=.1, xaxt="n", axes=FALSE ) axis(at=-3:1, side=2, pos=0) axis(at=9:84, side=1, pos=-3) #adds a boxplot boxplot(foram$logl~foram$stage, data=foram, outline=F, axes=FALSE, add=TRUE, col="gray82", medlwd=1) -- View this message in context: http://r.789695.n4.nabble.com/Boxplots-over-a-Scatterplot-tp2281526p2281526.html Sent from the R help mailing list archive at Nabble.com.
Would it really be so hard to provide commented, minimal, self-contained, _reproducible_ code ? -Peter Ehlers On 2010-07-07 15:16, fsch wrote:> > Hello- > > I'm new to R, coding and stats. (Oh no.) > Anyway, I have about 12000 data points in a data.frame (dealing with > dimensions and geological stage information for fossil protists) and have > plotted them in a basic scatter plot. I also added a boxplot to overlay > these points. Each worked fine independently, but when I attempt to > superimpose them with add=true, I get a different scale for the boxplot, > which results in it being shifted back to the left relative to the scatter > plot. How do I force the boxplot to use the scatterplot's scale or something > to that effect? > > My code. I'm not sure how to include the file I'm using as a dataframe: > > #imports data > > emdata<-read.table(file="#a file in my working directory", sep=",", > header=T) > > #specifies a dataframe > > foram<-data.frame(emdata) > > #calculates actual dimensions in mm > > foram$actual_l<-foram$length/foram$magnification > foram$actual_w<-foram$width/foram$magnification > foram$actual_h<-foram$height/foram$magnification > > #takes logs of all dimensions > > foram$logl<-log10(foram$actual_l) > foram$logw<-log10(foram$actual_w) > foram$logh<-log10(foram$actual_h) > > #Generates scatterplot > > plot(foram$stage, > foram$logl, > ylab="log max size", > xlab="stage", > cex=.1, > xaxt="n", > axes=FALSE > ) > > > axis(at=-3:1, side=2, pos=0) > > axis(at=9:84, side=1, pos=-3) > > #adds a boxplot > > boxplot(foram$logl~foram$stage, data=foram, > outline=F, axes=FALSE, add=TRUE, col="gray82", medlwd=1) > >
I managed to solve the problem myself using: q=sort(unique(foram$stage[which(foram$length>0)])) boxplot(foram$logl~foram$stage, data=foram, outline=F, at=q, axes=TRUE, add=TRUE, col="gray82", medlwd=1) points(foram$stage,foram$logl, cex=.1) However, for future reference since this was my first post and I was trying to give code like you requested, what did I do wrong? -- View this message in context: http://r.789695.n4.nabble.com/Boxplots-over-a-Scatterplot-tp2281526p2281690.html Sent from the R help mailing list archive at Nabble.com.
Try scatterplot() in the car package. It draws boxplots for X & Y in the margins, auto scaled to the axes fsch wrote:> Hello- > > I'm new to R, coding and stats. (Oh no.) > Anyway, I have about 12000 data points in a data.frame (dealing with > dimensions and geological stage information for fossil protists) and have > plotted them in a basic scatter plot. I also added a boxplot to overlay > these points. Each worked fine independently, but when I attempt to > superimpose them with add=true, I get a different scale for the boxplot, > which results in it being shifted back to the left relative to the scatter > plot. How do I force the boxplot to use the scatterplot's scale or something > to that effect? > > My code. I'm not sure how to include the file I'm using as a dataframe: > > #imports data > > emdata<-read.table(file="#a file in my working directory", sep=",", > header=T) > > #specifies a dataframe > > foram<-data.frame(emdata) > > #calculates actual dimensions in mm > > foram$actual_l<-foram$length/foram$magnification > foram$actual_w<-foram$width/foram$magnification > foram$actual_h<-foram$height/foram$magnification > > #takes logs of all dimensions > > foram$logl<-log10(foram$actual_l) > foram$logw<-log10(foram$actual_w) > foram$logh<-log10(foram$actual_h) > > #Generates scatterplot > > plot(foram$stage, > foram$logl, > ylab="log max size", > xlab="stage", > cex=.1, > xaxt="n", > axes=FALSE > ) > > > axis(at=-3:1, side=2, pos=0) > > axis(at=9:84, side=1, pos=-3) > > #adds a boxplot > > boxplot(foram$logl~foram$stage, data=foram, > outline=F, axes=FALSE, add=TRUE, col="gray82", medlwd=1) > >