0

I am trying to create a line graph which shows the pre and post scores of a questionnaire after an intervention. I am struggling as I want to have the each participant's score pre and post. There is 7 participants so I wanted to have the participants as the 'x' axis and the scores on 'y'. Does anyone know how to do this? I am very unfamiliar with R so unsure on where to even start. I hope the image makes sense for what I am trying to create lol enter image description hereenter image description here

JacobHad
  • 33
  • 4
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Sep 20 '22 at 03:13

1 Answers1

0

tidy solution:

set.seed(1)
df <- data.frame(participant = LETTERS[1:7],pre_score = runif(7,40,90),post_score = runif(7,60,100))

library(dplyr)
library(tidyr)
library(ggplot2)

df %>%
  pivot_longer(-participant, names_to = "intervention", values_to = "score") %>%
  ggplot(.,aes(x=participant,y=score,group=intervention,color=intervention))+
  geom_line()
Eric
  • 1,381
  • 9
  • 24
  • thank you so much for this! i think i found a way round, albeit a long one. my code is as follows: Quip_scores <- ggplot()+ geom_line(data = my_data, mapping = aes(x=Participant, y=PRE_QUIP_RS), color="blue") + geom_point(data = my_data, mapping = aes(x=Participant, y=PRE_QUIP_RS), color="blue") + geom_line(data = my_data, mapping = aes(x= Participant, y = POST_QUIP_RS), color = "orange") + geom_point(data = my_data, mapping = aes(x= Participant, y = POST_QUIP_RS), color = "orange") + labs(x = "Participants", y = "QUIP RS Scores", title = "Pre and Post QUIP-RS Scores") – JacobHad Sep 20 '22 at 05:33
  • your solution will work for this case but as you add more lines/groups or use facets it will become unwieldy. familiarize yourself with reshaping your data frame to long format (I'm showing tidyr but it's not the only way) before applying ggplot function. it will pay off in the long run – Eric Sep 20 '22 at 12:44
  • I tried adding this to the code just to see if it would work and it throws up this error 'Error in FUN(X[[i]], ...) : object 'participant' not found'. Any idea what this may be? – JacobHad Sep 20 '22 at 14:12
  • If you can [create a reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) it would be easier to figure out why you're getting an error. – Eric Sep 20 '22 at 20:25