[R] Violin Plot - using ggplot2

ONKELINX, Thierry Thierry.ONKELINX at inbo.be
Wed Mar 30 13:21:25 CEST 2011


Dear JP,

Please do not cross post between lists.

Here is a possible solution.

library(ggplot2)
p <- rep(c(rep("condition_a", 4), rep("condition_b", 4)), 2)
q <- c(rep("grp_1", 8), rep("grp_2", 8))
r <- sample(1:5, 16, rep = T)
d <- data.frame(p, q, r)
ggplot(d, aes(x = r)) + geom_ribbon(aes(ymax = ..density.., ymin = -..density..), stat = "density") + facet_grid(q ~ p) + coord_flip()
ggplot(d, aes(x = r, fill = p)) + geom_ribbon(alpha = 0.2, aes(ymax = ..density.., ymin = -..density..), stat = "density") + facet_wrap( ~ q) + coord_flip()

Best regards,

Thierry

----------------------------------------------------------------------------
ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek
team Biometrie & Kwaliteitszorg
Gaverstraat 4
9500 Geraardsbergen
Belgium

Research Institute for Nature and Forest
team Biometrics & Quality Assurance
Gaverstraat 4
9500 Geraardsbergen
Belgium

tel. + 32 54/436 185
Thierry.Onkelinx op inbo.be
www.inbo.be

To call in the statistician after the experiment is done may be no more than asking him to perform a post-mortem examination: he may be able to say what the experiment died of.
~ Sir Ronald Aylmer Fisher

The plural of anecdote is not data.
~ Roger Brinner

The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data.
~ John Tukey
  

> -----Oorspronkelijk bericht-----
> Van: r-help-bounces op r-project.org 
> [mailto:r-help-bounces op r-project.org] Namens JP
> Verzonden: woensdag 30 maart 2011 11:26
> Aan: r-help op r-project.org
> Onderwerp: [R] Violin Plot - using ggplot2
> 
> Hi there,
> 
> I am trying to create a violin plot with ggplot2 (which looks awesome
> by the way).  I am probably not understanding the code correctly by
> this is what I am trying (this works if you want to copy and paste it
> in R)
> 
> library(ggplot2)
> p <- rep(c(rep("condition_a", 4), rep("condition_b", 4)), 2)
> q <- c(rep("grp_1", 8), rep("grp_2", 8))
> r <- sample(1:5, 16, rep = T)
> d <- data.frame(p, q, r)
> gg1 <- ggplot(d, aes(y=r, x=factor(p)))
> gg2 <- gg1 + geom_ribbon(aes(ymax = ..density.., ymin = -..density..),
> stat = "density") + facet_grid(q ~ ., as.table = F, scales = "free_y")
> + labs(x = "some x desc", y = "some y desc")
> print(gg2)
> 
> The result I am getting is posted as an image at
> http://awesomescreenshot.com/0e8ae8c52  (I tried to draw what I would
> like to achieve.)
> 
> What I would like to achieve instead is the equivalent of 
> this using ggplot2
> 
> library(lattice)
> bwplot( r ~ p | q,
>            data=d,
>            panel = panel.violin
> )
> 
> What I want is to draw traditional violin plots grouping them by
> grp_1, grp_2 etc (so q above).  For each group I have two conditions
> (p above) which I want to plot violin plots for their values. I want
> the plots to be vertical (as drawn in red in my image). Any ideas on
> how to change the above ggplot2 code to do just that?  I have been
> playing with  facet_grid all day... Also the values on the axis are
> incorrect ... they should be in the range 1:5 (as r above) and a plot
> should show the probability density of that value...
> 
> Any help?  I am going mad...
> 
> Many Thanks
> JP
> 
> ______________________________________________
> R-help op r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide 
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
> 


More information about the R-help mailing list