Skip to content
Prev 327628 / 398502 Next

split beanplot in ggplot2 - adjusting bandwidth

Dear R Users,

I'm attempting to create a split beanplot in ggplot2 and have figured most of it out except how to adjust the bandwidth for the density statistic.  I read online that geom_violin will not plot 2 separate sets of data and that geom_ribbon should be used to create a split beanplot.  So, I was able to create this as a quick example that does a split beanplot of mpg for 6-cylinder vehicle vs 8 cylinder vehicles:

p <- ggplot(mtcars)
p + geom_ribbon(data=subset(mtcars,mtcars$cyl==6),
                aes(x=mpg,ymax=..density..,ymin=0),stat="density") +
            geom_ribbon(data=subset(mtcars,mtcars$cyl==8),
              aes(x=mpg,ymax=0,ymin=-..density..),stat="density")

What I can't figure out is how to adjust the bandwidth (e.g. as with 'adjust' in geom_density) for the density statistic in the ribbon plot to make the density plots either more or less smooth.  Or, is there a better way to go about this than what I've currently tried?

Any advice you might have on this would be greatly appreciated!  Thank you for your help!

cheers,
Andy