• 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 Violinplot with jittered data points in R

datavizpyr · July 3, 2022 ·

In this post, we will learn how to make grouped violin plot with jittered data points on top of it using ggplot2 in R. One of the challenges in making grouped violin or box plots with jittered data points is proper alignment of data points and the underlying plot. In an earlier tutorial, we addressed the alignment problem while making grouped boxplots with jittered data points. Although the same approach does not solve the problem fully for violinplot. In this tutorial, we will show how geom_sina() function ggforce will have add jittered data points to grouped violin plots.

How to add jittered data points to grouped violinplot
How to add jittered data points to grouped violinplot

library(tidyverse)
library(palmerpenguins)
theme_set(theme_bw(16))

Grouped Violin plot in ggplot2

In ggplot2, we can make violin plots using geom_violin(). TO make a grouped violin plot, we use either fill or color argument to aes() function with the column/variable that we want to group by.

penguins %>%
  drop_na() %>%
  ggplot(aes(x=species, y=body_mass_g, fill=sex))+
  geom_violin()
ggsave("grouped_violin_plot_ggplot2_R.png")
How to make Grouped Violin plot in R with ggplot
How to make Grouped Violin plot in R with ggplot

Grouped Violin plot in ggplot2 with jittered data points using geom_jitter()

To add jittered data points to the gouped violin plot, if we use geom_jitter(), it will not work, just like it does not for boxplot.

penguins %>%
  drop_na() %>%
  ggplot(aes(x=species, y=body_mass_g, fill=sex))+
  geom_violin()+
  geom_jitter(width=0.5)
ggsave("grouped_violin_plot_with_data_geom_jitter_ggplot2_R.png")

jittered points will not align nicely with the grouped violin plots.

Grouped Violin plot with jittered data points using geom_jitter()
Grouped Violin plot with jittered data points using geom_jitter()

Grouped Violin plot in ggplot2 with jittered data points using geom_points() with position_jitterdodge()

One of the solutions to align the jittered data points and violin plot is to use geom_point() with position_jitterdodge().

penguins %>%
  drop_na() %>%
  ggplot(aes(x=species, y=body_mass_g, fill=sex))+
  geom_violin()+
  geom_point(position=position_jitterdodge())
ggsave("grouped_violin_plot_with_jittered_points_ggplot2_R.png")

As the documentation page for position_jitterdodge() says, it is mainly design for “aligning points generated through geom_point() with dodged boxplots”. For dodged or grouped violin plots, we can see that position_jitterdodge() does not completely work.

Grouped violin plot with jittered points using geom_point() position
Grouped violin plot with jittered points using geom_point() position

Grouped Violin Plot with jittered data using geom_sina() from ggforce

A better option to make grouped violin plot with jittered data points is to use geom_sina() from ggforce R package on top of violin plot. The biggest advantage we get is that the data points by geom_sina() follows the shape of violin plot, spreading out where there are more points.

penguins %>% 
  drop_na() %>%
  ggplot(aes(x=species, y=body_mass_g, fill=sex))+
  geom_violin()+
  geom_sina(alpha=0.5)
ggsave("grouped_violin_plot_with_jittered_points_geom_sina_ggforce.png")

Another advantage is that jittered data points is aligned well with the groups.

Grouped violin plot with jittered points using geom_sina() in ggforce
Grouped violin plot with jittered points using geom_sina() in ggforce

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: geom_sina in ggforce, ggplot2, R Tagged With: Grouped violin plot with data points, Grouped violin plot with geom_sina()

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