R ggplot2 Violin Plot

The R ggplot2 Violin Plot is useful to graphically visualizing the numeric data group by specific data. Let us see how to Create a ggplot2 violin plot in R, Format its colors. And drawing horizontal violin plots, plot multiple violin plots using R ggplot2 with example. For this R ggplot Violin Plot demo, we use the diamonds data set provided by the R.

R ggplot2 Violin Plot Syntax

The syntax to draw a violin plot in R Programming is

geom_violin(mapping = NULL, data = NULL, stat = "ydensity",
  position = "dodge", ..., draw_quantiles = NULL, trim = TRUE,
  scale = "area", na.rm = FALSE, show.legend = NA, inherit.aes = TRUE)

Create a basic R ggplot2 Violin Plot

In this example, we show how to create a basic violin plot using the ggplot2 package. For this example, we are going to use the diamonds data set provided by the R Studio.

TIP: ggplot2 package not installed by default. Please refer Install R Packages article to understand installing the package.

# Create R ggplot Violin plot

# Importing the ggplot2 library
library(ggplot2)

# Create a Violin plot
ggplot(diamonds, aes(x = cut, y = price)) + 
  geom_violin()
R ggplot2 Violin Plot 1

Creating R ggplot2 Violin Plot

In our previous R ggplot violin plot example, data is huge, so there is no visibility of the proper violin plot. In this example, we scale y value with log10 and create a violin plot using the scaled y.

NOTE: If you require to import data from external files, then please refer to R Read CSV to understand importing the CSV file in R Programming.

# Create R ggplot Violin plot

# Importing the ggplot2 library
library(ggplot2)

# Create a Violin plot
ggplot(diamonds, aes(x = cut, y = price)) + 
  geom_violin() + scale_y_log10()
R ggplot2 Violin Plot 2

Change Colors of a ggplot Violin Plot in R

In this example, we show how to change the violin plot colors using fill argument

  • fill: Please specify the color you want to use for your violin plot. Type colors() in your console to get the list of colors available in R programming
# Change Colors of a R ggplot Violin plot

# Importing the ggplot2 library
library(ggplot2)

# Create a Violin plot
ggplot(diamonds, aes(x = cut, y = price)) + 
  geom_violin(fill = "seagreen") + scale_y_log10()
R ggplot2 Violin Plot 3

Change Colors of an R ggplot2 Violin Plot using Columns

This R ggplot violin plot example, we show how to change the violin plot colors using column data. Here, we are using the cut column data to differentiate the colors.

# Change Colors of a R ggplot Violin plot

# Importing the ggplot2 library
library(ggplot2)

# Create a Violin plot
ggplot(diamonds, aes(x = cut, y = price, fill = cut)) + 
  geom_violin() + scale_y_log10()
R ggplot2 Violin Plot 4

Trimming the R Violin Plot using Columns

Let us see how to trim, not to trim the data in the violin plot using trim argument

# Trimming R ggplot Violin plot

# Importing the ggplot2 library
library(ggplot2)

# Create a Violin plot
ggplot(diamonds, aes(x = clarity, y = price, fill = clarity)) + 
  geom_violin(trim = FALSE) + scale_y_log10())
R ggplot2 Violin Plot 5

Add Mean & Median to R ggplot2 Violin Plot

Let us see how to add Mean and Median value to the violin plot using stat_summary function.

# Adding Mean & Median to R ggplot Violin plot

# Importing the ggplot2 library
library(ggplot2)

# Create a Violin plot
ggplot(diamonds, aes(x = cut, y = price, fill = cut)) + 
  geom_violin() + scale_y_log10() +
  stat_summary(fun.y = "mean", geom = "point", shape = 8, size = 3, color = "midnightblue") +
  stat_summary(fun.y = "median", geom = "point", shape = 2, size = 3, color = "red")
R ggplot2 Violin Plot 6

Add Boxplot to R ggplot2 Violin Plot

In this example, we show how to add a boxplot to R Violin Plot using geom_boxplot function. It can help us to see the Median, along with the quartile for our violin plot.

