Grouped points in ggplot next to each other

Viewed 181

I am trying to recreat the following plot using ggplot2: enter image description here

But I am not able to separate the points into the different groups. What I get something similar to this:

library(ggplot2)

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

enter image description here

How can this be done?

1 Answers

I think you are looking for position_jitterdodge which is an argument of 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)

Created on 2022-02-11 by the reprex package (v2.0.1)

Related