Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I draw a violin plot using ggplot2?

Can I use ggplot2 to produce a violin plot? Perhaps using some variation of geom_boxplot()?

like image 729
Sam Avatar asked Mar 03 '11 12:03

Sam


People also ask

How do you make a violin plot in R?

How do you make a violin plot in R? To make a violin plot in R you can use ggplot2 and the geom_violin() function. For example, if we have the dataframe dataF and want to create a violin plot of the two groups response times you can use the following code: <code>p <- ggplot(aes(Group, RT), data = dataF))</code>.

What Geom might I use to add points to the violin plot?

The function stat_summary() can be used to add mean/median points and more on a violin plot.

Is violin plot better than Boxplot?

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. The difference is particularly useful when the data distribution is multimodal (more than one peak).


Video Answer


2 Answers

Version 0.9.0 includes the geom_violin: http://docs.ggplot2.org/current/geom_violin.html

like image 104
Ben Avatar answered Sep 18 '22 18:09

Ben


A quick googling returns this site, which uses geom_ribbon to draw violin plots for Figure 3.14.

Note to anyone catching up

As @Ben points out below, geom_violin() is now the preferred method for producing violin plots in ggplot2.

like image 29
Ari B. Friedman Avatar answered Sep 17 '22 18:09

Ari B. Friedman