Search code examples
rggplot2graphcolors

ggplot with colored lines connecting paired values


I would like to creat a ggplot graph with the following data:

`patient    condition   essai   10MWT
1   sans    1   9,57
1   sans    2   9,52
1   sans    3   8,92
1   avec    1   9,73
1   avec    2   9,38
1   avec    3   9,3
2   sans    1   19,41
2   sans    2   17,04
2   sans    3   17,61
2   avec    1   15,79
2   avec    2   16,58
2   avec    3   16,83
3   sans    1   6,99
3   sans    2   8,07
3   sans    3   7,91
3   avec    1   7,54
3   avec    2   7,65
3   avec    3   8,42
4   sans    1   8,75
4   sans    2   8,29
4   sans    3   8,18
4   avec    1   7,96
4   avec    2   8,09
4   avec    3   8,34
5   sans    1   9,36
5   sans    2   8,91
5   sans    3   8,88
5   avec    1   8,24
5   avec    2   7,57
5   avec    3   7,56
6   sans    1   7,53
6   sans    2   8,15
6   sans    3   7,52
6   avec    1   7,39
6   avec    2   7,34
6   avec    3   6,85
`

I wrote the following commands but I don't succeed in connecting the paires values (10MWT) for 1 same patient and the same try (essai 1 or 2) and the different conditions.

ggplot(data3, aes(factor(condition), value)) +
  geom_boxplot(width=0.3, size=1, fatten=1, colour="black") +
  geom_point( size=2, alpha=0.5, col= data3$patient) +
  geom_line(aes(x=factor(condition), y=value), colour= data3$patient)

Can anyone help me? Best regards


Solution

  • To connect your points with lines you have to map on the group aesthetic to tell ggplot2 which points should be treated as separate groups. As you have multiple observations per patients corresponding to different values of essai the relevant grouping is by patient and essai.

    As a side note, when you want to color by a variable then do so by mapping on aesthetics inside aes().

    library(ggplot2)
    
    ggplot(data3, aes(factor(condition), X10MWT)) +
      geom_boxplot(
        width = 0.3, size = 1, fatten = 1, colour = "black"
      ) +
      geom_point(
        aes(color = factor(patient)),
        size = 2, alpha = 0.5
      ) +
      geom_line(aes(
        color = factor(patient),
        group = interaction(patient, essai)
      ))
    

    DATA

    data3 <- structure(list(patient = c(
      1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2,
      3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 6, 6, 6,
      6, 6, 6
    ), condition = c(
      "sans", "sans", "sans", "avec", "avec",
      "avec", "sans", "sans", "sans", "avec", "avec", "avec", "sans",
      "sans", "sans", "avec", "avec", "avec", "sans", "sans", "sans",
      "avec", "avec", "avec", "sans", "sans", "sans", "avec", "avec",
      "avec", "sans", "sans", "sans", "avec", "avec", "avec"
    ), essai = c(
      1,
      2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1,
      2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3
    ), X10MWT = c(
      9.57, 9.52,
      8.92, 9.73, 9.38, 9.3, 19.41, 17.04, 17.61, 15.79, 16.58, 16.83,
      6.99, 8.07, 7.91, 7.54, 7.65, 8.42, 8.75, 8.29, 8.18, 7.96, 8.09,
      8.34, 9.36, 8.91, 8.88, 8.24, 7.57, 7.56, 7.53, 8.15, 7.52, 7.39,
      7.34, 6.85
    )), class = "data.frame", row.names = c(NA, -36L))