单因素箱线图上的抖动和闪避点

Jitter and dodge points on boxplots with single factor

我想避开箱线图,但对于只有一个因素的因素我想 保持与其他箱线图相同的宽度。解决方案是在箱线图位置添加 `preserve="single"。

同时我想在各自的箱线图上添加抖动点。所以我需要抖动闪避。 position_jitterdodge() 做了我需要的,但我不确定如何躲避它们 适合单因素箱线图

set.seed(123)
df <- data.frame(x=rep(c("a","b","c"),each=20), y=rnorm(20*3), z=c(rep(c("A","B"), times=20),rep("A",20)))

ggplot(df, aes(x,y,col=z)) + geom_boxplot(position = position_dodge(0.85, preserve = "single")) + 
  geom_point(position=position_jitterdodge(0.05))

试试这个:

library(ggplot2)
set.seed(123)
df <- data.frame(x=rep(c("a","b","c"),each=20), y=rnorm(20*3), z=c(rep(c("A","B"), times=20),rep("A",20)))
#Code
ggplot(df, aes(x,y,col=z)) + 
  geom_boxplot(position = position_dodge2(0.85, preserve = "single")) + 
  geom_jitter(position = position_dodge2(0.85, preserve = "single"))

输出:

或者这样:

#Code 2
ggplot(df, aes(x,y,col=z)) + 
  geom_boxplot(position = position_dodge2(0.85, preserve = "single")) + 
  geom_point(position=position_jitterdodge(0.05))

输出: