簡體   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