画图给点分组?》这一篇文章中给出了方法,画出两层分组的点图,一层分组是按照离散型变量在x轴上分组,第二层是每一个分组里,再来一个离散型变量再分一次。非常感谢读者们的反馈,原来这第二层分组在ggplot2中是有内置的实现方式的,那就是position_jitterdodge

library(ggplot2)

d = subset(diamonds, color %in% c("D", "E"))
ggplot(d, aes(cut, carat, fill = color, color = color)) +
  geom_violin(alpha = .3) +
  geom_jitter(aes(group = color),
              position = position_jitterdodge(jitter.width = 0.5,
                                              dodge.width = 0.9),
              alpha = .3) +
  theme_bw() +
  scale_color_discrete()

代码跑出来的图是这样子的: