geom_violin

570 views
Skip to first unread message

Winston Chang

unread,
Dec 5, 2011, 10:32:41 AM12/5/11
to ggplo...@googlegroups.com
Hi everyone - I have a rough working version of geom_violin. Comments and suggestions are appreciated.



Some examples -- hopefully these inline images will show up properly...

set.seed(110)
dat <- data.frame(x=LETTERS[1:3], y=round(rnorm(90),2))

# With points overlaid
ggplot(dat, aes(x=x, y=y)) + geom_violin() + geom_point(shape=21)
violin-1.png

# Narrower, no points
ggplot(dat, aes(x=x, y=y)) + geom_violin(width=.5)
violin-2.png

# Narrower, with tails
ggplot(dat, aes(x=x, y=y)) + geom_violin(width=.5, trim=FALSE) + geom_point(shape=21)
violin-3.png

# With box plot
ggplot(dat, aes(x=x, y=y)) + geom_violin(width=.5) + geom_boxplot(width=.1, outlier.size=0)
violin-4.png

# Dodging works
ggplot(dat, aes(x="foo", y=y, fill=x)) + geom_violin()
violin-5.png

violin-4.png
violin-1.png
violin-2.png
violin-5.png
violin-3.png

Dennis Murphy

unread,
Dec 5, 2011, 12:55:35 PM12/5/11
to ggplo...@googlegroups.com
Nice. An example with point jitter would be good, too; something like


ggplot(dat, aes(x=x, y=y)) + geom_violin() +
     geom_point(shape=21, position = position_jitter(width = 0.2))

That's a common problem in strip chart/boxplot combinations - it would be comforting to know that jittering works in geom_violin() too. I haven't tried it out yet, but I will when it gets merged into ggplot2. Just a wish list item :)

Dennis
violin-5.png
violin-3.png
violin-2.png
violin-1.png
violin-4.png

Winston Chang

unread,
Dec 5, 2011, 2:47:31 PM12/5/11
to ggplo...@googlegroups.com
Dennis, thanks for the feedback. Here's an example with jitter:

ggplot(dat, aes(x=x, y=y)) + geom_violin() + 
    geom_point(shape=21, position=position_jitter(height=0, width=.2))
violin-jitter.png

I think the next thing I'm going to do is implement a Wilkinson-style dot plot. I posted some code on the main ggplot2 list a little while back to make make this kind of graph, but it wasn't very elegant -- it required using a continuous x axis. Here's what it looks like using that hack:
violin-dotplot.png

I think this is way more informative than jittering. I just have to figure out a good way to do it. It's kind of complicated because of the way dot size is related to the binning and axis dimensions. With "regular" point graphs, the dot diameter is independent of the axis dimensions, but with a Wilkinson-style dot plot, the dot diameter determines the bin size.

I'm hoping that I can use geom_point with a new position_xx and/or stat_xx, but if not, I'll have to make a new geom.

-Winston
violin-jitter.png
violin-dotplot.png

Hadley Wickham

unread,
Dec 5, 2011, 8:47:54 PM12/5/11
to ggplo...@googlegroups.com
Looks very cool, thanks!
Hadley
--
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/
violin-1.png
violin-3.png
violin-2.png
violin-5.png
violin-4.png

Carey Duryea

unread,
Apr 17, 2013, 8:08:56 PM4/17/13
to ggplo...@googlegroups.com
so geom_violin is for compariing densities of distrobutions next to eachother? 
Reply all
Reply to author
Forward
0 new messages