How to pair points within group using ggplot?

Hi everyone! I am new to R and really need some help. I have a dataset with two groups - Experimental and Control. Each participant contributes two responses per group, which represent different learning styles. These are represented in the box plots with jitter below. I would like to connect each participant's two responses together with lines using ggplot (so each red line in the Control group would correspond to each turquoise line in the control group), however I can't figure out how to do this within the conditions. Can someone please help?

Then, I need to change the color of the lines within the conditions to black if Increase = TRUE and red if Increase = FALSE.

d <- data.frame (
  Subject = c("1", "2", "3", "4"),
  Group  = c("Exp", "Exp", "Control", "Control"),
  Tr = c("14", "11", "4", "23"),
  Sr = c("56", "78", "12", "10"),
  Increase = c("TRUE", "TRUE", "TRUE", "FALSE")
)

# put the data in long format
d <- d %>%
  gather(key = "Strategy", value = "raw", Tr, Sr)

d %>%
  ggplot(aes(x = Group, y = raw, color = Strategy)) +
  geom_boxplot(width = 0.5, lwd = 0.5) +
  geom_jitter(width = 0.15) +
  geom_line(aes(group = raw),
            color = "grey",
            arrow = arrow(type = "closed",
                          length = unit(0.075, "inches"))) 

Could you at the minimum hand draw the output chart you desire and upload? It is unclear to me what exactly you desire.

Note: Please edit to correct the variable name in your code. Replace data by d as you use d in the beginning.

Hi Sanjmeh, thank you for your reply. I changed the data to d in the code above.

What I am looking for is something like this, where both scores for each participant are connected by a lines with both the Ctr and Exp groups. Additionally, I also need them to be color coded based on True or False.
image

In the above solution, I am not able to follow/implement how the jitter function works before ggplot, nor how to implement it in my code in order to get the lines.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.