How To Make Violin Plots with ggplot2 in R (original) (raw)

Last Updated : 30 Apr, 2026

Violin plots are used to visualize the distribution of numerical data across one or more categories. They are similar to box plots, but instead of only showing summary statistics, they also display the data’s probability density, providing a more detailed view of the distribution.

Syntax

ggplot( dataframe, aes( x, y, fill, color)) + geom_violin()

**Where:

1. Creating basic Violin Plots

We create a violin plot to show the distribution of diamond prices across different cut categories using the built-in diamonds dataset.

library(ggplot2)

ggplot(diamonds, aes(x=cut, y=price)) +

geom_violin()

`

**Output:

 Violin Plots with ggplot2 in RGeeksforgeeks

Violin Plots with ggplot2 in R

2. Color Customization (Outline)

We create a colored violin plot by mapping cut to the color aesthetic which changes the outline color of each violin based on the cut type.

ggplot(diamonds, aes(x=cut, y=price, color=cut)) +

geom_violin()

`

**Output:

 Violin Plots with ggplot2 in RGeeksforgeeks

Violin Plots with ggplot2 in R

3. Fill Customization (Interior)

We change the interior fill color of each violin by mapping cut to the fill aesthetic which colors the inside of the violin based on the cut type.

library(ggplot2)

ggplot(diamonds, aes(x=cut, y=price, fill=cut)) +

geom_violin()

`

**Output:

Violin Plots with ggplot2 in RGeeksforgeeks

Violin Plots with ggplot2 in R

4. Horizontal Violin Plot

To convert a normal violin plot to a horizontal violin plot we add the coord_flip() function to the ggplot() function. This flips the coordinate axis of the plot and converts any ggplot2 plot into a horizontal plot.

Syntax

plot+ coord_flip()

Here, is a horizontal violin plot made using the coord_flip() function.

R `

library(ggplot2)

ggplot(diamonds, aes(x=cut, y=price)) +

geom_violin()+

coord_flip()

`

**Output:

Violin Plots with ggplot2 in RGeeksforgeeks

Violin Plots with ggplot2 in R

5. Mean marker customization

In ggplot2, we use the stat_summary() function to compute new summary statistics and add them to the plot. We use the stat_summary() function with ggplot() function.

Syntax

plot+ stat_summary(fun.y, geom, size, color)

**Where:

**Example:

In this example, we will compute the mean value of the y-axis variable using fun.y argument in the stat_summary() function.

R `

library(ggplot2)

ggplot(diamonds, aes(x=cut, y=price)) +

geom_violin()+

stat_summary(fun=mean, geom="point", size=2, color="red")

`

**Output:

Violin Plots with ggplot2 in RGeeksforgeeks

Violin Plots with ggplot2 in R

Here, the point in the center of the violin shows the variation of the mean of the y-axis for each category of data on the x-axis.

6. Adding Box Plot in Violin Plot

We add a box plot inside the violin plot using the geom_boxplot() function to show summary statistics like median, quartiles and outliers.

library(ggplot2)

ggplot(diamonds, aes(x = cut, y = price)) +

geom_violin() +

geom_boxplot(width = 0.2, fill = "white", color = "black")

`

**Output:

Violin Plots with ggplot2 in RGeeksforgeeks

Violin Plots with ggplot2 in R

7. Customization of the Box Plot

We customize the box plot and violin plot by adding colors, transparency, labels and themes using built-in functions from ggplot2.

library(ggplot2) theme_set(theme_minimal())

ggplot(diamonds, aes(x = cut, y = price)) + geom_violin(fill = "#66C2A5", color = "#084B83", alpha = 0.8) + geom_boxplot(width = 0.2, fill = "#EFCB68", color = "#8F2D56", alpha = 0.8) +

labs(x = "Cut", y = "Price", title = "Violin Plot with Box Plot") +

theme(axis.text = element_text(size = 12, color = "#333333")) +

theme(plot.background = element_rect(fill = "#F7F7F7")) +

theme(legend.background = element_rect(fill = "#F7F7F7"), legend.position = "bottom") +

theme(plot.margin = margin(20, 20, 20, 20))

`

**Output:

Violin Plots with ggplot2 in RGeeksforgeeks

Violin Plots with ggplot2 in R