R Programming

Violin plots with ggplot2 in R

We provide effective and economically affordable training courses for R and Python, Click here for more details and course registration !

Violin plot is quite similar as boxplot, in the sense that it shows the range of the data. And at the same time a violin plot illustrates the data distribution shape with a density curve on both sides of the central pillar. The follow image shows several typical violin plots of variable ‘price’ with respect to different values of variable ‘cut’.

Violin plots

Plotting violin plots with R can be conducted by applying geom_violin() function with ggplot() function from ggplot2 package. The basic form of the structure is

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

Where

x and y represent the variable for x-axis and y-axis, and fill and color are used for color options.

The following code example shows plotting a simple violin plot for variable ‘cut’ and ‘price’ from data frame ‘diamonds’ in ggplot2 package.

#load ggplot2 package
library(ggplot2)

# Basic violin plot
# diamonds dataframe being used here

ggplot(diamonds, aes(x=cut, y=price)) +
  geom_violin()
A simple violin plot

Next example show plotting violin plots by setting the contour color of the density curves, with respect to different values of ‘cut’.

#load ggplot2 package
library(ggplot2)

# Contour-colored violin plots
# diamonds dataframe being used here
ggplot(diamonds, aes(x=cut, y=price, color=cut)) +
  geom_violin()
Contour-colored violin plots

You can also fill the violin with different colors when plotting violin plots, see the following code example.

#load ggplot2 package
library(ggplot2)

# Color-filled violin plots
# diamonds dataframe being used here

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

Color-filled violin plots

We can also show mean value location for each violin plot by setting a red point in the violin.

#load ggplot2 package
library(ggplot2)

# violin plots with mean value points
# diamonds dataframe being used here

ggplot(diamonds, aes(x=cut, y=price)) +
  geom_violin()+
  stat_summary(fun.y="mean", geom="point", size=2, color="red")
Violin plots with mean value points

Violin plots can also be plotted horizontally.

#load ggplot2 package
library(ggplot2)

# violin plots show horizontally
# diamonds dataframe being used here

ggplot(diamonds, aes(x=cut, y=price)) +
  geom_violin()+
  coord_flip()
Horizontal violin plots

You can also watch full video of R tutorials from our YouTube channel.

wilsonzhang746

Recent Posts

Python Machine Learning Source Files

Click here to download Python Machine Learning Source Files !

2 weeks ago

Install PyTorch on Windows

PyTorch is a deep learning package for machine learning, or deep learning in particular for…

3 weeks ago

Topic Modeling using Latent Dirichlet Allocation with Python

Topic modeling is a subcategory of unsupervised machine learning method, and a clustering task in…

1 month ago

Document sentiment classification using bag-of-words in Python

For online Python training registration, click here ! Sentiment classification is a type of machine…

2 months ago

Download R Course source files

Click here to download R Course source files !

11 months ago

Download Python Course source files

Click here to download Python Course Source Files !

11 months ago