• 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 Violinplot with Data Points in R?

datavizpyr · August 7, 2020 ·

Violinplots are a great alternative to boxplot, as it combines boxplot and density plot. In this tutorial, we will see examples of how to make violinplots with using ggplot2’s geom_violin() function. Violinplots are like boxplot for visualizing numerical distributions for multiple groups. In comparison to boxplot, Violin plot adds information about density of distributions to the plot. A grouped violin plot is great for visualizing multiple grouping variables.

Let us load tidyverse and set ggplot2 theme_bw() with base size 16.

library(tidyverse)
theme_set(theme_bw(16))

We will use Palmer penguin dataset to make grouped violinplot with ggplot2 in R. Let us load the data directly from github page.

p2data <- "https://raw.githubusercontent.com/datavizpyr/data/master/palmer_penguin_species.tsv"
penguins_df <- read_tsv(p2data)

Let us make a simple violinplot with fill colors using geom_violin() function in ggplot2.

penguins_df %>%
  ggplot(aes(x=species, 
             y=flipper_length_mm,
             fill=species))+
  geom_violin()
ggsave("Simple_Violinplot_ggplot2_R.png")

With fill argument, ggplot2 automatically fills the violinplot with colors for each value of the fill variable.

Violinplot with ggplot2 geom_violin()
Violinplot with ggplot2 geom_violin()

Adding data points to Violinplot with geom_point()

Let us add data points to the violin plot adding the geom_point() function as additional layer to the plot.

penguins_df %>%
  ggplot(aes(x=species, 
             y=flipper_length_mm,
             color=species))+
  geom_violin()+
  geom_point()+
  theme(legend.position = "none")
ggsave("Violinplot_with_datapoints_ggplot2_R.png")

Now our plot displays the actual data points overlayed on the violinplot. However, you can see that all data points are lined in the middle of the violin plot. This can be a n issue when many data points overlap on each other.

Violinplot with data points
Violinplot with data points

How to Add Jittered Data Points to Violinplot in ggplto2?

Let us improve our violinplot with data points using random noise to the actual datapoints on the x-axis. This idea is called adding “jitters”. And with ggplot2, we can add jittered data points using geom_jitter() function. In this example we have added geom_jitter() instead of geom_point() as an additional layer to geom_violin()

penguins_df %>%
  ggplot(aes(x=species, y=flipper_length_mm, fill=species))+
  geom_violin()+
  geom_jitter()+
  theme(legend.position = "none")
ggsave("Violinplot_with_datapoints_jittered_ggplot2_R.png")
Violinplot with jittered points with default width
Violinplot with jittered points with default width

How to Specify jitter width to Violinplot in ggplto2?

Violin plot with jittered data points using geom_jitter() looks okay. We can make it better by customizing the width of the jitter. Here we we use width=0.15 to make the width smaller and also specify transparency of data points with alpha=0.5 argument to geom_jitter().

penguins_df %>%
  ggplot(aes(x=species, y=flipper_length_mm, fill=species))+
  geom_violin()+
  geom_jitter(width=0.15, alpha=0.5)+
  theme(legend.position = "none")
ggsave("Violinplot_with_jittered_ggplot2_R.png")

Now we have a better looking violin plot with jittered data points.

Violinplot with jittered points
Violinplot with jittered points

Related posts:

Grouped Violin plot with ggplot2How To Make Grouped Violinplot with ggplot2 in R? How to Make Heatmap with ggplot2?How To Make Simple Heatmaps with ggplot2 in R? Stacked Barplots Side By Side with ggplot2 in RHow to Make Horizontal Stacked Barplots with ggplot2 in R? Scatter Plot R: Fill color by variableHow To Color Scatter Plot by Variable in R with ggplot2?

Filed Under: R, Violinplot with data points Tagged With: ggplot2, R, Violinplot

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