I have a df
with an integer variable, counts
, and a POSIXct datetime
variable. The datetime variable represents minute intervals of different days:
counts datetime
246 02/11/2011 19:00
237 02/11/2011 19:01
268 02/11/2011 19:02
496 02/11/2011 19:03
259 02/11/2011 19:04
548 09/12/2011 20:14
I would like to have a line plot for each day, with the different HH:MM in the x axis. I can get the day, hours and minutes with lubridate
:
require(lubridate)
day(df$datetime)
minute(df$datetime)
But I am unable to put them together (I have tried the route of turning datetime to a HH:MM character unsuccessfully). I am missing the "x=factor(HH:MM of datetime)" below. Any idea comment?
ggplot(df, aes(x=factor(HH:MM of datetime), y=counts, group=day(datetime)) + geom_line()
Example: Each line should a different day. The x axis should be the HH:MM, the y the counts.
Data:
structure(list(all = c(246, 237, 268, 496, 259, 294, 414, 290,
267, 375, 402, 355, 572, 319, 354, 612, 478, 327, 511, 500, 468,
565, 472, 524, 844, 442, 382, 515, 542, 390, 511, 759, 933, 805,
774, 660, 748, 645, 501, 775, 768, 416, 612, 748, 554, 832, 1106,
760, 1127, 995, 846, 1057, 1287, 866, 923, 1264, 1017, 755, 1102,
753, 968, 1245, 943, 1013, 1324, 1515, 1098, 1160, 1656, 1209,
1434, 1330, 1248, 1558, 1406, 1689, 1584, 1543, 1405, 1907, 1606,
1353, 2332, 1861, 1653, 2396, 1894, 1730, 2620, 1795, 1940, 2186,
2443, 2234, 2618, 2397, 2122, 2410, 2835, 2325, 2456, 2484, 2375,
2515, 2679, 2833, 2476, 3362, 2629, 3262, 3070, 2683, 2882, 3190,
2523, 3286, 2988, 2724, 2897, 2917, 2630, 2990, 2399, 2329, 2571,
2361, 2162, 2206, 2008, 2243, 2494, 1869, 1807, 2316, 2381, 1803,
2396, 1850, 1872, 2156, 1739, 1722, 2094, 1462, 1891, 1980, 1303,
1640, 1659, 1217, 1236, 1323, 1127, 1314, 1551, 1169, 1405, 1284,
1120, 1306, 1424, 971, 1068, 1375, 1198, 1123, 1202, 1007, 988,
1195, 1087, 953, 1070, 1050, 764, 913, 813, 862, 1058, 844, 329,
299, 236, 462, 330, 264, 339, 388, 220, 443, 451, 407, 393, 289,
367, 482, 506, 367, 565, 604, 459, 615, 555, 485, 633, 411, 430,
341, 719, 619, 477, 903, 785, 713, 765, 607, 782, 799, 551, 706,
682, 447, 664, 1223, 609, 797, 957, 799, 815, 924, 889, 1133,
1114, 820, 857, 1236, 1007, 753, 1276, 966, 950, 1345, 1131,
1030, 1626, 828, 1358, 1469, 1267, 1232, 1462, 1419, 1461, 1736,
1470, 1281, 2091, 1797, 1247, 1731, 1512, 1460, 2199, 1691, 1890,
2229, 2128, 2107, 2623, 1875, 2163, 2057, 2626, 1914, 2312, 2229,
2362, 2234, 2674, 1745, 2995, 2709, 2150, 2666, 2450, 2363, 2610,
3050, 2975, 2861, 3075, 2638, 3136, 3272, 2440, 2874, 2904, 2716,
3124, 3056, 2413, 2916, 2057, 2384, 2649, 2304, 2034, 2246, 2360,
2324, 2566, 1972, 1986, 2430, 2150, 1853, 2067, 1900, 1778, 2091,
1936, 1749, 1906, 1789, 1656, 1998, 1463, 1449, 1534, 1252, 1515,
1704, 1440, 1397, 1521, 1155, 1320, 1281, 1033, 1229, 1369, 989,
1239, 1154, 1079, 924, 1168, 1008, 1127, 1186, 991, 921, 1174,
991, 799, 694, 1037, 1033, 962, 874, 417, 114, 263, 499, 297,
292, 424, 376, 270, 396, 382, 300, 523, 352, 236, 590, 613, 355,
608, 527, 357, 594, 491, 423, 796, 478, 433, 491, 547, 303, 485,
611, 378, 940, 863, 475, 650, 855, 868, 903, 561, 551, 521, 1084,
820, 690, 1040, 759, 1024, 896, 499, 1022, 1153, 697, 1208, 802,
745, 1242, 1070, 1039, 1020, 1204, 1127, 997, 1491, 993, 1270,
1390, 1014, 1233, 1525, 1098, 954, 1581, 1751, 1379, 1669, 1875,
1742, 1195, 2002, 1715, 1891, 1581, 1441, 1889, 2346, 1838, 2400,
1955, 2344, 2027, 2057, 1744, 2436, 2328, 2191, 1899, 2942, 2348,
2871, 2691, 2773, 2592, 2961, 2811, 2517, 2850, 2739, 2814, 2998,
2726, 2969, 3176, 2785, 3039, 2817, 2778, 3290, 2653, 2495, 2622,
2553, 2549, 2398, 2161, 2121, 2401, 2504, 2289, 2418, 2238, 2077,
2288, 2062, 2033, 2081, 2096, 1722, 1942, 1772, 1800, 1731, 1424,
1821, 2101, 1454, 1533, 1585, 1680, 1522, 1547, 1335, 1614, 1557,
1389, 1369, 1412, 1294, 1351, 1417, 1395, 997, 1092, 1296, 1050,
1023, 1229, 1025, 1271, 745, 994, 1078, 1020, 1074, 776, 578,
1044, 1107, 966), datetime = structure(c(1265176800, 1265176860,
1265176920, 1265176980, 1265177040, 1265177100, 1265177160, 1265177220,
1265177280, 1265177340, 1265177400, 1265177460, 1265177520, 1265177580,
1265177640, 1265177700, 1265177760, 1265177820, 1265177880, 1265177940,
1265178000, 1265178060, 1265178120, 1265178180, 1265178240, 1265178300,
1265178360, 1265178420, 1265178480, 1265178540, 1265178600, 1265178660,
1265178720, 1265178780, 1265178840, 1265178900, 1265178960, 1265179020,
1265179080, 1265179140, 1265179200, 1265179260, 1265179320, 1265179380,
1265179440, 1265179500, 1265179560, 1265179620, 1265179680, 1265179740,
1265179800, 1265179860, 1265179920, 1265179980, 1265180040, 1265180100,
1265180160, 1265180220, 1265180280, 1265180340, 1265180400, 1265180460,
1265180520, 1265180580, 1265180640, 1265180700, 1265180760, 1265180820,
1265180880, 1265180940, 1265181000, 1265181060, 1265181120, 1265181180,
1265181240, 1265181300, 1265181360, 1265181420, 1265181480, 1265181540,
1265181600, 1265181660, 1265181720, 1265181780, 1265181840, 1265181900,
1265181960, 1265182020, 1265182080, 1265182140, 1265182200, 1265182260,
1265182320, 1265182380, 1265182440, 1265182500, 1265182560, 1265182620,
1265182680, 1265182740, 1265182800, 1265182860, 1265182920, 1265182980,
1265183040, 1265183100, 1265183160, 1265183220, 1265183280, 1265183340,
1265183400, 1265183460, 1265183520, 1265183580, 1265183640, 1265183700,
1265183760, 1265183820, 1265183880, 1265183940, 1265184000, 1265184060,
1265184120, 1265184180, 1265184240, 1265184300, 1265184360, 1265184420,
1265184480, 1265184540, 1265184600, 1265184660, 1265184720, 1265184780,
1265184840, 1265184900, 1265184960, 1265185020, 1265185080, 1265185140,
1265185200, 1265185260, 1265185320, 1265185380, 1265185440, 1265185500,
1265185560, 1265185620, 1265185680, 1265185740, 1265185800, 1265185860,
1265185920, 1265185980, 1265186040, 1265186100, 1265186160, 1265186220,
1265186280, 1265186340, 1265186400, 1265186460, 1265186520, 1265186580,
1265186640, 1265186700, 1265186760, 1265186820, 1265186880, 1265186940,
1265187000, 1265187060, 1265187120, 1265187180, 1265187240, 1265187300,
1265187360, 1265187420, 1265187480, 1265187540, 1265263200, 1265263260,
1265263320, 1265263380, 1265263440, 1265263500, 1265263560, 1265263620,
1265263680, 1265263740, 1265263800, 1265263860, 1265263920, 1265263980,
1265264040, 1265264100, 1265264160, 1265264220, 1265264280, 1265264340,
1265264400, 1265264460, 1265264520, 1265264580, 1265264640, 1265264700,
1265264760, 1265264820, 1265264880, 1265264940, 1265265000, 1265265060,
1265265120, 1265265180, 1265265240, 1265265300, 1265265360, 1265265420,
1265265480, 1265265540, 1265265600, 1265265660, 1265265720, 1265265780,
1265265840, 1265265900, 1265265960, 1265266020, 1265266080, 1265266140,
1265266200, 1265266260, 1265266320, 1265266380, 1265266440, 1265266500,
1265266560, 1265266620, 1265266680, 1265266740, 1265266800, 1265266860,
1265266920, 1265266980, 1265267040, 1265267100, 1265267160, 1265267220,
1265267280, 1265267340, 1265267400, 1265267460, 1265267520, 1265267580,
1265267640, 1265267700, 1265267760, 1265267820, 1265267880, 1265267940,
1265268000, 1265268060, 1265268120, 1265268180, 1265268240, 1265268300,
1265268360, 1265268420, 1265268480, 1265268540, 1265268600, 1265268660,
1265268720, 1265268780, 1265268840, 1265268900, 1265268960, 1265269020,
1265269080, 1265269140, 1265269200, 1265269260, 1265269320, 1265269380,
1265269440, 1265269500, 1265269560, 1265269620, 1265269680, 1265269740,
1265269800, 1265269860, 1265269920, 1265269980, 1265270040, 1265270100,
1265270160, 1265270220, 1265270280, 1265270340, 1265270400, 1265270460,
1265270520, 1265270580, 1265270640, 1265270700, 1265270760, 1265270820,
1265270880, 1265270940, 1265271000, 1265271060, 1265271120, 1265271180,
1265271240, 1265271300, 1265271360, 1265271420, 1265271480, 1265271540,
1265271600, 1265271660, 1265271720, 1265271780, 1265271840, 1265271900,
1265271960, 1265272020, 1265272080, 1265272140, 1265272200, 1265272260,
1265272320, 1265272380, 1265272440, 1265272500, 1265272560, 1265272620,
1265272680, 1265272740, 1265272800, 1265272860, 1265272920, 1265272980,
1265273040, 1265273100, 1265273160, 1265273220, 1265273280, 1265273340,
1265273400, 1265273460, 1265273520, 1265273580, 1265273640, 1265273700,
1265273760, 1265273820, 1265273880, 1265273940, 1265349600, 1265349660,
1265349720, 1265349780, 1265349840, 1265349900, 1265349960, 1265350020,
1265350080, 1265350140, 1265350200, 1265350260, 1265350320, 1265350380,
1265350440, 1265350500, 1265350560, 1265350620, 1265350680, 1265350740,
1265350800, 1265350860, 1265350920, 1265350980, 1265351040, 1265351100,
1265351160, 1265351220, 1265351280, 1265351340, 1265351400, 1265351460,
1265351520, 1265351580, 1265351640, 1265351700, 1265351760, 1265351820,
1265351880, 1265351940, 1265352000, 1265352060, 1265352120, 1265352180,
1265352240, 1265352300, 1265352360, 1265352420, 1265352480, 1265352540,
1265352600, 1265352660, 1265352720, 1265352780, 1265352840, 1265352900,
1265352960, 1265353020, 1265353080, 1265353140, 1265353200, 1265353260,
1265353320, 1265353380, 1265353440, 1265353500, 1265353560, 1265353620,
1265353680, 1265353740, 1265353800, 1265353860, 1265353920, 1265353980,
1265354040, 1265354100, 1265354160, 1265354220, 1265354280, 1265354340,
1265354400, 1265354460, 1265354520, 1265354580, 1265354640, 1265354700,
1265354760, 1265354820, 1265354880, 1265354940, 1265355000, 1265355060,
1265355120, 1265355180, 1265355240, 1265355300, 1265355360, 1265355420,
1265355480, 1265355540, 1265355600, 1265355660, 1265355720, 1265355780,
1265355840, 1265355900, 1265355960, 1265356020, 1265356080, 1265356140,
1265356200, 1265356260, 1265356320, 1265356380, 1265356440, 1265356500,
1265356560, 1265356620, 1265356680, 1265356740, 1265356800, 1265356860,
1265356920, 1265356980, 1265357040, 1265357100, 1265357160, 1265357220,
1265357280, 1265357340, 1265357400, 1265357460, 1265357520, 1265357580,
1265357640, 1265357700, 1265357760, 1265357820, 1265357880, 1265357940,
1265358000, 1265358060, 1265358120, 1265358180, 1265358240, 1265358300,
1265358360, 1265358420, 1265358480, 1265358540, 1265358600, 1265358660,
1265358720, 1265358780, 1265358840, 1265358900, 1265358960, 1265359020,
1265359080, 1265359140, 1265359200, 1265359260, 1265359320, 1265359380,
1265359440, 1265359500, 1265359560, 1265359620, 1265359680, 1265359740,
1265359800, 1265359860, 1265359920, 1265359980, 1265360040, 1265360100,
1265360160, 1265360220, 1265360280, 1265360340), tzone = "", class = c("POSIXct",
"POSIXt"))), .Names = c("all", "datetime"), row.names = c(NA,
540L), class = "data.frame")
You could try
library(ggplot2)
library(scales)
df$date <- factor(as.Date(df$datetime))
df$dtime <- as.POSIXct(sprintf('%s %s:%s', Sys.Date(),
format(df$datetime, '%H:%M'), '00'))
p <- ggplot(df, aes(x=dtime, y=all, group=date, color=date))+
geom_line()
p+
scale_x_datetime(breaks=date_breaks('1 hour'),
labels=date_format('%H:%M')) +
theme_light()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With