简体   繁体   English

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

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

I would like to find a way for the jitter to stay in its own boxplot, without extending over the neighboring boxplots.我想找到一种方法让抖动保持在自己的箱线图中,而不会延伸到相邻的箱线图上。

So far, I looked at this answers:到目前为止,我看了这个答案:

but none of them really addressed my issue;但他们都没有真正解决我的问题; the main difference is that I have 3 groups running through a timeline on the X-axis.主要区别在于我有 3 个组通过 X 轴上的时间轴运行。

The code I have so far:我到目前为止的代码:

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)

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

As you can see, the jitter obeys the Timepoint distribution (T0, T1, T2, T3), but when it comes to the diagnosis(Diagnose), it overlaps with the other boxes.如您所见,jitter 服从 Timepoint 分布(T0、T1、T2、T3),但是当涉及到诊断(Diagnose)时,它与其他框重叠。

Here is an example of how my data looks like:这是我的数据的示例:

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"
))

and this the output when using position = position_jitter(), position=position_jitterdodge(), position_dodge, position_jitterdodge(dodge.width= ) etc...这是 output 使用 position = position_jitter(), position=position_jitterdodge(), position_dodge, position_jitterdodge(dodge.width= ) 等... 在此处输入图像描述 As you can see, this packs all the jitter in the central boxplots.如您所见,这包含了中央箱线图中的所有抖动。

Thanks!谢谢!

Almost!几乎! what you are looking for is geom_point(position = position_jitterdodge()) .您正在寻找的是geom_point(position = position_jitterdodge()) You can also adjust the width with jitter.width您还可以使用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))

在此处输入图像描述

Specify the dodge width指定闪避宽度

+ geom_jitter(width = 0.05)

or geom_point(position = position_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