Not very good at blogging for sure. But here is a trick I just found for which I have been fighting for a long time: how to align boxplot and violin plots in ggplot2? Nothing really tricky a priori thanks to the great “geom” system of ggplot2, but problems start to arise when grouping factors are involve. geom_boxplot will “stack” boxes side by side within a group, while violin_plot will set them apart. I finally found out this is due to default “dodging” behaviors, which can be fixed as follow:

p <- ggplot(data = dat, aes(x=group1, y=value))
p <- p + geom_violin(aes(fill=group2), position = position_dodge(width = 0.8))
p <- p + geom_boxplot(notch = FALSE, alpha = 0.5, aes(colour = group2), position = position_dodge(width = 0.8), width = 0.5)
p <- p + theme_bw() + guides(color = "none")
p

Sorry, no picture… but if you came across that problem you’d know what I’m talking about. And the trick is “position = position_dodge(width = 0.8))”!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s