How To Make Half Violinplot with ggplot2 in R?
Last Updated :
13 Jun, 2023
Half Violin plots are basically used to visualize the distribution and the overall summary of the data at the same time. They are also known as Raincloud plots. A combination of half violin plots with jittered points on top, boxplots and can be further enhanced by adding central measures of tendency, quartile ranges, etc. Using this plot we can acquire insights about the density, key summary statistics, and overall range of the data.
In this article let’s check out how to plot a Half Violin Plot using ggplot2 Package in the R programming language.
Install and Load the required packages:
Let’s install and load ggplot2 and see packages.
R
install.packages ( "ggplot2" )
install.packages ( "see" )
library (ggplot2)
library (see)
|
Load the dataset:
Let’s load an in-built dataset called diamonds.
Output:
carat cut color clarity depth table price x y z
<dbl> <ord> <ord> <ord> <dbl> <dbl> <int> <dbl> <dbl> <dbl>
1 0.23 Ideal E SI2 61.5 55 326 3.95 3.98 2.43
2 0.21 Premium E SI1 59.8 61 326 3.89 3.84 2.31
3 0.23 Good E VS1 56.9 65 327 4.05 4.07 2.31
4 0.29 Premium I VS2 62.4 58 334 4.2 4.23 2.63
5 0.31 Good J SI2 63.3 58 335 4.34 4.35 2.75
6 0.24 Very Good J VVS2 62.8 57 336 3.94 3.96 2.48
Plotting a Half Violin Plot using ggplot2
Example 1: Simple Half Violin plot
Let’s plot a Half Violin plot for the cut vs x of diamonds dataset.
R
ggplot (df, aes (cut , x, fill = cut)) +
geom_violinhalf () +
theme (legend.position = "none" )
|
Output:
Half Violinplot with ggplot2 in R
Example 2: Horizontal Half Violin Plot
Let’s check out to align the Half violin plot horizontally using the coord_flip() function.
R
ggplot (df, aes (cut, x, fill = cut)) +
geom_violinhalf () + coord_flip () +
theme (legend.position = "none" )
|
Output:
Half Violinplot with ggplot2 in R
Example 3: Horizontal Half Violin Plot with color filled by cut
Let’s check out to plot a Half violin plot horizontally and fill color by column cut.
R
ggplot (df, aes (cut,x, color=cut)) +
geom_violinhalf () + coord_flip ()+
theme (legend.position = "none" )
|
Output:
Half Violinplot with ggplot2 in R
Example 4: Horizontal Half Violin Plot with jittered data points by the side
Let’s check out to plot a Half violin plot along with jittered points.
R
ggplot (df, aes (cut, x, fill = cut)) +
geom_violinhalf (position = position_nudge (x = .2, y = 0)) +
geom_jitter (alpha = 0.01, width = 0.15) +
theme (legend.position = "none" )
|
Output:
Half Violinplot with ggplot2 in R
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...