Home > Software design >  Splitting a ggplot2 jitter stripchart by group left and right
Splitting a ggplot2 jitter stripchart by group left and right

Time:09-20

I made a stripchart plot using ggplot2:

Using this code:

p.Insulinefast2 <- ggplot(data, aes(x=Visites, y=Insulinefast, color=cluster, shape=cluster))   geom_jitter()
p.Insulinefast2   stat_summary(fun.data=data_summary, color="blue")   theme(legend.position="right")   scale_color_brewer(palette="Dark2")   theme_classic()

The colors and shapes differentiate the groups of participants (cluster). For reading purposes, I would like all participants of group 1 (green) to appear left of the blue center of the plot, and participants in group 2 (orange) to be on the right (for visit V2 and for visit V4).

Is there a way to achieve this using ggplot2 ?

Here is my data:

data <- structure(list(Visites = c("V2", "V4", "V2", "V4", "V2", "V4", 
"V2", "V4", "V2", "V4", "V2", "V4", "V2", "V4", "V2", "V4", "V2", 
"V4", "V2", "V4", "V2", "V4", "V2", "V4", "V2", "V4", "V2", "V4", 
"V2", "V4", "V2", "V4", "V2", "V4", "V2", "V4", "V2", "V4", "V2", 
"V4", "V2", "V4", "V2", "V4", "V2", "V4", "V2", "V4"), Insulinefast = c(68.5, 
89, 121, 111, 193, 181, 43.5, 67, 46, 57.5, 180.5, 170.5, 113, 
90, 60, 66.5, 88, 51.5, 187, 156, 56.5, 54, 66.5, 56, 82.5, 72.5, 
72, 85, 48.5, 61, 66.5, 36.5, 126.5, 132, 67, 53.5, 56.5, 64.5, 
35, NA, 36, 47.5, 54, 30, 47, 51.5, 72, 72), cluster = c("respc1", 
"respc1", "respc1", "respc1", "respc1", "respc1", "respc1", "respc1", 
"respc1", "respc1", "respc1", "respc1", "respc2", "respc2", "respc1", 
"respc1", "respc2", "respc2", "respc1", "respc1", "respc1", "respc1", 
"respc1", "respc1", "respc1", "respc1", "respc1", "respc1", "respc1", 
"respc1", "respc2", "respc2", "respc2", "respc2", "respc2", "respc2", 
"respc1", "respc1", "respc1", "respc1", "respc1", "respc1", "respc1", 
"respc1", "respc1", "respc1", "respc1", "respc1")), class = "data.frame", row.names = c(NA, 
-48L))

This is what I'm looking for:

Thank you

CodePudding user response:

One option would be to switch to position_jitterdodge:

Note: Instead of your data_summary which you did not provide I switched to mean_cl_boot in stat_summary.

library(ggplot2)

ggplot(data, aes(x = Visites, y = Insulinefast, color = cluster, shape = cluster))  
  geom_point(position = position_jitterdodge())  
  stat_summary(fun.data = mean_cl_boot, color = "blue")  
  theme(legend.position = "right")  
  scale_color_brewer(palette = "Dark2")  
  theme_classic()

enter image description here

  • Related