# Re: [R] adding the mean and standard deviation to boxplots

From: HBaize <HBaize_at_buttecounty.net>
Date: Mon, 4 Feb 2008 14:38:50 -0800 (PST)

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.

______________________________________________
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.
```
Received on Mon 04 Feb 2008 - 22:42:34 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 23 Jul 2008 - 07:32:19 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.