Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Median and quartile on violin plots in ggplot2

Tags:

I would like to draw some violin plots with ggplot2, but I noticed that median and first and third quartile are not automatically displayed. I believe these plots would be much more informative with this information. Does anybody know of a way to do it?

like image 701
Martombo Avatar asked Jun 26 '13 12:06

Martombo


People also ask

Do violin plots show mean or median?

A violin plot is more informative than a plain box plot. While a box plot only shows summary statistics such as mean/median and interquartile ranges, the violin plot shows the full distribution of the data.

What is geom_violin?

geom_violin.Rd. A violin plot is a compact display of a continuous distribution. It is a blend of geom_boxplot() and geom_density() : a violin plot is a mirrored density plot displayed in the same way as a boxplot.


2 Answers

One way to do this is to just put a thin box plot over the top of it. Here's an example with the iris data:

require(ggplot2) ggplot(iris,aes(Species,Sepal.Length))+geom_violin()+geom_boxplot(width=.1) 

enter image description here

like image 190
Sam Dickson Avatar answered Oct 01 '22 03:10

Sam Dickson


geom_violin has an argument draw_quantiles that allows you to specify which quantiles to include. Here is an example of 1st, 2nd, and 3rd quartiles on iris.

require(ggplot2) ggplot(iris, aes(Species, Sepal.Length)) + geom_violin(draw_quantiles = c(0.25, 0.5, 0.75)) 

enter image description here

like image 23
Justin Braaten Avatar answered Oct 01 '22 04:10

Justin Braaten