1

I am trying to plot points on top of existing lines in ggplot. If I plot just the lines, the y axis is in the order that I'd like it to be in. However, when I try to add the points, the y axis order changes and I can't figure out why.

Here's the code I've been using to produce the plot:

ggplot(sincevax_reshape, aes(x=value,y=factor(record_id), group=factor(record_id))) + 
  xlab("Days Since Vaccine") + ylab("Participant ID") +
  geom_line(data=sincevax_reshape[sincevax_reshape$variable=="Sample",], aes(x=value, y=factor(record_id), group=factor(record_id)), color="darkgrey", size=2) +
  geom_point(aes(x=value, y=factor(record_id)))

And here's some reproducible data to play around with:

     record_id  variable value
6           10    Sample  -182
7           11    Sample  -233
14          21    Sample  -189
16          23    Sample  -232
17          24    Sample  -214
18          24    Sample   20
19          24    Sample   102     
1110        10     Today   177
1111        11     Today   118
1112        13     Today   115
1113        14     Today    62
1114        15     Today   111
1115        16     Today   211
1116        18     Today   120
1117        20     Today    97
1118        21     Today   134
1119        22     Today    15
1120        23     Today    90
1121        24     Today   107
  • Each group consists of only one observation, that's not a great line... ;-) – Martin Gal Aug 17 '21 at 21:56
  • Edited the data above -- Record 24 should have multiple samples and a "Today" value as well. – user16368087 Aug 17 '21 at 22:00
  • Okay, now I'm able to plot your data. But I don't understand your problem. My y-axis isn't reordered. Since you are using `y = factor(...)` perhaps this causes your problem? Try removing the `factor(...)`? – Martin Gal Aug 17 '21 at 22:09
  • When I have records in the hundreds (like 100, for example), the record IDs end up being plotted as 10, 100, 104, 11 instead of 10, 11, 100, 104. Since the record IDs are integers, I thought I could get around that by converting them to factors. The ordering (10, 11, 100, 104) is correct when I have a geom_point followed by a geom_line, but when I try to switch the order of the layers, it reverts to 10, 100, 104, 11. – user16368087 Aug 17 '21 at 22:13
  • 1
    It might be because the factors in the line layer get other (less) levels than those in the point layers due to the subsetting operation. Faced with two factor variables with different levels, ggplot2 defaults to alphanumeric order. I also can't reproduce these ordering artefacts with the presented data. – teunbrand Aug 17 '21 at 22:17
  • It might help to reorder the limits in the scale: `scale_y_discrete(limits = ~ .x[order(as.numeric(.x))])`. – teunbrand Aug 17 '21 at 22:19

0 Answers0