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))”!