• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Data Viz with Python and R

Learn to Make Plots in Python and R

  • Home
  • Seaborn
  • Matplotlib
  • ggplot2
  • Altair
  • About
    • Privacy Policy
  • Visualizing Activation Functions in Neural Networks
  • Confusion Matrix Calculator
  • Visualizing Dropout Rate in Neural Network
  • Visualizing Loss Functions in Neural Networks
  • Show Search
Hide Search

How To Show Mean Value in Boxplots with ggplot2?

datavizpyr · June 10, 2020 ·

Last updated on September 25, 2020

Boxplots are a great way to visualize multiple distributions using summary statistics. Boxplot shows five summary statistics; the minimum, the maximum, the median, and the first and third quartiles of the data. Sometimes, you might want to add other statistical summary values on the boxplot.

In this post, we will see how to show mean value on a boxplot made with ggplot2 in R. We will use gapminder data make boxplots and add mean values to each box in the boxplot.

Let us load tidyverse suite of packages.

library(tidyverse)
library(gapminder)

Let us first make a simple boxplot using gapminder data with continent on x-axis and lifeExp values on y-axis.

gapminder %>%
  ggplot(aes(continent,lifeExp, fill=continent)) +
  geom_boxplot() +
  theme(legend.position = "none")

We get a simple boxplot with colors filled by continent variable.

Simple Boxplot with ggplot2
Simple Boxplot with ggplot2

Add Mean Values to Boxplot with stat_summary()

Let us add mean values of lifeExp for each continent in the boxplot. In ggplot2, we can use stat_summary() function to cmpute new summary statistics and add it to the plot. In this example, we compute mean value of y-axis using fun.y argument in stat_summary() function.

gapminder %>%
  ggplot(aes(continent,lifeExp, fill=continent)) +
  geom_boxplot() +
  stat_summary(fun.y="mean")+
  theme(legend.position = "none")

We get a boxplot with black filled circle showing the mean values of lifeExp in each box.

Add Mean mark to boxplot with ggplot2
Add Mean mark to boxplot with
ggplot2

Customize Mean Values to Boxplot in ggplot2

We can change color of the mean mark and customize further using multiple options within stat_summary() function. In this example, we change the color and change the shape for the symbol used to show mean values.

gapminder %>%
  ggplot(aes(continent,lifeExp, fill=continent)) +
  geom_boxplot() +
  stat_summary(fun.y="mean", color="white", shape=15)+
  theme(legend.position = "none")

We can see that, now we get boxplot with mean values highlighted by white squares in each box.

Customizing  Mean mark to boxplot with ggplot2
Customizing Mean mark to boxplot with ggplot2

Explore the Complete ggplot2 Guide

35+ tutorials with code: scatterplots, boxplots, themes, annotations, facets, and more—tested and beginner-friendly.

Visit the ggplot2 Hub → No fluff—just code and visuals.

Related posts:

Customizing Labels on Bars in Side by side Stacked BarplotHow To Add Labels to Grouped Barplot with Bars Side-By-Side in R? ggplot2 legend on top of a plotHow To Adjust Legend Position in ggplot2? Visualizing Missing Data with Barplot in R ggplot2Visualizing Missing Data with Barplot in R Annotate Clusters with Ellipse with Labels ggforceHow To Annotate Clusters with Circle/Ellipse by a Variable in R

Filed Under: ggplot2, mean in boxplot, R Tagged With: Boxplot, ggplot2, R

Primary Sidebar

Python & R Viz Hubs

  • Seaborn Guide & Cookbook
  • ggplot2 Guide & Cookbook
  • Matplotlib Guide & Cookbook
  • Confusion Matrix Calculator
  • Visualizing Activation Functions
  • Visualizing Dropout
  • Visualizing Loss Functions

Buy Me a Coffee

Copyright © 2026 · Daily Dish Pro on Genesis Framework · WordPress · Log in

Go to mobile version