I am attempting to graph two time series off sales for two items across a 12 month period (subset below). The sales of each item is influenced by the promotion condition of the other product, sometimes (due to seasonality) this causes a decrease in sales while other times it actually promotes sales. In order to represent this I have created two line graphs which are facet wrapped. In each facet we have one set of item sales(y = Sales), and its cross effect (y = cross_effect; dashed line) which shows the effect of the opposing promotion on sales, between these two lines I have a ribbon filling the gap in powderblue to demonstrate the magnitude. The ylim is allowed to vary with the data input. The issues I am not sure how to fix:
- Is it possible to make the colour of the ribbon and/or the cross-effect line vary i.e. green for increase and red for decrease?
- How can I specify my Y sales to be rounded to 10s if max value is 100, 100s if max is under 1000... up to 100000s? I know -2 to the round function would help here, but is there a better way to build the function than hard coding the rules?
Thanks for taking the time to read my question!
Here is some toy data and one of my attempts...
my_breaks <- function(x){seq(min(0), max(x), length.out = 5)}
my_labels <- function(x){round(x, digits = 0)}
Prop_change_df %>%
mutate(cross_effect = as.integer(cross_effect))%>%
mutate(cross_eff_gp = as.factor(if_else(cross_effect > Sales, "Lift", "Decline"))) %>%
ggplot(aes(x = day, group = Items)) +
geom_ribbon(aes(ymin=Sales, ymax=cross_effect, fill = Sales - cross_effect > 0), fill = "powderblue")+
geom_line(aes(y = Sales, colour = interaction(Items, cross_eff_gp)), size = 0.25) +
geom_line(aes(y = cross_effect), size = 0.25, linetype="twodash", colour = "#F8766D") +
facet_wrap(~ Items, scales = 'free', nrow = 2) +
scale_y_continuous(breaks = my_breaks, labels = my_labels) +
scale_fill_manual(values=c("turquoise4","green1","turquoise4","darkgreen"))
structure(list(day = structure(c(1546300800, 1547078400, 1554850800,
1554937200, 1555023600, 1555110000, 1555196400, 1555282800, 1555369200,
1555455600, 1555542000, 1555628400, 1547164800, 1555714800, 1555801200,
1555887600, 1555974000, 1556060400, 1556146800, 1556233200, 1556319600,
1556406000, 1556492400, 1547251200, 1556578800, 1556665200, 1556751600,
1556838000, 1556924400, 1557010800, 1557097200, 1557183600, 1557270000,
1557356400, 1547337600, 1557442800, 1557529200, 1557615600, 1557702000,
1557788400, 1557874800, 1557961200, 1558047600, 1558134000, 1558220400,
1547424000, 1558306800, 1558393200, 1558479600, 1558566000, 1558652400,
1558738800, 1558825200, 1558911600, 1558998000, 1559084400, 1547510400,
1559170800, 1559257200, 1559343600), tzone = "", class = c("POSIXct",
"POSIXt")), rowname = c("1", "10", "100", "101", "102", "103",
"104", "105", "106", "107", "108", "109", "11", "110", "111",
"112", "113", "114", "115", "116", "117", "118", "119", "12",
"120", "121", "122", "123", "124", "125", "126", "127", "128",
"129", "13", "130", "131", "132", "133", "134", "135", "136",
"137", "138", "139", "14", "140", "141", "142", "143", "144",
"145", "146", "147", "148", "149", "15", "150", "151", "152"),
Items = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L), .Label = c("ITEMA", "ITEMB"), class = "factor"),
Sales = c(344L, 250L, 316L, 187L, 196L, 151L, 236L, 170L,
137L, 231L, 258L, 194L, 327L, 300L, 144L, 240L, 196L, 222L,
292L, 215L, 241L, 210L, 156L, 408L, 151L, 137L, 168L, 172L,
203L, 186L, 197L, 148L, 154L, 139L, 363L, 136L, 183L, 177L,
130L, 141L, 132L, 144L, 143L, 152L, 165L, 402L, 128L, 133L,
163L, 113L, 164L, 171L, 162L, 116L, 117L, 128L, 432L, 107L,
125L, 187L), z_diff_HC = c(15.5644333787362, 1.87906641317019,
12.557755209854, 14.8812985333839, 17.5223825569917, 23.8360074736518,
18.7410953048883, 14.9583507793006, 13.9273511861036, -3.51818494122973,
12.1544583602306, 3.02440323681235, 3.82962965039171, 17.5478815812969,
3.3229377520704, 0.633981373873377, 9.27905096386199, 12.5799626114306,
14.2247591638052, 16.6487747981391, 15.8304549924645, 9.15979751528926,
15.0129291111416, -0.688334784614703, -7.63226366995004,
0.428951331549642, 10.2702773598704, 36.1219319585148, 15.4142833246758,
4.88470834307489, 21.2490429809954, -7.33361550396995, -4.29869918645501,
18.9489364038734, -1.76460720804452, -1.35823311958288, 34.6935883900468,
1.48949451885331, -0.787584852115686, -4.934382699854, 29.0731805864111,
-2.26200238484375, 4.65925660604302, 4.57357539667069, 13.5774688780233,
-7.43056606661547, 6.80825163894218, 26.2460480670466, -0.0345819873156576,
-4.12627881544668, 21.9890241216193, 8.85020652256215, -4.58203685844702,
-1.56753295765628, 6.87606203426416, -1.24701222724131, -5.49773520072737,
26.6408805552948, 13.7954350138116, 2.38997658321286), z_diff_LC = c(13.3437666668954,
14.5236741281306, 10.7951593816903, 4.84956568132256, 7.45825970577198,
21.9209205048645, 13.771397911861, 7.45865297041878, 6.46080539304303,
5.14672039650974, 12.5316920559886, 15.2040992604187, 12.6541200645773,
14.2950716064143, 10.5446098231127, 6.43989960683895, 16.4096578863693,
16.4101967481371, 16.0975590010918, 12.6720845665324, 16.1643139814777,
11.6413490880193, 2.83465891620418, 10.8460318787892, 9.15035156783542,
10.2708678061698, 14.1911274394713, 23.0683580892434, 16.8380931420443,
4.8403754007098, 7.18799162588475, 6.36638381655323, 8.24158256116018,
11.3007628036994, 12.9255831702492, 6.41222412825569, 19.7883919842208,
13.3191292882506, 12.7806778770094, 4.86388566858485, 14.6793005237244,
2.42776728708029, 7.3976730645803, 12.0699413784232, 6.69843620821456,
12.048529261754, -0.949761782343618, 24.7048534238846, 7.06724785291648,
1.66908179491139, -0.863601498137782, 11.8756265308355, 11.5238370520726,
4.17572476604349, 10.6038524757556, 11.5103217175066, 11.4243857521004,
1.35708802651482, 1.5516675954511, 13.6221661122224), cross_effect = c(389.90255733412,
286.309185320327, 350.112703646141, 196.068687824073, 210.618189023313,
184.100589962345, 268.500499071992, 182.679710049712, 145.851303388469,
242.888924115937, 290.331765504451, 223.495952565212, 368.378972611168,
342.885214819243, 159.184238145282, 255.455759056413, 228.162929457284,
258.430636780864, 339.004872283188, 242.244981818045, 279.955996695361,
234.44683308484, 160.422067909279, 452.25181006546, 164.817030867431,
151.071088894453, 191.841094098312, 211.677575913499, 237.18132907835,
195.00309824532, 211.160343502993, 157.422248048499, 166.692037144187,
154.708060297142, 409.919866908005, 144.720624814428, 219.212757331124,
200.574858840204, 146.614881240112, 147.858078792705, 151.376676691316,
147.495984893396, 153.57867248235, 170.346310895203, 176.052419743554,
450.435087632251, 126.7843049186, 165.857455053767, 174.519614000254,
114.88606242825, 162.583693543054, 191.307321367729, 180.668616024358,
120.84384072861, 129.406507396634, 142.733211798408, 481.353346449074,
108.452084188371, 126.939584494314, 212.473450629856)), row.names = c(NA,
-60L), key = structure(list(rowname = c("1", "10", "100", "101",
"102", "103", "104", "105", "106", "107", "108", "109", "11",
"110", "111", "112", "113", "114", "115", "116", "117", "118",
"119", "12", "120", "121", "122", "123", "124", "125", "126",
"127", "128", "129", "13", "130", "131", "132", "133", "134",
"135", "136", "137", "138", "139", "14", "140", "141", "142",
"143", "144", "145", "146", "147", "148", "149", "15", "150",
"151", "152"), .rows = structure(list(1L, 2L, 3L, 4L, 5L, 6L,
7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L,
19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L,
31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L, 39L, 40L, 41L, 42L,
43L, 44L, 45L, 46L, 47L, 48L, 49L, 50L, 51L, 52L, 53L, 54L,
55L, 56L, 57L, 58L, 59L, 60L), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), row.names = c(NA, 60L), class = c("tbl_df",
"tbl", "data.frame"), .drop = TRUE), index = structure("day", ordered = TRUE), index2 = "day", interval = structure(list(
year = 0, quarter = 0, month = 0, week = 0, day = 0, hour = 1,
minute = 0, second = 0, millisecond = 0, microsecond = 0,
nanosecond = 0, unit = 0), .regular = TRUE, class = c("interval",
"vctrs_rcrd", "vctrs_vctr")), class = c("tbl_ts", "tbl_df", "tbl",
"data.frame"))