0

I would like to create a line graph for the following data: DV = "RKZsumscore" (numerical), IVs = "Zeitpunkt" (T1 vs T2) "Wahrheit" (true vs. false). It is a within-subject design, i.e. each of the 30 Subjects had 4 experimental sessions (T1 true, T1 false, T2 true, T2 false). Now I would like to plot the RKZsumscores for each subject at T1 and T2, for the true and false conditions. I used the following code to accomplish this:

ggplot(data = Artikel2_Long) + 
      geom_point(aes(x = Zeitpunkt, y = SummenscoreRKZall, color = Wahrheit, group = Vp_Code)) 

This worked fine and resulted in the plot below: Graph 1

No here is my problem: What I would like to do further is to connect each subject value/point at T1 with each corresponding subject value/point at T2, separately for true and false (so for instance, a green line for the scores of subject #1 in the true condition between T1 and T2, and and orange line for the scores of subject #1 in false condition between T1 and T2). I tried to accomplish this by:

ggplot(data = Artikel2_Long, aes(x = Zeitpunkt, y = SummenscoreRKZall, color = Wahrheit, group = Vp_Code)) +
      geom_point() + geom_line()

However, this results in the following graph:

Graph 2, points wrongly connected

So the problem is that R does not connect the points in the way specified above, but rather arbitrarily. If I understand this correctly, this problem is caused by the fact that there are four conditions for each subject. Hence, for group = Vp_Code (formula to group for subject), there are 4 scores for each subject, and R does not know which of these 4 scores should be connected to pairs. Do I have to label the Vp_Codes differently for true and false condition, or is there a better way to have the points connected in the right way (so for each individual, T1true -> T2true, T1false -> T2false)?

Thank you very much in advance!

MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Nov 08 '18 at 17:58
  • 1
    Yes, this will be easier to answer (and know that we've succeeded) if you can provide a data sample. I suspect something like `group = interaction(Vp_Code, Wahrheit)` might work, since you want `geom_line` to connect the true and false responses for each subject separately. – Jon Spring Nov 08 '18 at 18:05
  • The command group = interaction worked, thank you! One question for future posts: Could you briefly point out how to provide a data sample? I understand this was missing in order to make my example reproducible – eliOnis Nov 08 '18 at 22:41

0 Answers0