• 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
  • Python Viz
  • Seaborn
  • Altair
  • R Viz
  • ggplot2
  • About
    • Privacy Policy
  • Show Search
Hide Search

How To Show Mean Value in Boxplots with ggplot2?

datavizpyr · June 10, 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

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

Tags

Altair barplot Boxplot boxplot python boxplot with jiitered text labels Bubble Plot Color Palette Countplot Density Plot Facet Plot gganimate ggplot2 ggplot2 Boxplot ggplot2 error ggplot boxplot ggridges ggtext element_markdown() Grouped Barplot R heatmap heatmaps Histogram Histograms Horizontal boxplot Python lollipop plot Maps Matplotlib Pandas patchwork pheatmap Pyhon Python R RColorBrewer reorder boxplot ggplot Ridgeline plot Scatter Plot Scatter Plot Altair Seaborn Seaborn Boxplot Stock Price Over Time Stripplot UpSetR Violinplot Violin Plot World Map ggplot2

Buy Me a Coffee

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

Go to mobile version