[R] adding the mean and standard deviation to boxplots

HBaize HBaize at buttecounty.net
Mon Feb 4 23:38:50 CET 2008


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.



More information about the R-help mailing list