I have plotted a boxplot+points. I want to add colors to the points. The position_jitterdodge
worked fine without color as shown in Figure B, the points are close, which is I intended to do. But when I try to add colors to the points, the jitter.width
parameter doesn't work any more (Figure A). The points are too far apart. I tried different numbers for jitter.width
, not working. How do I solve this problem?
library(tidyverse)
library(ggpubr)
mtcars$cyl <- factor(mtcars$cyl)
p1 <- mtcars %>% ggplot(aes(x = cyl, y = mpg, fill = cyl)) +
geom_boxplot() +
geom_point(position = position_jitterdodge(jitter.width = 0.2),
aes(color = factor(wt)), show.legend = FALSE)
p2 <- mtcars %>% ggplot(aes(x = cyl, y = mpg, fill = cyl)) +
geom_boxplot() +
geom_point(position = position_jitterdodge(jitter.width = 0.2))
ggarrange(p1, p2, labels = c("A", "B"))