Home > front end >  In R, how to make the jitter (geom_jitter()) stay inside its correspondant boxplot without extending
In R, how to make the jitter (geom_jitter()) stay inside its correspondant boxplot without extending

Time:12-03

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:

  • 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!

    CodePudding user response:

    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

    CodePudding user response:

    Specify the dodge width

    geom_jitter(width = 0.05)

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

  • Related