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, draw 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.

library(ggplot2)

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

Creating R ggplot2 Violin Plot

In our previous R ggplot 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 this chart using the scaled y.

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

library(ggplot2)

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 colors using fill argument

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

library(ggplot2)

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

Change Colors using Columns

This R ggplot 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.

library(ggplot2)

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 using trim argument

# Trimming 
library(ggplot2)

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

Add Mean & Median

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

# Adding Mean & Median 

library(ggplot2)

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 chart.

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

library(ggplot2)

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.

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.

library(ggplot2)

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 example, we change the default vertical into a horizontal using coord_flip() function

library(ggplot2)

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 example, we show how to group multiple violin plots

library(ggplot2)

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 example, we draw multiple violin plot, by dividing the data based on column value. Here, we are using the clarity column data to divide them.

library(ggplot2)

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 alt. But, you can change it (giving independent axis) to each one by adding one more attribute called scale. facet_wrap(~ clarity, scale = “free”).

Assigning names

In this example, we show you how to assign names to chart, X-Axis, and Y-Axis using labs function

library(ggplot2)

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 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 default theme to dark. If you type theme_, then R Studio intelligence shows the list of available options. For example, theme_grey().
library(ggplot2)

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