Book Image

R Data Visualization Cookbook

Book Image

R Data Visualization Cookbook

Overview of this book

Table of Contents (17 chapters)
R Data Visualization Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Generating a violin plot


Violin plots are very similar to box plots. It is hard to glorify one over the other. I observe that box plots look a little more cluttered when they are plotted along with outliers.

In the following violin plot, we can observe the mean, which is displayed using white dots, and the dispersion of various variables:

Getting ready

We need to install and load the ISLR and vioplot packages using the following lines of code:

install.packages(c("ISLR","vioplot"))
library(ISLR)
library(vioplot)

We require the ISLR package to download the baseball dataset in R. The vioplot package is utilized to generate the violin plot.

How to do it…

The violin plot is constructed using the vioplot() function:

vioplot(Hitters$AtBat,Hitters$Hits,Hitters$HmRun, horizontal = TRUE, col =c("green"), names = c("At Bat"," Hits", "Home runs"))

The violin plot can be constructed using the vioplot() function. The first argument in the function is the vector of data. In this recipe, we would like to plot the...