How To Make Violinplot with Data Points in R?
In this article, we will discuss how to make violinplot with data points in the R programming language.
A violin plot is a compact display of a continuous distribution. The geom_violin() method in R is used to construct a violin plot in the working space which understands various aesthetic mappings, like alpha, color or fill.
To construct a regular violin plot simply call the geom_violin() function after the visualization.
Example: A regular violinplot
Adding data points to violinplot
The width of a dot corresponds to the bin width in the case of a dot plot. This is followed by the case where the dots are stacked, where each of the dots represents one observation. To add data points we use geom_dotplot() after creating a violinplot.
geom_dotplot(mapping = NULL,data = NULL,binwidth = NULL,binaxis = “x”,stackdir = “up”)
- mapping – Set of aesthetic mappings created by aes()
- data – The data to be displayed
- binaxis – The axis to bin along, “x” or “y”
- stackdir – Defines the direction in which the dots should be stacked
- dotsize – The diameter of the dots relative to binwidth
Example: Adding data points to violinplot
Adding jitters to the violin plot
The geom_plot() can be improved by plotting the violinplot with data points using random noise to the actual datapoints on the x-axis. These data points are referred to as the “jitters”. The geom_jitter() method in R is used to add a small amount of random variation to the location of each point.
Example: Adding jitters to the violin plot
Specifying jitter width in the violin plot
The transparency, as well as the width of the data points in the violin plot, can be improvised by specifying the arguments, width, and alpha in the geom_jitter method in R.
- alpha: fixes the transparency
- width: used to specify the width of the jitter points
Example: Specifying jitter width in the violin plot