• 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

Grouped Boxplots with reproducible jittered data points

datavizpyr · December 29, 2021 ·

Last updated on December 31, 2021

In this post we will learn how to make a grouped boxplots with reproducible jittered points.

We can make a grouped boxplot with jittered points using position_jitterdodge() function as position argument to geom_point().

p0_1 <- penguins %>%
  drop_na() %>%
  ggplot(aes(x = species,
             y = bill_length_mm,
             color = sex))+
  geom_boxplot(outlier.shape = NA)+
  geom_point(position = position_jitterdodge())+
  theme(legend.position = "none")
print(p0_1)
ggsave("grouped_boxplot_with_jittered_points_ggplot2.png")

Note that using the handy wrapper function geom_jitter() instead of geom_point() with position_jitterdodge() will not give right plot with jittered data.

Grouped Boxplot with Jittered Data points
Grouped Boxplot with Jittered Data points

If we use the same code again to create a grouped boxplot, the resultiung plot will be slightly different due to the added randomness while jittering data points along the x-axis. Notice the differences in the data positions in the two boxplots below.
Grouped Boxplot with Irreproducible Jittered Points
Grouped Boxplot with Irreproducible Jittered Points

Grouped Boxplots with reproducible jittered data points

To make a grouped boxplot with reproducible jittered data points, we need to use seed argument inside ” position_jitterdodge()” function instead of position_jitter() function.

p1 <- penguins %>%
  drop_na() %>%
  ggplot(aes(x = species, 
             y = bill_length_mm,
             color=sex))+
  geom_boxplot(outlier.shape = NA)+
  geom_point(position = position_jitterdodge(seed = 42))+
  theme(legend.position = "none")

Let us make the same plot using the same seed but with different color palette

p2 <- penguins %>%
  drop_na() %>%
  ggplot(aes(x = species,
             y = bill_length_mm,
             color = sex))+
  geom_boxplot(outlier.shape = NA)+
  geom_point(position = position_jitterdodge(seed = 42))+
  theme(legend.position = "none")+
  scale_color_brewer(palette="Dark2")

We can see that we have successfully reproduced jittered data points in a grouped boxplot.

print(p1+p2+plot_annotation(
  title = 'Grouped Boxplot with reproducible jittered points',
  subtitle='using position_jitterdodge(seed=42)'))
ggsave("grouped_boxplot_with_reproducible_jittered_points_ggplot2.png",
       width=12, height=6)
Grouped Boxplots with Reproducible Jittered Data Points
Grouped Boxplots with Reproducible Jittered Data Points

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 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? ggforce geom_circle(): Annotate with a circleHow To Annotate a Plot with Circle in R Default ThumbnailHow to Make Axis Text Bold in ggplot2

Filed Under: ggplot2, R Tagged With: grouped boxplots with reproducible jittered points

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