[R] superpose violin and boxplot in ggplot

Ben Bolker bbolker at gmail.com
Wed Sep 11 15:45:55 CEST 2013


PIKAL Petr <petr.pikal <at> precheza.cz> writes:

> 
> Dear all
> 
> I am struggling a bit with tricky violinplot. I found how to superpose
boxplots correctly to violinplots.
> 
> p<-ggplot(Cars93, aes(x=Origin, y=Price, fill=Type, colour=Type))

 [snip]
> 
> How I could change colour of boxplots to single colour 
> but get the boxes at the correct locations e.g. over violins.
> 
> Thanks
> Petr
> 
> 

Use the group() aesthetic:

p+geom_violin()+ geom_boxplot(aes(fill=NULL,group=interaction(Type,Origin)),
   position=position_dodge(width=.9), width=.5, colour="black")

By the way, it would be useful to remind us that Cars93 is available in
the MASS package -- I had to hunt around a little bit.



More information about the R-help mailing list