TIP: Please refer R ggplot2 Boxplot article to understand the Boxplot arguments.

# Adding Boxplot to R ggplot Violin plot

# Importing the ggplot2 library
library(ggplot2)

# Create a Violin plot
ggplot(diamonds, aes(x = cut, y = price, fill = cut)) + 
  geom_violin() + scale_y_log10() +
  geom_boxplot(width = 0.2)
R ggplot2 Violin Plot 7

Alter Legend position of a R ggplot2 Violin Plot

By default, ggplot position the legend at the right side of a violin plot.

In this example, we change the legend position of the R ggplot violin plot from right to the top. Note, You can use a legend.position = “none” to altogether remove the legend.

# Alter legend position of a R ggplot Violin plot

# Importing the ggplot2 library
library(ggplot2)

# Create a Violin plot
ggplot(diamonds, aes(x = cut, y = price, fill = cut)) + 
  geom_violin() + scale_y_log10() +
  geom_boxplot(width = 0.2) +
  theme(legend.position = "top")
R ggplot2 Violin Plot 8

Horizontal ggplot Violin Plot in R

In this R ggplot violin plot example, we change the default vertical violin plot into a horizontal violin plot using coord_flip() function

# Change X, Y Axis in a R ggplot Violin plot

# Importing the ggplot2 library
library(ggplot2)

# Create a Violin plot
ggplot(diamonds, aes(x = cut, y = price, fill = cut)) + 
  geom_violin() + scale_y_log10() +
  geom_boxplot(width = 0.2) +
  coord_flip()
R ggplot2 Violin Plot 9

Multiple ggplot Violin plots in R

In this R ggplot violin plot example, we show how to group multiple violin plots

# Multiple R ggplot Violin plot

# Importing the ggplot2 library
library(ggplot2)

# Create a Violin plot
ggplot(diamonds, aes(x = cut, y = price, fill = clarity)) + 
  geom_violin() + scale_y_log10()
R ggplot2 Violin Plot 10

How to use Facets in R ggplot2 Violin plot

This R ggplot violin plot example, we draw multiple violin plot, by dividing the data based on column value. Here, we are using the clarity column data to divide the violin plots

# Multiple R ggplot Violin plot

# Importing the ggplot2 library
library(ggplot2)

# Create a Violin plot
ggplot(diamonds, aes(x = cut, y = price, fill = clarity)) + 
  geom_violin(trim= FALSE) + scale_y_log10() +
  facet_wrap(~ clarity)
R ggplot2 Violin Plot 11

By default, facet_wrap() assigns the same y-axis to all the violin plot. But, you can change it (giving independent axis) to each violin plot by adding one more attribute called scale. facet_wrap(~ clarity, scale = “free”).

Assigning names to ggplot2 Violin plot

In this example, we show you how to assign names to R ggplot violin plot, X-Axis, and Y-Axis using labs function

# Add Names to R ggplot Violin plot

# Importing the ggplot2 library
library(ggplot2)

# Create a Violin plot
ggplot(diamonds, aes(x = cut, y = price, fill = clarity)) + 
  geom_violin(trim= FALSE) + scale_y_log10() +
  facet_wrap(~ clarity) +
  labs(title="GGPLOT VIOLIN PLOT", x="Price in Dollars", y="Cut")
R ggplot2 Violin Plot 12

Change ggplot2 Violin plot Theme

In this example, we show how to change the default theme of an R ggplot violin plot.

  • theme_dark(): We use this function to change the violin plot default theme to dark. If you type theme_, then R Studio intelligence shows the list of available options. For example, theme_grey().
# Change theme of a R ggplot Violin plot

# Importing the ggplot2 library
library(ggplot2)

# Create a Violin plot
ggplot(diamonds, aes(x = cut, y = price, fill = cut)) + 
  geom_violin(trim = FALSE) + scale_y_log10() +
  geom_boxplot(width = 0.2) +
  labs(title="GGPLOT VIOLIN PLOT", x="Price in Dollars", y="Cut") +
  theme_dark()
R ggplot2 Violin Plot 13