繁体   English   中英

在 R 中,如何使抖动(geom_jitter())保持在其对应的箱线图中而不延伸到相邻的箱线图中?

[英]In R, how to make the jitter (geom_jitter()) stay inside its correspondant boxplot without extending over the neighboring boxplots?

我想找到一种方法让抖动保持在自己的箱线图中,而不会延伸到相邻的箱线图上。

到目前为止,我看了这个答案:

但他们都没有真正解决我的问题; 主要区别在于我有 3 个组通过 X 轴上的时间轴运行。

我到目前为止的代码:

ggplot(longitudinal, mapping= aes(x = Time, y = Values), shape= Diagnose)+
geom_boxplot(aes(color = Diagnose), outlier.shape = NA ) +
geom_jitter(aes(color= Diagnose, shape=Diagnose)  ,alpha = 0.5)

图像 output: 在此处输入图像描述

如您所见,jitter 服从 Timepoint 分布(T0、T1、T2、T3),但是当涉及到诊断(Diagnose)时,它与其他框重叠。

这是我的数据的示例:

structure(list(Time = c("T0", "T0", "T0", "T0", "T0", "T0", "T0", 
"T0", "T0", "T1", "T1", "T1", "T1", "T1", "T1", "T1", "T1", "T2", 
"T2", "T2", "T2", "T2", "T2", "T2", "T2", "T2", "T3", "T3", "T3", 
"T3", "T3", "T3", "T3", "T3", "T3"), Diagnose = c("PDD", "PDD", 
"PDD", "PD-MCI", "PD-MCI", "PD-MCI", "PD", "PD", "PD", "PD", 
"PD", "PD-MCI", "PD-MCI", "PD-MCI", "PDD", "PDD", "PDD", "PD", 
"PD", "PD", "PD-MCI", "PD-MCI", "PD-MCI", "PDD", "PDD", "PDD", 
"PD", "PD", "PD", "PD-MCI", "PD-MCI", "PD-MCI", "PDD", "PDD", 
"PDD"), Values = c(13.47, 14.25, 15, 20, 19.57, 15, 15, 17.54, 
18, 16.93, 11.42, 18, 15, 19.48, 15, 11, 15, 18.03, 11, 15, 17.85, 
19, 15, 15, 17.85, 20, 15, 19, 14.11, 12, 18.31, 16, 17.36, 20, 
12)), row.names = c(NA, -35L), class = c("tbl_df", "tbl", "data.frame"
))

这是 output 使用 position = position_jitter(), position=position_jitterdodge(), position_dodge, position_jitterdodge(dodge.width= ) 等... 在此处输入图像描述 如您所见,这包含了中央箱线图中的所有抖动。

谢谢!

几乎! 您正在寻找的是geom_point(position = position_jitterdodge()) 您还可以使用jitter.width调整宽度

 ggplot(df, mapping= aes(x = Time, y = Values))+
  geom_boxplot(aes(color = Diagnose), outlier.shape = NA ) +
  geom_point(aes(color= Diagnose, shape=Diagnose), alpha = 0.5, 
                 position = position_jitterdodge(jitter.width = 0.1))

在此处输入图像描述

指定闪避宽度

+ geom_jitter(width = 0.05)

geom_point(position = position_jitter(width = 0.05))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM