Suppose I have following data for a student's score on a test.

set.seed(1)
df <- data.frame(question = 0:10,
                 resp = c(NA,sample(c("Correct","Incorrect"),10,replace=TRUE)), 
                 score.after.resp=50)
for (i in 1:10) {
  ifelse(df$resp[i+1] == "Correct",
         df$score.after.resp[i+1] <- df$score.after.resp[i] + 5, 
         df$score.after.resp[i+1] <- df$score.after.resp[i] - 5)
}
df

.

   question      resp score.after.resp
1         0      <NA>               50
2         1   Correct               55
3         2   Correct               60
4         3 Incorrect               55
5         4 Incorrect               50
6         5   Correct               55
7         6 Incorrect               50
8         7 Incorrect               45
9         8 Incorrect               40
10        9 Incorrect               35
11       10   Correct               40

I want to get following graph:

library(ggplot2)
ggplot(df,aes(x = question, y = score.after.resp)) + geom_line() + geom_point()

enter image description here

My problem is: I want to color segments of this line according to student response. If correct (increasing) line segment will be green and if incorrect response (decreasing) line should be red. I tried following code but did not work:

ggplot(df,aes(x = question, y = score.after.resp, color=factor(resp))) + 
  geom_line() + geom_point()

enter image description here

Any ideas?

Edit
Report