Crafting simple violin plots
To visualize data and its distribution format, violin plots may be the way to go. Some data scientists love them, others hate. Still they play a role of major importance in data visualization and it's good to know how to brew them and the whole utility belt that comes along. There are alternatives though, as we are going to discuss later.
Again, until first half of 2017, both ggvis
and plotly
R's libraries does not dispose of a dedicated functions and types to draw violin plots, thus making relatively difficult to obtain such a viz using those packages. ggplot2
has a fully dedicated function, geom_violin()
. Current recipe is teaching how to use this function. Make sure to have car
package installed.
How to do it...
Let's get started with the recipe:
- Draw the base aesthetics under an object:
> library(ggplot2) > base <-ggplot(car::Salaries, aes(x = rank, y = salary))
- Call
geom_violin()
to draw a simple violin plot:
> base + geom_violin()
Following...