• 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 Make Grouped Boxplot with Jittered Data Points in ggplot2

datavizpyr · February 5, 2021 ·

Grouped boxplots help visualize three variables in comparison to two variables with a simple boxplot. In this post we will see how to make a grouped boxplot with jittered data points with position_jitterdodge() using ggplot2 in R.

We can make grouped boxplot without datapoints easily by using the third “grouping” variable either for color or fill argument inside aes(). However, when we try to add the layer of jittered data points on the grouped boxplot using geom_jitter(), the plot will not look good. This post shows how can we get a better grouped boxplot with jittered data points using geom_point() with position_jitterdodge() as argument.

Step by Step Guide to Make Grouped Boxlplot with jittered points
Step by Step Guide to Make Grouped Boxlplot with jittered points

Let u load tidyverse and palmer penguin dataset to make the grouped boxplot.

library(tidyverse)
library(palmerpenguins)

How To Make Grouped Boxplot with ggplot2?

As said earlier, we can easily make a grouped boxplot in ggplot2 using geom_boxplot() and specifying the third variable as color or fill argument. In this example, we group the boxplots by the variable sex and use color option.

penguins %>%
  drop_na()%>%
  ggplot(aes(x=species,y=body_mass_g, color=sex)) +
  geom_boxplot(width=.5)
Grouped Boxplot with ggplot2
Grouped Boxplot with ggplot2

How To Make Grouped Boxplot with data points using geom_jitter()?

Naively, we might try to add jittered data points to the grouped boxplot using geom_jitter() function after geom_boxplot() function. geom_jitter() function is a handy shortcut for geom_point(position=”jitter”).

penguins %>%
  drop_na()%>%
  ggplot(aes(x=species,y=body_mass_g, color=sex)) +
  geom_boxplot(outlier.shape=NA)+
  geom_jitter(width=0.15)

However, this makes a grouped boxplot with overlapping boxes and data points from grouping variable.

Grouped Boxplot with geom_jitter() in ggplot2
Grouped Boxplot with geom_jitter() in ggplot2

How To Make Grouped Boxplot with jittered data points ggplot2?

To make a better grouped boxplot with jittered data points, we can use geom_point() after geom_boxplot(). However, we use position argument, position_jitterdodge(), inside geom_point() function.

penguins %>%
  drop_na()%>%
  ggplot(aes(x=species,y=body_mass_g, color=sex)) +
  geom_boxplot(outlier.shape=NA)+
  geom_point(position=position_jitterdodge())

And we get a nice looking grouped boxplot with clearly separated boxes and jittered data within each box.

Grouped Boxplot with jittered points using position_jitterdodge()
Grouped Boxplot with jittered points in ggplot2

Note that we used outlier.shape=NA inside geom_boxpot() to avoid showing outlier data point two times.

Related posts:

Customizing Mean mark to boxplot with ggplot2How To Show Mean Value in Boxplots with ggplot2? Scatterplot with marginal multi-histogram with ggExtraHow To Make Scatterplot with Marginal Histograms in R? Sinaplot and ViolinplotSinaplot vs Violin plot: Why Sinaplot is better than Violinplot Visualizing Missing Data with Barplot in R ggplot2Visualizing Missing Data with Barplot in R

Filed Under: ggplot2, grouped boxplot, grouped boxplot with jittered data Tagged With: ggplot2 position_jitterdodge(), grouped boxplot with jittered data

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