Re: [R] Violin Plot - using ggplot2

From: JP <jeanpaul.ebejer_at_inhibox.com>
Date: Wed, 30 Mar 2011 12:42:24 +0100

Sorry for the cross post - but I will post your answer in the other place too for completeness

And thanks for your answer... that is brilliant especially the second transparent plot

Fantastic

On 30 March 2011 12:21, ONKELINX, Thierry <Thierry.ONKELINX_at_inbo.be> wrote:

> 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_at_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_at_r-project.org
> > [mailto:r-help-bounces_at_r-project.org] Namens JP
> > Verzonden: woensdag 30 maart 2011 11:26
> > Aan: r-help_at_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_at_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.
> >

-- 

Jean-Paul Ebejer
Early Stage Researcher

InhibOx Ltd
Pembroke House
36-37 Pembroke Street
Oxford
OX1 1BP
UK

(+44 / 0) 1865 262 034



This email and any files transmitted with it are confide...{{dropped:22}}

______________________________________________
R-help_at_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.
Received on Wed 30 Mar 2011 - 11:52:22 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 30 Mar 2011 - 12:00:25 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive