0

I want to visualize individual longitudinal data with a spaghetti plot using ggplot2.

My data looks like this:

ID   task   response timepoint
1    naming   15       1
1    naming   28       2
2    naming   8        1
2    naming   10       2

All variables except of "response" are factors.

I ran this code and got a plot with lines, that connect something, but not the datapoint of timepoint 1 and timepoint 2.

datal.diff %>%
ggplot(aes(timepoint,response, color = ID, group=1)) + 
facet_grid(.~ task) +
geom_point() +
geom_line()

Thank you for any comments and ideas!

Screenshot of the plot, I get with my code

Marco Sandri
  • 23,289
  • 7
  • 54
  • 58
Akoasma
  • 11
  • 5
  • 2
    Why did you use `group=1`? That seems to be causing your problem. And if your IDs are all numeric (which they are in your example but don't appear to be in your actual data), use `group=ID` instead, or use `color = factor(ID)` depending on how you want the coloring. – MrFlick Jul 17 '17 at 22:04
  • Awsome! `group = ID` works perfectly. I used `group = 1`, because that was a suggestion in a different thread with a similar problem. Thank you so much! – Akoasma Jul 17 '17 at 22:14

1 Answers1

2

The group=1 is treating all the points as coming from the same series so it draws one line. If you want to connect the points for each ID, then use

ggplot(aes(timepoint, response, color = ID, group=ID))

But since you are already using color, that will by default use ID as the group so

ggplot(aes(timepoint, response, color = ID))

should have worked as well.

MrFlick
  • 195,160
  • 17
  • 277
  • 295