Home > Software design >  Line connecting dots of same ID in nested ggplot
Line connecting dots of same ID in nested ggplot

Time:06-28

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))

the first plot I obtained

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)

the plot I want

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

the second plot I obtained

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.

CodePudding user response:

One option to achieve your desired result would be to manually do the dodging for the geom_line by computing the x positions like so:

library(ggplot2)

width <- .75

mydata$x_line <- as.numeric(factor(mydata$Sex))   
  ifelse(mydata$Time == "t0", -1, 1) * width / 4

ggplot(mydata, aes(x = Sex, y = Variable, fill = Time))  
  facet_grid(. ~ Group, scales = "free_x")  
  geom_boxplot(alpha = 0.80, outlier.colour = NA, position = position_dodge(width))  
  geom_point(aes(group = Time), size = 2, shape = 21, position = position_dodge(width))  
  geom_line(aes(x = x_line, group = ID), size = 0.3, alpha = 0.8)

DATA

mydata <- data.frame(
  Sex = c(
    "M", "M", "M", "M", "M", "M",
    "M", "M", "M", "M", "M", "M", "M", "M", "M", "M", "M", "M",
    "M", "M", "F", "F", "F", "F", "F", "F", "F", "F", "F",
    "F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "F"
  ),
  Time = c(
    "t0", "t0", "t0", "t0", "t0",
    "t0", "t0", "t0", "t0", "t0", "t30", "t30", "t30", "t30",
    "t30", "t30", "t30", "t30", "t30", "t30", "t0", "t0",
    "t0", "t0", "t0", "t0", "t0", "t0", "t0", "t0", "t30", "t30",
    "t30", "t30", "t30", "t30", "t30", "t30", "t30", "t30"
  ),
  Group = c(
    "control", "treatment",
    "control", "treatment", "control", "treatment", "control",
    "treatment", "control", "treatment", "control", "treatment",
    "control", "treatment", "control", "treatment", "control",
    "treatment", "control", "treatment", "control",
    "treatment", "control", "treatment", "control", "treatment",
    "control", "treatment", "control", "treatment", "control",
    "treatment", "control", "treatment", "control", "treatment",
    "control", "treatment", "control", "treatment"
  ),
  ID = c(
    "ID01", "ID02", "ID03", "ID04",
    "ID05", "ID06", "ID07", "ID08", "ID09", "ID10", "ID01",
    "ID02", "ID03", "ID04", "ID05", "ID06", "ID07", "ID08",
    "ID09", "ID10", "ID11", "ID12", "ID13", "ID14", "ID15",
    "ID16", "ID17", "ID18", "ID19", "ID20", "ID11", "ID12",
    "ID13", "ID14", "ID15", "ID16", "ID17", "ID18", "ID19",
    "ID20"
  ),
  Variable = c(
    87.4468574183993,
    61.6036264109425, 87.1487529366277, 50.6166256731376, 72.9583341162652,
    56.7137802368961, 65.8371140481904, 86.0453418688849,
    83.5156044340692, 68.5201310901903, 84.504537214525,
    56.6733720363118, 91.0738771315664, 94.9689620872959,
    76.23775213724, 74.8982135788538, 80.9279862442054,
    51.5291749034077, 85.7785020256415, 51.461190963164, 52.1936888922937,
    91.9786257203668, 83.2809177343734, 71.7776856268756,
    89.8885271861218, 84.3541337642819, 87.5198906287551,
    94.8092532227747, 56.6507609561086, 72.2115040174685,
    53.5416435450315, 61.9461603346281, 63.9171704184264,
    73.8425903371535, 77.3780003190041, 80.7108032982796,
    82.9249111004174, 72.6573116029613, 88.3334786165506, 50.722946121823
  )
)
  • Related