elG
elG

Reputation: 629

R - ggplot two lines on same graph depending on date

After combining, subsetting my data, and converting the DATE column to a datetime by using this: atms$DATE<-dmy_hms(as.POSIXct(atms$DATE,tz=Sys.timezone())), it looks like this:

    ATM                DATE DENOM_1 REMAIN_1 DENOM_2 REMAIN_2 Total
176    8 2020-09-13 19:00:00      50     1334     100     1494  2828
177    8 2020-09-13 19:01:00      50     1334     100     1494  2828
178    8 2020-09-13 19:02:00      50     1334     100     1494  2828
179    8 2020-09-13 19:03:00      50     1334     100     1494  2828
180    8 2020-09-13 19:04:00      50     1334     100     1494  2828
181    8 2020-09-13 19:05:00      50     1334     100     1494  2828
182    8 2020-09-13 19:06:00      50     1331     100     1493  2824
183    8 2020-09-13 19:07:00      50     1317     100     1487  2804
184    8 2020-09-13 19:08:00      50     1315     100     1471  2786
185    8 2020-09-13 19:09:00      50     1305     100     1468  2773
186    8 2020-09-13 19:10:00      50     1302     100     1459  2761
187    8 2020-09-13 19:11:00      50     1278     100     1428  2706
188    8 2020-09-13 19:12:00      50     1275     100     1421  2696
189    8 2020-09-13 19:13:00      50     1238     100     1392  2630
190    8 2020-09-13 19:14:00      50     1204     100     1322  2526
191    8 2020-09-13 19:15:00      50     1153     100     1236  2389
192    8 2020-09-13 19:16:00      50     1109     100     1140  2249
193    8 2020-09-13 19:17:00      50     1045     100     1047  2092
194    8 2020-09-13 19:18:00      50      974     100      974  1948
195    8 2020-09-13 19:19:00      50      932     100      868  1800
196    8 2020-09-13 19:20:00      50      871     100      781  1652
197    8 2020-09-13 19:21:00      50      841     100      720  1561
198    8 2020-09-13 19:22:00      50      829     100      705  1534
199    8 2020-09-13 19:22:40      50      825     100      696  1521
200    8 2020-09-13 19:23:00      50      825     100      696  1521
7576   8 2020-09-20 19:00:00      50     1097     100      698  1795
7577   8 2020-09-20 19:01:00      50     1097     100      698  1795
7578   8 2020-09-20 19:02:00      50     1097     100      698  1795
7579   8 2020-09-20 19:03:00      50     1097     100      698  1795
7580   8 2020-09-20 19:04:00      50     1097     100      698  1795
7581   8 2020-09-20 19:05:00      50     1097     100      698  1795
7582   8 2020-09-20 19:06:00      50     1085     100      691  1776
7583   8 2020-09-20 19:07:00      50     1085     100      689  1774
7584   8 2020-09-20 19:08:00      50     1068     100      680  1748
7585   8 2020-09-20 19:09:00      50     1064     100      669  1733
7586   8 2020-09-20 19:10:00      50     1039     100      647  1686
7587   8 2020-09-20 19:11:00      50     1014     100      622  1636
7588   8 2020-09-20 19:12:00      50     1007     100      600  1607
7589   8 2020-09-20 19:13:00      50      991     100      578  1569
7590   8 2020-09-20 19:14:00      50      975     100      552  1527
7591   8 2020-09-20 19:15:00      50      880     100      473  1353
7592   8 2020-09-20 19:16:00      50      816     100      403  1219
7593   8 2020-09-20 19:17:00      50      757     100      341  1098
7594   8 2020-09-20 19:18:00      50      707     100      290   997
7595   8 2020-09-20 19:19:00      50      680     100      256   936
7596   8 2020-09-20 19:20:00      50      644     100      150   794
7597   8 2020-09-20 19:21:00      50      630     100      139   769
7598   8 2020-09-20 19:21:51      50      623     100      110   733
7599   8 2020-09-20 19:22:00      50      623     100      110   733
7600   8 2020-09-20 19:23:00      50      623     100      110   733

I want to graph the total for day 13 in one line and the total for day 20 in other line, on the same graph. Originally the data is separated in two .dat files, one for each date, I've tried to graph only day 13 adding it to an empty ggplot() by using geom_line(), then adding an independent geomline() for day 20 to the same ggplot, each line has its own "X" axis inside its "aes" as follows: geom_line(data=myData,aes(x=correspondingDate,y=Total)), but I just see one line representing the Total, the other one is hidden; then I've tried a "global" X axis for both lines, by using ggplot(data=myData,aes(x=date13)) and then geom_line(data=myData,y=totalCorrespondingDate)) for each line representing each day, but I just see two vertical lines, one to the left and the other to the right, with X axis showing weird dates.

Should I keep the data separated by day as it comes? If yes, how can I "draw" my X axis to include both days? If no, how can I tell ggplot to separate the data depending on date only, when the column is a datetime? I know how to use melt function at a basic level, but I don't know how to tell it to take just the day of a daytime to make the "partition".

Upvotes: 0

Views: 277

Answers (1)

Jon Spring
Jon Spring

Reputation: 66445

I'd suggest making two new variables, one to track which date the row belongs to, and one which shows the time of day, but using one common date. For instance:

library(tidyverse)
df  %>%
  mutate(DATE_DAY = floor_date(DATE, "day"),   # day of the row
         DATE_TIME = min(DATE_DAY) + (DATE - DATE_DAY)) %>%   # datetime shifted to 1st day (arbitrarily)
  ggplot(aes(DATE_TIME, Total, color = DATE_DAY, group = DATE_DAY)) +
  geom_line()

enter image description here

Upvotes: 2

Related Questions