Using stat_summary to customize violin plots
Think about some summarized information you want to display inside your violin. Maybe you want to show the median along with the interval given by plus and minus one standard deviation. Or maybe you want just the median to be displayed. That is possible and not difficult.
You can either create new a function that summarizes the information to be displayed into your violins or use an already existing function. This recipe teaches how that can be done and also how to draw quantiles inside your violins.
Getting ready
Make sure the Hmisc
and car
packages are already installed:
> if( !require(Hmisc)){ install.packages('Hmisc')} > if( !require(car)){ install.packages('car')}
To the drawing now!
How it works...
We can start using stat_summary
to customize violin plots as follows:
- Create a basic violin plot with quantiles:
> library(ggplot2) > violin <- ggplot(car::Salaries, aes(x = rank, y = salary)) + geom_violin(draw_quantiles = c(.25,.75...