Home > Enterprise >  How can we jitter points in ggplot
How can we jitter points in ggplot

Time:07-30

I have a data as below:

data<-data.frame(ID=c("Study1","Study2","Study3", 
                      "Study1","Study2","Study3"),
                 avg=c(2,3,3.5,
                        2.5, 3.3, 4.0),
                 lower = c(1.8, 2.7, 3.15, 2.25, 2.97, 3.6),
                 upper = c(2.2, 3.3, 3.85, 2.75, 3.63, 4.4),
                 gender = c("m","m","m","f","f","f"))

The graph I want to plot is to visualize the average values of male and female within each studying group.

p <- ggplot(data = df, aes(x = ID, y = avg, colour = ID))  
  geom_point(size = 4, alpha = 0.6, position=position_jitter(width=0.5, seed = 50))  
  geom_linerange(aes(ymin = lower, ymax = upper),
               position = position_jitter(width = 0.5, seed = 50)) 
p

Fig1 enter image description here

I realized that the scatters of those points are very different. So, I decided to use position_dodge just to check the position of points.

pp <- ggplot(data = df, aes(x = ID, y = avg, colour = ID))  
  geom_point(position = position_dodge(width = 0.2))
pp

Fig2 enter image description here

This time, the points do not even move.

For fig3, I set colour = ID to show you how I imagine the scatter of points.

Fig3 enter image description here

In my mind, I would like to jitter points looks like Fig3 (points that represent sexes within each studying group are distributed evenly).

Do you guys have any solutions for this case? Thank you in advance!!!

CodePudding user response:

I think you need to tell ggplot to group your data by gender (in your mapping = aes() call). Otherwise it doesn't know to dodge them on that basis. By grouping correctly, you may not need to use position_jitter() you can just use position_dodge().

library(tidyverse)

df <- data.frame(ID=c("Study1","Study2","Study3", 
                      "Study1","Study2","Study3"),
                 avg=c(2,3,3.5,
                       2.5, 3.3, 4.0),
                 lower = c(1.8, 2.7, 3.15, 2.25, 2.97, 3.6),
                 upper = c(2.2, 3.3, 3.85, 2.75, 3.63, 4.4),
                 gender = c("m","m","m","f","f","f")) 


p <- ggplot(data = df, aes(x = ID, y = avg, colour = ID, 
                           shape = gender, group = gender))  
  geom_point(size = 4, alpha = 0.6, position=position_dodge(width=0.2))  
  geom_linerange(aes(ymin = lower, ymax = upper),
                 position = position_dodge(width = 0.2)) 
p

Incidentally you can use geom_pointrange() if you like rather than using geom_point() and geom_linerange() separately.

Created on 2022-07-29 by the reprex package (v2.0.1)

  • Related