# adding the mean and standard deviation to boxplots Classic List Threaded 8 messages Open this post in threaded view
|

## adding the mean and standard deviation to boxplots

 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]] ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: 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.htmlOn Feb 4, 2008 10:41 AM, Tom Cohen <[hidden email]> 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]] > > > ______________________________________________ > [hidden email] 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. > > ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: adding the mean and standard deviation to boxplots

 In reply to this post by Tom Cohen-2 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@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: adding the mean and standard deviation to boxplots

 In reply to this post by Tom Cohen-2 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 ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: adding the mean and standard deviation to boxplots

 In reply to this post by HBaize 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
Open this post in threaded view
|

## Re: adding the mean and standard deviation to boxplots

 ?bxp This is the underlying routine called by boxplot and you can supply your own values to the 5 that define a boxplot. On Wed, Jul 23, 2008 at 2:24 AM, Fernando Marmolejo-Ramos <[hidden email]> wrote: > > 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. > > ______________________________________________ > [hidden email] 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. > -- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve? ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.