2

So I would like to get a plot with two categories on the x-axis, then the color of the lines and data points defined by one variable, and the shape of the points defined by the second variable

basically connect the same dots with a line

effectBaseline <- ggplot(
    data = bdataPEV, 
    aes(x=variable, y=value, group=Electrode, color=subject)) +
geom_line()+
geom_jitter(aes(x=variable,
        y = value,
        shape = Electrode,
        color=subject),
    size=2,
    show.legend=TRUE,
    width = 0.1)+
labs(x = "",
    y="PEV (dps)",
title = "Effect of baseline",
subtitle= "PEV values at UCL and T") +
theme_classic() +
theme(axis.title=element_text(size=8,face="bold"),
    axis.text.x = element_text(face="bold", size=8, angle=0),
    axis.text.y = element_text(face="bold",size=10, angle=90),
    panel.grid.major.x = element_blank(),
    panel.grid.minor.x = element_blank()) +
geom_hline(yintercept = 0)+
scale_x_discrete(labels=c("no Baseline","with Baseline"))

Example data:

    structure(list(subject = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 
4L, 4L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L), .Label = c("1", 
"2", "3", "6"), class = "factor"), Electrode = c("LAN", "SAN", 
"PAN", "LAN", "SAN", "PAN", "LAN", "SAN", "PAN", "LAN", "SAN", 
"PAN", "LAN", "SAN", "PAN", "LAN", "SAN", "PAN", "LAN", "SAN", 
"PAN", "LAN", "SAN", "PAN", "LAN", "SAN", "PAN", "LAN", "SAN", 
"PAN", "LAN", "SAN", "PAN", "LAN", "SAN", "PAN"), variable = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L), .Label = c("PEVsum_block_noB", "PEVsum_block_B", 
"PEVsum_block_B_norm", "PEVsum_block_B_inhibition", "PEVsum_blok_B_inhibition_norm"
), class = "factor"), value = c(26.5655048141819, 24.25, 4.30277563773199, 
158.352853610442, 100.585046948683, 26.0372066865141, 147.516666666667, 
156.7275, 65.9128571428571, 1.7378211394883, 1.9853826151828, 
2.96232650874689, 4.24264068711928, 14, 1, 141.354165131417, 
90.2108640907513, 5, 130.96, 137.72, 23.32, 2.4099999998795, 
1.86214499960808, 2.96192610446308, -2.82842712474619, 0, -1, 
-15.0332963783729, -9.4339811320566, -9.05538513813742, -31.13, 
-28.57, -30.27, 0, 0, -0.679999999932)), row.names = c(1L, 2L, 
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 
17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 37L, 38L, 39L, 40L, 41L, 
42L, 43L, 44L, 45L, 46L, 47L, 48L), class = "data.frame")

tjebo
  • 21,977
  • 7
  • 58
  • 94
Stan
  • 23
  • 3
  • Do you want a line to join the three points on the graph? – Mhairi McNeill Jan 31 '23 at 09:04
  • For each electrode/subject combination I would like a line, thanks! – Stan Jan 31 '23 at 09:54
  • If you want to connect pair of dots, then this thread will help you. (also consider a scatter plot formatted data, as suggested in the thread) https://stackoverflow.com/a/75248172/7941188 – tjebo Jan 31 '23 at 10:44
  • and https://stackoverflow.com/questions/44656299/lines-connecting-jittered-points-dodging-by-multiple-groups – tjebo Jan 31 '23 at 10:45

1 Answers1

0

So, I've changed two things in your plot:

  1. I used interaction to group the lines by both the subject and electrode.
  2. I set both lines and points to have the same position adjustment. For more see this question here: How to jitter both geom_line and geom_point by the same magnitude?
pos <- position_dodge(width = 0.1)

ggplot(data = bdataPEV,
       aes(
           x = variable,
           y = value,
           color = subject,
           shape = Electrode,
           group = interaction(subject, Electrode)
       )) +
    geom_line(position = pos) +
    geom_point(
        size = 2,
        show.legend = TRUE,
        position = pos
    ) +
    labs(
        x = "",
        y = "PEV (dps)",
        title = "Effect of baseline",
        subtitle = "PEV values at UCL and T"
    ) +
    theme_classic() +
    theme(
        axis.title = element_text(size = 8, face = "bold"),
        axis.text.x = element_text(face = "bold", size = 8, angle = 0),
        axis.text.y = element_text(face = "bold", size = 10, angle = 90),
        panel.grid.major.x = element_blank(),
        panel.grid.minor.x = element_blank()
    ) +
    geom_hline(yintercept = 0) +
    scale_x_discrete(labels = c("no Baseline", "with Baseline"))

enter image description here

Mhairi McNeill
  • 1,951
  • 11
  • 20
  • Let me know if that's what you were looking for - I can edit my answer. – Mhairi McNeill Feb 01 '23 at 10:23
  • Many thanks! That look nice. Now I just realize that I would only like to connect the 'no Baseline' dots with the corresponding positive 'with baseline' dots. But then I probably need to adjust the data frame right? – Stan Feb 06 '23 at 11:11
  • Yeah, you will need to change the data. There are multiple points for each subject/electrode combo at 'with Baseline'. I don't know if that makes sense for your data or not! – Mhairi McNeill Feb 07 '23 at 10:56