How can I draw boxplots with mean values in R? 2

How can I draw boxplots with mean values in R?

Boxplots are a useful tool for visually representing the distribution of numerical data. In R, boxplots can be created using the “boxplot” function. However, to include mean values on the boxplots, additional steps need to be taken. This can be achieved by calculating the mean values separately using the “mean” function and then adding them to the boxplot using the “points” or “text” functions. Alternatively, the “boxplot.stats” function can be used to automatically calculate and add the mean values to the boxplot. Overall, by following these steps, one can effectively create boxplots with mean values in R.

Draw Boxplots with Mean Values in R (With Examples)


You can use the following methods to draw a boxplot with a mean value in R:

Method 1: Use Base R

#create boxplots
boxplot(df$values~df$group)

#calculate mean value by group
means <- tapply(df$values, df$group, mean)

#add means as circles to each boxplot
points(means, pch=20) 

Method 2: Use ggplot2

library(ggplot2)

#create boxplots with mean values shown as circles
ggplot(df, aes(x=group, y=values, fill=group)) +
  geom_boxplot() +
  stat_summary(fun=mean, geom='point', shape=20)

The following examples show how to use each method in practice with the following data frame in R:

#create data frame
df <- data.frame(team=rep(c('A', 'B', 'C'), each=5),
                 points=c(4, 4, 5, 6, 8, 7, 6, 8, 9, 12,
                          11, 12, 13, 16, 18))

#view first six rows of data frame
head(df)

  team points
1    A      4
2    A      4
3    A      5
4    A      6
5    A      8
6    B      7

Example 1: Create Boxplots with Mean Values in Base R

The following code shows how to create boxplots with mean values in base R:

#create boxplots
boxplot(df$points~df$team)

#calculate mean value by group
means <- tapply(df$points, df$team, mean)

#add means as circles to each boxplot
points(means, pch=20, cex=1.5)

The black lines inside each boxplot represent the median value and the black circles inside each boxplot represent the mean value.

Note: Change the value for the cex argument to change the circle size.

Example 2: Create Boxplots with Mean Values in ggplot2

The following code shows how to create boxplots with mean values in ggplot2:

library(ggplot2)

#create boxplots with mean values
ggplot(df, aes(x=team, y=points, fill=team)) +
  geom_boxplot() +
  stat_summary(fun=mean, geom='point', shape=20, size=8) +
  theme(legend.position='none')

boxplot with mean value in R

Note: Change the value for the size argument within the stat_summary() function to change the circle size.

Additional Resources

The following tutorials offer additional information about boxplots:

Cite this article

stats writer (2024). How can I draw boxplots with mean values in R?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-draw-boxplots-with-mean-values-in-r/

stats writer. "How can I draw boxplots with mean values in R?." PSYCHOLOGICAL SCALES, 28 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-draw-boxplots-with-mean-values-in-r/.

stats writer. "How can I draw boxplots with mean values in R?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-draw-boxplots-with-mean-values-in-r/.

stats writer (2024) 'How can I draw boxplots with mean values in R?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-draw-boxplots-with-mean-values-in-r/.

[1] stats writer, "How can I draw boxplots with mean values in R?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can I draw boxplots with mean values in R?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top