3

I have a patient data something like below. Each subject are measured at 4 different time points.

df <- data.frame(
    result = rnorm(48, 1,3),
    time = rep(c('t1', 't2', 't3', 't4'), 12  ),
    subject = rep(c(1:12), each=4),
    gender = rep (c('M', 'F'), 6, each=4) )

I made a boxplot with overlaying datapoints:

pd = position_jitterdodge(dodge.width = 0.75, jitter.width = 0.3)
df %>% 
    ggplot (aes(x= time, y=result, fill=gender))+
    geom_boxplot(alpha=0.2)+
    geom_point(aes(color = gender),position = pd)+
    scale_fill_brewer(palette = 'Set1')+
    scale_color_brewer(palette = 'Set1')

enter image description here

Now I need to add lines to link all patients (data points) along the time course.

df %>% 
    ggplot (aes(x= time, y=result, fill=gender))+
    geom_boxplot(alpha=0.2)+
    geom_point(aes(color = gender, group=subject),position = pd)+
    geom_line(aes(color=gender, group=subject), 
    position=pd, alpha=0.3)+
    scale_fill_brewer(palette = 'Set1')+
    scale_color_brewer(palette = 'Set1')

enter image description here

All the lines seems to 'disconnected from data points. How can I fix this problem? I have spent hours but could not find solution. Could anyone kindly help me with that? Thanks a lot.

zesla
  • 11,155
  • 16
  • 82
  • 147
  • The problem is something about how position interacts with points vs. lines. I assume the jitter is necessary, since it works if you take it out? Sorry I couldn't be more helpful . . . – divibisan Feb 23 '18 at 18:53
  • @Henrik I tried the method from the link. But the problem is I have a extra factor. I could not figure out how to make it work – zesla Feb 23 '18 at 19:01
  • [Combining geom_point and geom_line with position_jitterdodge in ggplot2 for two grouping factors](https://stackoverflow.com/questions/37020435/combining-geom-point-and-geom-line-with-position-jitterdodge-in-ggplot2-for-two) – Henrik Feb 23 '18 at 19:42

1 Answers1

5

Some days ago position_jitterdodge gained a seed argument in the development version. So,

devtools::install_github("tidyverse/ggplot2")
library(ggplot2)

and then

pd = ggplot2::position_jitterdodge(dodge.width = 0.75, jitter.width = 0.3, seed = 1)
df %>% 
  ggplot (aes(x= time, y=result, fill=gender))+
  geom_boxplot(alpha=0.2)+
  geom_point(aes(color = gender, group=subject),position = pd)+
  geom_line(aes(color=gender, group=subject), 
            position=pd, alpha=0.3)+
  scale_fill_brewer(palette = 'Set1')+
  scale_color_brewer(palette = 'Set1')

gives this plot

enter image description here

erocoar
  • 5,723
  • 3
  • 23
  • 45
  • sorry for a followup question. I notice there are always two point with grey color at the middle top and lower right respectively. Do you know why? how do I get ride of that? thank you! @erocoar – zesla Feb 23 '18 at 20:09
  • That should be fixed when you replace your boxplot argument with `geom_boxplot(alpha=0.2, outlier.shape = NA)` :) – erocoar Feb 23 '18 at 20:32
  • I found one problem with this. The points are not aligned with the same color box. Anyway to adjust that? – zesla Feb 23 '18 at 21:28
  • Yes, I see the problem now :/ It is that for the `geom_point` you need `group=gender`, whereas for the `geom_segment` you need `group=subject`. I think manual jittering will be the only way to go in that case, since the order of drawing (and so jitter) will be different for the two geoms – erocoar Feb 24 '18 at 12:43