Hi,
I have a dataset which I am trying to plot Revenue by year over the 12 month period of each year.
After exploring the data and decided to generate a simple Revenue X Time simple plot.
My issue is that when I group by year, I lose the "roughness" / "jaggedness" of the daily variations as well as the x-axis comes out of format. Any insight on what might be the issue here and how to get the plot to have x-axis Jan-Dez grouped by year.
Please find my below reprex:
library(tidyverse)
library(lubridate)
# Fictitious sample data of total revenue or amount billed by date.
x <- tribble(
~ActualInvDate, ~Tot.Billed.Amt,
"2-20-2018", 2315,
"2-22-2018", 825,
"2-23-2018", 710,
"2-26-2018", 1032,
"2-27-2018", 2519,
"3-1-2018", 5709,
"3-6-2018", 3457,
"3-9-2018", 2380,
"3-19-2018", 2515,
"3-23-2018", 1399,
"3-26-2018", 915,
"3-28-2018", 2846,
"4-3-2018", 2995,
"4-6-2018", 1848,
"4-16-2018", 2163,
"4-17-2018", 3316,
"4-18-2018", 4051,
"4-19-2018", 9872,
"4-20-2018", 3258,
"4-23-2018", 3248,
"4-24-2018", 2609,
"4-25-2018", 3436,
"4-26-2018", 1128,
"4-27-2018", 7194,
"4-30-2018", 1725,
"5-1-2018", 8568,
"5-2-2018", 5845,
"5-3-2018", 8665,
"5-4-2018", 12012,
"5-7-2018", 6676,
"5-8-2018", 12266,
"5-9-2018", 20535,
"5-10-2018", 15335,
"5-11-2018", 65970,
"5-14-2018", 2230,
"5-15-2018", 7316,
"5-16-2018", 8852,
"5-17-2018", 63012,
"5-18-2018", 34252,
"5-21-2018", 7421,
"5-22-2018", 6600,
"5-23-2018", 19201,
"5-24-2018", 11100,
"5-25-2018", 50899,
"5-26-2018", 3800,
"5-29-2018", 13308,
"5-30-2018", 7512,
"5-31-2018", 22619,
"6-1-2018", 39321,
"6-4-2018", 3566,
"6-5-2018", 13047,
"6-6-2018", 138839,
"6-7-2018", 26474,
"6-8-2018", 29660,
"6-11-2018", 8637,
"6-12-2018", 22942,
"6-13-2018", 30953,
"6-14-2018", 126284,
"6-15-2018", 55290,
"6-18-2018", 9670,
"6-19-2018", 42982,
"6-20-2018", 88784,
"6-21-2018", 41224,
"6-22-2018", 16450,
"6-25-2018", 26242,
"6-26-2018", 256503,
"6-27-2018", 18127,
"6-28-2018", 27626,
"6-29-2018", 45475,
"7-2-2018", 53976,
"7-3-2018", 25175,
"7-5-2018", 37834,
"7-6-2018", 33602,
"7-9-2018", 79154,
"7-10-2018", 16275,
"7-11-2018", 105632,
"7-12-2018", 22028,
"7-13-2018", 22039,
"7-16-2018", 63600,
"7-17-2018", 12807,
"7-18-2018", 28392,
"7-19-2018", 24746,
"7-20-2018", 44127,
"7-23-2018", 18321,
"7-24-2018", 58024,
"7-25-2018", 17775,
"7-26-2018", 29174,
"7-27-2018", 35527,
"7-30-2018", 52932,
"7-31-2018", 17608,
"8-1-2018", 73385,
"8-2-2018", 31679,
"8-3-2018", 53366,
"8-6-2018", 107639,
"8-7-2018", 10754,
"8-8-2018", 36492,
"8-9-2018", 78571,
"8-10-2018", 43583,
"8-13-2018", 30338,
"8-14-2018", 38666,
"8-15-2018", 46697,
"8-16-2018", 68630,
"8-17-2018", 58900,
"8-20-2018", 33593,
"8-21-2018", 33823,
"8-22-2018", 25898,
"8-23-2018", 21140,
"8-24-2018", 26173,
"8-27-2018", 57885,
"8-28-2018", 27521,
"8-29-2018", 93063,
"8-30-2018", 40859,
"8-31-2018", 14984,
"9-4-2018", 53205,
"9-5-2018", 84689,
"9-6-2018", 51083,
"9-7-2018", 64887,
"9-10-2018", 16876,
"9-11-2018", 34002,
"9-12-2018", 75343,
"9-13-2018", 35299,
"9-14-2018", 24313,
"9-16-2018", 12994,
"9-17-2018", 23038,
"9-18-2018", 69125,
"9-19-2018", 115924,
"9-20-2018", 77321,
"9-21-2018", 71522,
"9-24-2018", 54669,
"9-25-2018", 166560,
"9-26-2018", 32937,
"9-27-2018", 45888,
"9-28-2018", 55788,
"10-1-2018", 76223,
"10-2-2018", 38322,
"10-3-2018", 131780,
"10-4-2018", 24949,
"10-5-2018", 72824,
"10-8-2018", 32683,
"10-9-2018", 11963,
"10-10-2018", 48699,
"10-11-2018", 21639,
"10-12-2018", 67049,
"10-15-2018", 29531,
"10-16-2018", 33038,
"10-17-2018", 27702,
"10-18-2018", 108123,
"10-19-2018", 37491,
"10-22-2018", 42021,
"10-23-2018", 436226,
"10-24-2018", 85408,
"10-25-2018", 48811,
"10-26-2018", 48260,
"10-29-2018", 115547,
"10-30-2018", 87492,
"10-31-2018", 102753,
"11-1-2018", 40049,
"11-2-2018", 99763,
"11-3-2018", 1583,
"11-5-2018", 31273,
"11-6-2018", 32896,
"11-7-2018", 44155,
"11-8-2018", 50792,
"11-9-2018", 35316,
"11-12-2018", 13245,
"11-13-2018", 36688,
"11-14-2018", 58860,
"11-15-2018", 120772,
"11-16-2018", 72833,
"11-19-2018", 65900,
"11-20-2018", 57201,
"11-21-2018", 89682,
"11-23-2018", 25196,
"11-26-2018", 26029,
"11-27-2018", 177255,
"11-28-2018", 192322,
"11-29-2018", 90096,
"11-30-2018", 40402,
"12-3-2018", 41149,
"12-4-2018", 36235,
"12-5-2018", 193966,
"12-6-2018", 140907,
"12-7-2018", 335491,
"12-10-2018", 26606,
"12-11-2018", 52543,
"12-12-2018", 71969,
"12-13-2018", 42853,
"12-14-2018", 194443,
"12-17-2018", 31843,
"12-18-2018", 37400,
"12-19-2018", 68673,
"12-20-2018", 96150,
"12-21-2018", 106817,
"12-24-2018", 9951,
"12-26-2018", 47133,
"12-27-2018", 53725,
"12-28-2018", 91384,
"12-31-2018", 46223,
"1-2-2019", 134153,
"1-3-2019", 25835,
"1-4-2019", 105737,
"1-7-2019", 117669,
"1-8-2019", 64041,
"1-9-2019", 56282,
"1-10-2019", 57692,
"1-11-2019", 62629,
"1-14-2019", 19359,
"1-15-2019", 90822,
"1-16-2019", 105597,
"1-17-2019", 109124,
"1-18-2019", 67683,
"1-21-2019", 138530,
"1-22-2019", 111611,
"1-23-2019", 129246,
"1-24-2019", 73279,
"1-25-2019", 87689,
"1-28-2019", 35619,
"1-29-2019", 23879,
"1-30-2019", 74315,
"1-31-2019", 93815,
"2-1-2019", 20004,
"2-4-2019", 39538,
"2-5-2019", 21436,
"2-6-2019", 50803,
"2-7-2019", 53845,
"2-8-2019", 91022,
"2-11-2019", 60783,
"2-12-2019", 54061,
"2-13-2019", 115675,
"2-14-2019", 26104,
"2-15-2019", 172181,
"2-18-2019", 103545,
"2-19-2019", 56036,
"2-20-2019", 102366,
"2-21-2019", 105714,
"2-22-2019", 128733,
"2-24-2019", 4915,
"2-25-2019", 46749,
"2-26-2019", 76465,
"2-27-2019", 73043,
"2-28-2019", 65527,
"3-1-2019", 76577,
"3-2-2019", 1275,
"3-3-2019", 1056,
"3-4-2019", 84193,
"3-5-2019", 39162,
"3-6-2019", 45854,
"3-7-2019", 52611,
"3-8-2019", 202706,
"3-9-2019", 1574,
"3-11-2019", 191915,
"3-12-2019", 66025,
"3-13-2019", 50385,
"3-14-2019", 104651,
"3-15-2019", 178230,
"3-16-2019", 1819,
"3-18-2019", 176311,
"3-19-2019", 113162,
"3-20-2019", 193697,
"3-21-2019", 236478,
"3-22-2019", 137631,
"3-24-2019", 4494,
"3-25-2019", 172074,
"3-26-2019", 286207,
"3-27-2019", 134305,
"3-28-2019", 158184,
"3-29-2019", 82140,
"4-1-2019", 113498,
"4-2-2019", 180771,
"4-3-2019", 80910,
"4-4-2019", 81043,
"4-5-2019", 79580,
"4-6-2019", 1249,
"4-7-2019", 25047,
"4-8-2019", 112628,
"4-9-2019", 92044,
"4-10-2019", 107552,
"4-11-2019", 102431,
"4-12-2019", 83815,
"4-14-2019", 7185,
"4-15-2019", 79806,
"4-16-2019", 221777,
"4-17-2019", 94871,
"4-18-2019", 112382,
"4-19-2019", 58334,
"4-22-2019", 83021,
"4-23-2019", 111868,
"4-24-2019", 77150,
"4-25-2019", 84455,
"4-26-2019", 104736,
"4-29-2019", 76470,
"4-30-2019", 140846,
"5-1-2019", 127323,
"5-2-2019", 139238,
"5-3-2019", 92862,
"5-6-2019", 283460,
"5-7-2019", 127881,
"5-8-2019", 79966,
"5-9-2019", 159864,
"5-10-2019", 87209,
"5-13-2019", 107573,
"5-14-2019", 171410,
"5-15-2019", 108894,
"5-16-2019", 89317,
"5-17-2019", 74481,
"5-20-2019", 148184,
"5-21-2019", 215174,
"5-22-2019", 51308,
"5-23-2019", 110106,
"5-24-2019", 92743,
"5-26-2019", 29351,
"5-27-2019", 18849,
"5-28-2019", 240982,
"5-29-2019", 134842,
"5-30-2019", 106541,
"5-31-2019", 99460,
"6-2-2019", 8412,
"6-3-2019", 118405,
"6-4-2019", 144759,
"6-5-2019", 123294,
"6-6-2019", 67532,
"6-7-2019", 202912,
"6-10-2019", 164081,
"6-11-2019", 367452,
"6-12-2019", 189177,
"6-13-2019", 90003,
"6-14-2019", 119539,
"6-17-2019", 169452,
"6-18-2019", 196062,
"6-19-2019", 104542,
"6-20-2019", 62737,
"6-21-2019", 75287,
"6-24-2019", 127385,
"6-25-2019", 295774,
"6-26-2019", 279868,
"6-27-2019", 100558,
"6-28-2019", 78487,
"7-1-2019", 127187,
"7-2-2019", 285203,
"7-3-2019", 48705,
"7-5-2019", 115280,
"7-7-2019", 1207,
"7-8-2019", 124428,
"7-9-2019", 251938,
"7-10-2019", 104617,
"7-11-2019", 141377,
"7-12-2019", 187875,
"7-15-2019", 117554,
"7-16-2019", 212808,
"7-17-2019", 147232,
"7-18-2019", 149241,
"7-19-2019", 94922,
"7-22-2019", 209061,
"7-23-2019", 192436,
"7-24-2019", 258726,
"7-25-2019", 272500,
"7-26-2019", 183545,
"7-28-2019", 3450,
"7-29-2019", 150473,
"7-30-2019", 322693,
"7-31-2019", 154438,
"8-1-2019", 118792,
"8-2-2019", 127683,
"8-5-2019", 193117,
"8-6-2019", 258290,
"8-7-2019", 149324,
"8-8-2019", 134461,
"8-9-2019", 58165,
"8-10-2019", 1728,
"8-12-2019", 100383,
"8-13-2019", 204296,
"8-14-2019", 107806,
"8-15-2019", 198694,
"8-16-2019", 203261,
"8-19-2019", 186531,
"8-20-2019", 312579,
"8-21-2019", 99326,
"8-22-2019", 190502,
"8-23-2019", 155308,
"8-26-2019", 324124,
"8-27-2019", 293942,
"8-28-2019", 183753,
"8-29-2019", 217908,
"8-30-2019", 81776,
"8-31-2019", 11544,
"9-3-2019", 191848,
"9-4-2019", 315857,
"9-5-2019", 231207,
"9-6-2019", 125868,
"9-7-2019", 2337,
"9-9-2019", 146405,
"9-10-2019", 252506,
"9-11-2019", 124663,
"9-12-2019", 198406,
"9-13-2019", 137195,
"9-14-2019", 972,
"9-16-2019", 124401,
"9-17-2019", 246132,
"9-18-2019", 127232,
"9-19-2019", 216183,
"9-20-2019", 177872,
"9-22-2019", 2755,
"9-23-2019", 232763,
"9-24-2019", 257909,
"9-25-2019", 169977,
"9-26-2019", 205241,
"9-27-2019", 144566,
"9-30-2019", 269271,
"10-1-2019", 208830,
"10-2-2019", 159483,
"10-3-2019", 213372,
"10-4-2019", 148954,
"10-5-2019", 514,
"10-7-2019", 146648,
"10-8-2019", 299011,
"10-9-2019", 254048,
"10-10-2019", 256041,
"10-11-2019", 119752,
"10-14-2019", 115786,
"10-15-2019", 180412,
"10-16-2019", 305405,
"10-17-2019", 106641,
"10-18-2019", 105346,
"10-20-2019", 3150,
"10-21-2019", 240890,
"10-22-2019", 317188,
"10-23-2019", 189734,
"10-24-2019", 77177,
"10-25-2019", 165337,
"10-28-2019", 180917,
"10-29-2019", 78138,
"10-30-2019", 176064,
"10-31-2019", 108147,
"11-1-2019", 107501,
"11-2-2019", 2467,
"11-3-2019", 894,
"11-4-2019", 217879,
"11-5-2019", 179882,
"11-6-2019", 52904,
"11-7-2019", 119438,
"11-8-2019", 167452,
"11-9-2019", 504,
"11-10-2019", 4232,
"11-11-2019", 231509,
"11-12-2019", 214653,
"11-13-2019", 261901,
"11-14-2019", 36632,
"11-15-2019", 264392,
"11-17-2019", 2794,
"11-18-2019", 236941,
"11-19-2019", 272816,
"11-20-2019", 180210,
"11-21-2019", 168248,
"11-22-2019", 198985,
"11-23-2019", 80153,
"11-24-2019", 6696,
"11-25-2019", 145417,
"11-26-2019", 233239,
"11-27-2019", -1281939,
"11-29-2019", 15402,
"12-2-2019", 75270,
"12-3-2019", 215988,
"12-4-2019", 210551,
"12-5-2019", 174883,
"12-6-2019", 181061,
"12-7-2019", 504,
"12-9-2019", 147408,
"12-10-2019", 310209,
"12-11-2019", 182883,
"12-12-2019", 157903,
"12-13-2019", 145769,
"12-14-2019", 800,
"12-16-2019", 136420,
"12-17-2019", 213845,
"12-18-2019", 175403,
"12-19-2019", 73617,
"12-20-2019", 254792,
"12-22-2019", 3800,
"12-23-2019", 210088,
"12-24-2019", 102881,
"12-26-2019", 163274,
"12-27-2019", 88936,
"12-29-2019", 15859,
"12-30-2019", 170950,
"12-31-2019", 331191,
"1-1-2020", 9963,
"1-2-2020", 100166,
"1-3-2020", 166192,
"1-4-2020", 4248,
"1-6-2020", 90837,
"1-7-2020", 186577,
"1-8-2020", 95335,
"1-9-2020", 143383,
"1-10-2020", 169206,
"1-13-2020", 171433,
"1-14-2020", 223742,
"1-15-2020", 213288,
"1-16-2020", 143010,
"1-17-2020", 398692,
"1-20-2020", 212388,
"1-21-2020", 420029,
"1-22-2020", 147302,
"1-23-2020", 191470,
"1-24-2020", 266806,
"1-27-2020", 203302,
"1-28-2020", 267417,
"1-29-2020", 222521,
"1-30-2020", 209008,
"1-31-2020", 134952,
"2-2-2020", 10680,
"2-3-2020", 281478,
"2-4-2020", 352522,
"2-5-2020", 104263,
"2-6-2020", 138173,
"2-7-2020", 215664,
"2-8-2020", 4441,
"2-10-2020", 284293,
"2-11-2020", 352285,
"2-12-2020", 149764,
"2-13-2020", 225254,
"2-14-2020", 159852,
"2-17-2020", 195529,
"2-18-2020", 186526,
"2-19-2020", 345102,
"2-20-2020", 179146,
"2-21-2020", 322430,
"2-23-2020", 660,
"2-24-2020", 301683,
"2-25-2020", 250295,
"2-26-2020", 148598,
"2-27-2020", 190384,
"2-28-2020", 265806,
"3-2-2020", 135718,
"3-3-2020", 363251,
"3-4-2020", 164761,
"3-5-2020", 184143,
"3-6-2020", 45863,
"3-9-2020", 248728,
"3-10-2020", 206968,
"3-11-2020", 186121,
"3-12-2020", 225251,
"3-13-2020", 124201,
"3-16-2020", 230773,
"3-17-2020", 253656,
"3-18-2020", 137603,
"3-19-2020", 181515,
"3-20-2020", 44376,
"3-21-2020", 521,
"3-23-2020", 93516,
"3-24-2020", 410314,
"3-25-2020", 120042,
"3-26-2020", 179452,
"3-27-2020", 62426,
"3-30-2020", 141558,
"3-31-2020", 245599,
"4-1-2020", 279977,
"4-2-2020", 159775,
"4-3-2020", 79169,
"4-6-2020", 133797,
"4-7-2020", 140002,
"4-8-2020", 385024,
"4-9-2020", 151364,
"4-10-2020", 70551,
"4-12-2020", 3814,
"4-13-2020", 331806,
"4-14-2020", 300080,
"4-15-2020", 223719,
"4-16-2020", 235440,
"4-17-2020", 42567,
"4-20-2020", 154566,
"4-21-2020", 323098,
"4-22-2020", 39385,
"4-23-2020", 298896,
"4-24-2020", 234195,
"4-27-2020", 177914,
"4-28-2020", 441017,
"4-29-2020", 223406,
"4-30-2020", 278052,
"5-1-2020", 226830,
"5-3-2020", 1898,
"5-4-2020", 314639,
"5-5-2020", 228052,
"5-6-2020", 110759,
"5-7-2020", 204911,
"5-8-2020", 239334,
"5-10-2020", 2020,
"5-11-2020", 167912,
"5-12-2020", 250557,
"5-13-2020", 161858,
"5-14-2020", 277749,
"5-15-2020", 117101,
"5-17-2020", 3864,
"5-18-2020", 192543,
"5-19-2020", 274125,
"5-20-2020", 162523,
"5-21-2020", 147519,
"5-22-2020", 157107,
"5-26-2020", 349935,
"5-27-2020", 58701,
"5-28-2020", 182074,
"5-29-2020", 355316,
"5-30-2020", 1570,
"5-31-2020", 19866,
"6-1-2020", 488207,
"6-2-2020", 207082,
"6-3-2020", 286356,
"6-4-2020", 280647,
"6-5-2020", 208814,
"6-8-2020", 257497,
"6-9-2020", 171638,
"6-10-2020", 142506,
"6-11-2020", 181565,
"6-12-2020", 136672,
"6-13-2020", 29165,
"6-15-2020", 607336,
"6-16-2020", 389575,
"6-17-2020", 183919,
"6-18-2020", 295681,
"6-19-2020", 199838,
"6-22-2020", 380438,
"6-23-2020", 121738,
"6-24-2020", 101299,
"6-25-2020", 216002,
"6-26-2020", 137952,
"6-27-2020", 107081,
"6-29-2020", 336415,
"6-30-2020", 396493,
"7-1-2020", 179598,
"7-2-2020", 95814,
"7-3-2020", 20702,
"7-6-2020", 285842,
"7-7-2020", 232135,
"7-8-2020", 215388,
"7-9-2020", 153461,
"7-10-2020", 176932,
"7-13-2020", 208367)
# Breaking into y/m/d and rename it:
x <- x %>% mutate(`ActualInvDate` = mdy(`ActualInvDate`)) %>%
mutate_at(vars(`ActualInvDate`),
funs(year, month, day)) %>%
rename("Act.Inv.Year" = `year`,
"Act.Inv.Month" = `month`,
"Act.Inv.Day" = `day`
) %>% mutate(`Act.Inv.Year` = as.factor(`Act.Inv.Year`)) # Ensure Factor for plotting by year
#Plot 1
# Simple revenue x time
ggplot(data = x,
aes(
x = `ActualInvDate`, y = `Tot.Billed.Amt`/1000
)) + geom_line()
# Plot2
# grouped by year, issue on x-axis (should be the months),
# lost the "granularity" of first
ggplot(data = x,
aes(x = `Act.Inv.Month`, y = `Tot.Billed.Amt`/1000,
color = `Act.Inv.Year`
)) +
geom_line() +
labs(x = "Invoice Date", y = "Amount Billed (Thousands $)")
Any feedback would be much appreciated.
Thanks.