1

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.

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: enter image description here

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.

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 ... enter image description here As you can see, this packs all the jitter in the central boxplots.

Thanks!

Pauliinaa
  • 61
  • 7

2 Answers2

2

Almost! what you are looking for is geom_point(position = position_jitterdodge()). You can also adjust the width with 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))

enter image description here

Cecilia López
  • 347
  • 1
  • 9
0

Specify the dodge width

+ geom_jitter(width = 0.05)

or geom_point(position = position_jitter(width = 0.05))

geom_na
  • 258
  • 2
  • 10
  • This doesn't work. I updated my question to make it clearer. The problem with this is that it packs all the jitter in the middle. And what I am looking for is: the jitter from each diagnosis to stay inside its corresponding boxplot. – Pauliinaa Dec 02 '21 at 09:30