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

Download R Course source files

Click here to download R Course source files !

2 months ago

Download Python Course source files

Click here to download Python Course Source Files !

2 months ago

How to create a data frame from nested dictionary with Pandas in Python

For online Python training registration, click here ! Pandas provides flexible ways of generating data…

5 months ago

How to delete columns of a data frame in Python

For online Python training registration, click here ! Data frame is the tabular data object…

5 months ago

Using isin() to check membership of a data frame in Python

Click her for course registration ! When a data frame in Python is created via…

5 months ago

How to assign values to Pandas data frame in Python

We provide affordable online training course(via ZOOM meeting) for Python and R programming at fundamental…

5 months ago