Tons of answers for dodging points and boxplots at the same time but what if I want different shaped points by a 3rd factor?
data<-cbind(expand.grid(trt=factor(1:4), block=factor(1:2), year=factor(c(2013, 2022))), value=runif(512))
I have:
ggplot(data, aes(x = trt, y = value, color = year)) +
geom_boxplot(outlier.size = 0,size=3,alpha=0.7) +
geom_point(alpha=0.4, position = position_jitterdodge(dodge.width = 1),
size=3,aes(shape=factor(block)))
And
geom_boxplot(position = position_dodge2(0.85, preserve = "single"), outlier.size=0, size=3,alpha=0.7) +
geom_jitter(position = position_dodge2(0.85,preserve = "single"),aes(shape=factor(block)),size=3,alpha=0.4)
Both of which produce roughly the same thing:
How do I make this, but the points from each "year" are displayed dodged to the same side that their respective year is dodged in the boxplots? As it stands, ggplot is putting all of block 1 with 2013 every time instead of just those block 1 points that actually also are in year==2013.
You can use the group
aesthetic
ggplot(data, aes(x = trt, y = value, color = year)) +
geom_boxplot(outlier.size = 0, alpha = 0.7, size = 2) +
geom_point(alpha = 0.4, position = position_jitterdodge(jitter.width = 0.8),
size = 3, aes(shape = factor(block), group = year))