0

I have a dataframe panel data

This is a sample df

date    code_ID name_ID X_Value Y_Value
2021-03-10T17:00:00 13  Alpha   372 621
2021-03-11T17:00:00 13  Alpha   608 4578
2021-03-12T17:00:00 13  Alpha   515 4618
2021-03-13T17:00:00 13  Alpha   320 1522
2021-03-14T17:00:00 13  Alpha   323 1531
2021-03-15T17:00:00 13  Alpha   329 4663
2021-03-16T17:00:00 13  Alpha   212 4281
2021-03-17T17:00:00 13  Alpha   304 419
2021-03-18T17:00:00 13  Alpha   462 761
2021-03-10T17:00:00 17  Beta    115 3664
2021-03-11T17:00:00 17  Beta    151 4362
2021-03-12T17:00:00 17  Beta    141 4733
2021-03-13T17:00:00 17  Beta    137 3068
2021-03-14T17:00:00 17  Beta    106 4378
2021-03-15T17:00:00 17  Beta    67  4403
2021-03-16T17:00:00 17  Beta    166 1549
2021-03-17T17:00:00 17  Beta    126 9
2021-03-18T17:00:00 17  Beta    179 2871
2021-03-10T17:00:00 8   eta-firm    2155    181
2021-03-11T17:00:00 8   eta-firm    2845    4446
2021-03-12T17:00:00 8   eta-firm    3477    4212
2021-03-13T17:00:00 8   eta-firm    2950    914
2021-03-14T17:00:00 8   eta-firm    3023    1155
2021-03-15T17:00:00 8   eta-firm    2822    4920
2021-03-16T17:00:00 8   eta-firm    2184    4562
2021-03-17T17:00:00 8   eta-firm    2026    1021
2021-03-18T17:00:00 8   eta-firm    2531    1227
2021-03-10T17:00:00 6   phi hotel   866 4785
2021-03-11T17:00:00 6   phi hotel   991 873
2021-03-12T17:00:00 6   phi hotel   971 2824
2021-03-13T17:00:00 6   phi hotel   953 1113
2021-03-14T17:00:00 6   phi hotel   604 4851
2021-03-15T17:00:00 6   phi hotel   398 1364
2021-03-16T17:00:00 6   phi hotel   672 2210
2021-03-17T17:00:00 6   phi hotel   986 3668
2021-03-18T17:00:00 6   phi hotel   1058    4518

How can I plot the last N observations of X-Y by code_id/time using ggplot2 and without to delete rows?

Code I use actually:

ggplot(data=df, aes(x = X_Value, y = Y_Value, color = name_ID)) +
  geom_line( arrow = arrow(angle = 30, length = unit(0.09, "inches"), ends = "last", type = "closed"))
LastBorn
  • 123
  • 8
  • You'll need to subset your data before plotting. How to subset is already answered here: https://stackoverflow.com/questions/53994497/how-to-select-last-n-observation-from-each-group-in-dplyr-dataframe – MrFlick Mar 29 '21 at 16:39
  • The fact is that I do not want to delete rows or create subset df. Is there a way to make it inside ggplot code? – LastBorn Mar 29 '21 at 16:50
  • 2
    Nope. ggplot is a plotting tool. It plots whatever you pass in. If you don't want to plot data, then don't plot it in. I supposed you could create your own custom geom layer that would do the subsetting for you, but that's just hiding the subsetting, not eliminating the need. you don't have to save the subsetted data anywhere, you can just pass it in as the `data=` parameter. I'm not sure why you are trying so hard to avoid subsetting. Is there some reason you haven't yet mentioned? – MrFlick Mar 29 '21 at 16:53
  • No reason. I would have preferred to use only one dataframe. However I tried to use `by (df, df $ code_ID, FUN = tail, 11)` but it doesn't create a subset of my data. What am I doing wrong? – LastBorn Mar 29 '21 at 17:07
  • It seems like you picked the one answer on the question that doesn't actually return a data.frame that you can use for plotting. If you are just trying to use base R, try `do.call("rbind", (by(df, df$code_ID, FUN = tail, 11)))` – MrFlick Mar 29 '21 at 17:12

1 Answers1

0

This works fine do.call("rbind", (by(df, df$code_ID, FUN = tail, 11)))

LastBorn
  • 123
  • 8