I have a set of data to study the response to a treatment in both sexes. Either treatment or control vehicle is administered and the variable of interest measured at time 0 and 30 days for every individual:
| Sex | Time | Group | ID | Variable |
|---|---|---|---|---|
| M | t0 | control | ID01 | 87.4468574183993 |
| M | t0 | treatment | ID02 | 61.6036264109425 |
| M | t0 | control | ID03 | 87.1487529366277 |
| M | t0 | treatment | ID04 | 50.6166256731376 |
| M | t0 | control | ID05 | 72.9583341162652 |
| M | t0 | treatment | ID06 | 56.7137802368961 |
| M | t0 | control | ID07 | 65.8371140481904 |
| M | t0 | treatment | ID08 | 86.0453418688849 |
| M | t0 | control | ID09 | 83.5156044340692 |
| M | t0 | treatment | ID10 | 68.5201310901903 |
| M | t30 | control | ID01 | 84.504537214525 |
| M | t30 | treatment | ID02 | 56.6733720363118 |
| M | t30 | control | ID03 | 91.0738771315664 |
| M | t30 | treatment | ID04 | 94.9689620872959 |
| M | t30 | control | ID05 | 76.23775213724 |
| M | t30 | treatment | ID06 | 74.8982135788538 |
| M | t30 | control | ID07 | 80.9279862442054 |
| M | t30 | treatment | ID08 | 51.5291749034077 |
| M | t30 | control | ID09 | 85.7785020256415 |
| M | t30 | treatment | ID10 | 51.461190963164 |
| F | t0 | control | ID11 | 52.1936888922937 |
| F | t0 | treatment | ID12 | 91.9786257203668 |
| F | t0 | control | ID13 | 83.2809177343734 |
| F | t0 | treatment | ID14 | 71.7776856268756 |
| F | t0 | control | ID15 | 89.8885271861218 |
| F | t0 | treatment | ID16 | 84.3541337642819 |
| F | t0 | control | ID17 | 87.5198906287551 |
| F | t0 | treatment | ID18 | 94.8092532227747 |
| F | t0 | control | ID19 | 56.6507609561086 |
| F | t0 | treatment | ID20 | 72.2115040174685 |
| F | t30 | control | ID11 | 53.5416435450315 |
| F | t30 | treatment | ID12 | 61.9461603346281 |
| F | t30 | control | ID13 | 63.9171704184264 |
| F | t30 | treatment | ID14 | 73.8425903371535 |
| F | t30 | control | ID15 | 77.3780003190041 |
| F | t30 | treatment | ID16 | 80.7108032982796 |
| F | t30 | control | ID17 | 82.9249111004174 |
| F | t30 | treatment | ID18 | 72.6573116029613 |
| F | t30 | control | ID19 | 88.3334786165506 |
| F | t30 | treatment | ID20 | 50.722946121823 |
I managed to generate a suitable nested plot:
ggplot(mydata, aes(x = Sex, y = Variable, fill = Time)) + facet_grid(. ~ Group) + geom_boxplot(alpha = 0.80,outlier.colour = NA) + geom_point(aes(fill = Time), size 2, shape = 21, position = position_jitterdodge(0))
But I would like to include lines joining the dots belonging to a same individual, something like this (I added the black lines as an example using a software for image edition)
I tried:
ggplot(mydata, aes(x = Sex, y = Variable, fill = Time)) + facet_grid(. ~ Group) + geom_boxplot(alpha = 0.80,outlier.colour = NA) + geom_point(aes(fill = Time), size = 2, shape = 21, position = position_jitterdodge(0))+ geom_line(aes(group=interaction(ID,Sex)), size=0.3, alpha=0.8)
But the lines were not correctly drawn
I also looked at this solution: How to add lines connecting dots in boxplot in R?
But I would not like to add more panels to my plot. Thanks a lot for any help.



