Dear list, How can I add the mean and standard deviation to each of the boxplots using the example provided in the boxplot function? boxplot(len ~ dose, data = ToothGrowth, boxwex = 0.25, at = 1:3 - 0.2, subset = supp == "VC", col = "yellow", main = "Guinea Pigs' Tooth Growth", xlab = "Vitamin C dose mg", ylab = "tooth length", ylim = c(0, 35), yaxs = "i") boxplot(len ~ dose, data = ToothGrowth, add = TRUE, boxwex = 0.25, at = 1:3 + 0.2, subset = supp == "OJ", col = "orange") legend(2, 9, c("Ascorbic acid", "Orange juice"), fill = c("yellow", "orange")) Thanks for any help, Tom --------------------------------- Jämför pris på flygbiljetter och hotellrum: http://shopping.yahoo.se/c-169901-resor-biljetter.html [[alternative HTML version deleted]]
Richard.Cotton at hsl.gov.uk
2008-Feb-04 16:33 UTC
[R] adding the mean and standard deviation to boxplots
>> How can I add the mean and standard deviation to each of the boxplotsusing the example provided in the boxplot function? boxplot(len ~ dose, data = ToothGrowth) #You could add the mean as a point points(tapply(len, dose, mean), col="red") Alternatively, replace the median value with the mean, by writing a replacement for fivenum that calculates mean instead of median, then call bxp instead of boxplot. As for the standard deviation, are you sure you want this? Standard deviation only makes sense if the data are normally distributed, and boxplots are typically used to examine how the data are distributed (i.e., if you knew they were normally distributed already, you wouldn't need to draw the boxplot). Regards, Richie. Mathematical Sciences Unit HSL ------------------------------------------------------------------------ ATTENTION: This message contains privileged and confidential inform...{{dropped:20}}
Richard.Cotton at hsl.gov.uk
2008-Feb-04 16:54 UTC
[R] adding the mean and standard deviation to boxplots
> "As for the standard deviation, are you sure you want this? Standard > deviation only makes sense if the data are normally distributed..." > -------------- > > -- This is false, of course. What you probably meant to say is something > like: > > "The sample standard deviation may not tell you what you think it tellsyou> if the underlying data are not distributed something like a normal > distribution."Yes. How very sloppy of me. Consider my wrist slapped. Regards, Richie. Mathematical Sciences Unit HSL ------------------------------------------------------------------------ ATTENTION: This message contains privileged and confidential inform...{{dropped:20}}
Gabor Grothendieck
2008-Feb-04 16:59 UTC
[R] adding the mean and standard deviation to boxplots
Not precisely what you asked for but see the notch= argument to boxplot for a graphic measure of variability. If you simply wish to print certain statistics below the numbers already on the X axis then see: https://stat.ethz.ch/pipermail/r-help/2008-January/152994.html On Feb 4, 2008 10:41 AM, Tom Cohen <tom.cohen78 at yahoo.se> wrote:> Dear list, > > How can I add the mean and standard deviation to each of the boxplots using the example provided in the boxplot function? > > boxplot(len ~ dose, data = ToothGrowth, > boxwex = 0.25, at = 1:3 - 0.2, > subset = supp == "VC", col = "yellow", > main = "Guinea Pigs' Tooth Growth", > xlab = "Vitamin C dose mg", > ylab = "tooth length", ylim = c(0, 35), yaxs = "i") > boxplot(len ~ dose, data = ToothGrowth, add = TRUE, > boxwex = 0.25, at = 1:3 + 0.2, > subset = supp == "OJ", col = "orange") > legend(2, 9, c("Ascorbic acid", "Orange juice"), > fill = c("yellow", "orange")) > > Thanks for any help, > Tom > > > --------------------------------- > > J?mf?r pris p? flygbiljetter och hotellrum: http://shopping.yahoo.se/c-169901-resor-biljetter.html > [[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. > >
There are many ways to do it. The following will place a blue point on the boxplot at the mean, then print the mean at the bottom of the plot. In some plots I've gone too far and included median points and values as well. You could also put 95% CI on the same plot, but it would get perhaps too "busy." # Plot boxplot of vitamin C subset bx <- boxplot(len ~ dose, data = ToothGrowth, boxwex = 0.25, at = 1:3 - 0.2, subset = supp == "VC", col = "yellow", main = "Guinea Pigs' Tooth Growth", xlab = "Vitamin C dose mg", ylab = "tooth length", ylim = c(0, 35), yaxs = "i") # keep location at <- c(1:length(bx$names)) # find means, plot as points SubTc <-subset(ToothGrowth, supp == "VC") means <- by(SubTc$len, SubTc$dose, mean,na.rm=TRUE) points(at - 0.2, means, pch = 19, col = "blue") # write mean values text(at - 0.1, 1, labels = formatC(means, format = "f", digits = 1), pos = 2, cex = 1, col = "red") # Orange juice subset boxplot(len ~ dose, data = ToothGrowth, add = TRUE, boxwex = 0.25, at = 1:3 + 0.2, subset = supp == "OJ", col = "orange") # find means, plot as points SubTo <-subset(ToothGrowth, supp == "OJ") meano <- by(SubTo$len, SubTo$dose, mean,na.rm=TRUE) points(at + 0.2,meano, pch = 19, col = "blue") # write mean values text(at + 0.3, 1, labels = formatC(meano, format = "f", digits = 1), pos = 2, cex = 1, col = "orange") legend(2, 9, c("Ascorbic acid", "Orange juice"), fill = c("yellow", "orange")) ------------------------------ THT, I'm sure my R code is not as efficient as it could be. Harold Baize Butte County Department of Behavioral Health Tom Cohen-2 wrote:> > Dear list, > > How can I add the mean and standard deviation to each of the boxplots > using the example provided in the boxplot function? > > boxplot(len ~ dose, data = ToothGrowth, > boxwex = 0.25, at = 1:3 - 0.2, > subset = supp == "VC", col = "yellow", > main = "Guinea Pigs' Tooth Growth", > xlab = "Vitamin C dose mg", > ylab = "tooth length", ylim = c(0, 35), yaxs = "i") > boxplot(len ~ dose, data = ToothGrowth, add = TRUE, > boxwex = 0.25, at = 1:3 + 0.2, > subset = supp == "OJ", col = "orange") > legend(2, 9, c("Ascorbic acid", "Orange juice"), > fill = c("yellow", "orange")) > > Thanks for any help, > Tom > > > --------------------------------- > > J?mf?r pris p? flygbiljetter och hotellrum: > http://shopping.yahoo.se/c-169901-resor-biljetter.html > [[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. > >-- View this message in context: http://www.nabble.com/adding-the-mean-and-standard-deviation-to-boxplots-tp15271398p15278974.html Sent from the R help mailing list archive at Nabble.com.
Felipe Carrillo
2008-Feb-05 18:58 UTC
[R] adding the mean and standard deviation to boxplots
Tom: You can do this with ggplot2. The code below puts 95% CI,a smooth line and the mean(blue point)on the same plot. Felipe library(ggplot2) r <- ggplot(ToothGrowth, aes(y=len, x=factor(dose))) r$background.fill = "cornsilk" r + geom_boxplot(aes(colour=supp)) + stat_summary(aes(group=supp),fun="mean_cl_normal",colour="red",geom="smooth",linetype=3,size=1) + stat_summary(aes(group=supp),fun="mean_cl_normal",colour="blue",geom="point",size=4) grid.gedit("label", gp=gpar(fontsize=10, col="red")) # color the axis labels Felipe D. Carrillo Fishery Biologist US Fish & Wildlife Service California, USA ____________________________________________________________________________________ Be a better friend, newshound, and
Fernando Marmolejo-Ramos
2008-Jul-23 06:24 UTC
[R] adding the mean and standard deviation to boxplots
Dear users This is a message I was directing to Harold Baize but because I pressed the wrong button the message got lost grrrr!!! So I?m doing it all over again: Lets suppose I have three batches of data: a <- rnorm(50,2500,300) b <- rnorm(50,3500,250) c <- rnorm(50,4000,200) # Now I want to plot them as boxplots and violin plots require(vioplot) vioplot (a,b,c, horizontal=T, col=?white?) boxplot (a,b,c, horizontal=T, col=?white?) As we know boxplot show the least-greates values, lower-upper quartiles, the mean, and outliers (when present). However, for some data is not important the MEDIAN but the MEAN. Also, it is more relevant to show ERROR BARS instead of quartiles. So, how could I see (for the batches of data I introduced above)? 1. a boxplot showing the MEAN and the SD instead of the lower/upper quartile? 2. a boxplot showing the MEAN and the STANDARD ERROR OF THE MEAN instead of the lower/upper quartile? 3. a boxplot showing the MEAN and the 95% CI instead of the lower/upper quartile? (I think in all these cases is preferable to have visual access, or to have the line that shows, the LEAST and the GREATEST VALUES.) In other words, that the ERROR BARS (95% CI, SD, SE) proposed here take the place of the boxes usually used to represent the lower/upper quartile. Now, the big question, is all this jazz possible to be implemented in violin plots as well? How could that be done? Cheers, Fernando -- View this message in context: http://www.nabble.com/adding-the-mean-and-standard-deviation-to-boxplots-tp15271398p18604571.html Sent from the R help mailing list archive at Nabble.com.
This thread may help http://www.nabble.com/adding-the-mean-and-standard-deviation-to-boxplots-td15271398.html --- On Wed, 7/23/08, Fernando Marmolejo-Ramos <fernando.marmolejoramos at adelaide.edu.au> wrote:> From: Fernando Marmolejo-Ramos <fernando.marmolejoramos at adelaide.edu.au> > Subject: Re: [R] adding the mean and standard deviation to boxplots > To: r-help at r-project.org > Received: Wednesday, July 23, 2008, 2:24 AM > Dear users > > This is a message I was directing to Harold Baize but > because I pressed the > wrong button the message got lost grrrr!!! > > So I?m doing it all over again: > > Lets suppose I have three batches of data: > > a <- rnorm(50,2500,300) > b <- rnorm(50,3500,250) > c <- rnorm(50,4000,200) > > # Now I want to plot them as boxplots and violin plots > require(vioplot) > vioplot (a,b,c, horizontal=T, col=?white?) > boxplot (a,b,c, horizontal=T, col=?white?) > > As we know boxplot show the least-greates values, > lower-upper quartiles, the > mean, and outliers (when present). > > However, for some data is not important the MEDIAN but the > MEAN. Also, it is > more relevant to show ERROR BARS instead of quartiles. > > So, how could I see (for the batches of data I introduced > above)? > > 1. a boxplot showing the MEAN and the SD instead of the > lower/upper > quartile? > 2. a boxplot showing the MEAN and the STANDARD ERROR OF THE > MEAN instead of > the lower/upper quartile? > 3. a boxplot showing the MEAN and the 95% CI instead of the > lower/upper > quartile? > > (I think in all these cases is preferable to have visual > access, or to have > the line that shows, the LEAST and the GREATEST VALUES.) > > In other words, that the ERROR BARS (95% CI, SD, SE) > proposed here take the > place of the boxes usually used to represent the > lower/upper quartile. > > Now, the big question, is all this jazz possible to be > implemented in violin > plots as well? > > How could that be done? > > Cheers, > > Fernando > -- > View this message in context: > http://www.nabble.com/adding-the-mean-and-standard-deviation-to-boxplots-tp15271398p18604571.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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.__________________________________________________________________ Instant Messaging, free SMS, sharing photos and more... Try the new Yahoo! Canada Messenger at http://ca.beta.messenger.yahoo.com/