ggplot 中的分组点彼此相邻

Grouped points in ggplot next to each other

我正在尝试使用 ggplot2 重新创建以下图:

但我无法将这些点分成不同的组。我得到的是类似这样的东西:

library(ggplot2)

mpg %>%  ggplot(aes(class, displ)) +
  geom_jitter(aes(fill=drv), width=.09, shape =21)

如何做到这一点?

我认为您正在寻找 position_jitterdodge,这是 geom_point

的一个参数
library(ggplot2)


mpg |>  ggplot(aes(class, displ)) +
  geom_point(aes(fill=drv), position = position_jitterdodge(jitter.width = 0.05, jitter.height = 0.5, dodge.width = 0.4), shape =21)

reprex package (v2.0.1)

于 2022-02-11 创建