У меня проблемы с записью моего фрейма данных по месяцам, поэтому я могу правильно построить график сезонности в r.
Я попробовал несколько подходов, чтобы исправить это, и это, похоже, не работает. Вот моя таблица, которая показывает правильный порядок месяцев в году:
structure(list(Month = structure(c(5L, 5L, 5L, 5L, 4L, 4L, 4L,
4L, 8L, 8L, 8L, 8L, 1L, 1L, 1L, 1L, 9L, 9L, 9L, 9L, 7L, 7L, 7L,
7L, 6L, 6L, 6L, 6L, 2L, 2L, 2L, 2L, 12L, 12L, 12L, 12L, 11L,
11L, 11L, 11L, 10L, 10L, 10L, 10L, 3L, 3L), .Label = c("Apr",
"Aug", "Dec", "Feb", "Jan", "Jul", "Jun", "Mar", "May", "Nov",
"Oct", "Sep"), class = "factor"), Year = structure(c(1L, 1L,
2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L,
2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L,
2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L), .Label = c("2018",
"2019"), class = "factor"), Item = structure(c(1L, 2L, 1L, 2L,
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L,
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L,
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L), .Label = c("Beverage",
"Food"), class = "factor"), sum_units_sold = c(54488, 65842,
73503, 82874, 78975, 93826, 75098, 85470, 108831, 133739, 87469,
96005, 74372, 93526, 85655, 95713, 101783, 117764, 112107, 119282,
79951, 97568, 87642, 91912, 76637, 88947, 85650, 96090, 105557,
126821, 116862, 125077, 80087, 91296, 86279, 96559, 29156, 31628,
114781, 123999, 31582, 32427, 92046, 92895, 28474, 31471), sum_of_sales = c(84115.75,
90219.05, 127423.85, 116271.75, 126889.55, 129136.55, 129551.2,
121963, 182407.25, 185782.95, 152122.75, 137368.35, 124589.3,
130210.6, 148284.65, 137323, 171008.7, 166117.95, 194131.2, 171148,
135850.35, 137817.45, 151609.75, 131792, 130555.55, 125183.4,
149413.55, 137055.25, 179726.75, 179377.5, 206204.25, 178044,
136823.25, 129580.25, 153166.95, 137723.25, 49609.6, 44440.25,
202933.55, 176673.9, 54555.2, 45730.5, 162333.7, 132356.4, 49633.3,
44106.5), Products_available_for_sale_by_machine = c(13731, 18588,
21108, 20900, 13741, 18602, 21566, 21130, 14668, 19982, 22593,
20501, 14717, 19622, 22548, 20598, 15224, 19986, 23130, 20254,
15530, 20723, 23557, 20692, 15820, 21085, 23583, 21167, 17669,
21435, 26348, 21670, 20030, 21576, 26416, 22102, 6956, 7454,
26657, 22299, 7072, 7256, 25791, 22051, 7189, 7319), sum_of_spoils = c(6617,
8156, 14364, 7497, 7422, 7288, 14915, 6997, 8854, 7527, 13955,
4998, 9660, 11205, 13574, 4106, 22536, 15419, 14060, 7520, 11968,
10748, 14609, 4810, 11545, 8851, 10700, 4126, 18379, 11758, 17260,
4631, 13737, 9101, 12971, 4686, 4053, 2194, 15454, 6560, 5008,
1932, 10398, 5267, 4957, 2701), Op_days = c(18, 18, 23, 23, 20,
20, 20, 20, 25, 25, 20, 20, 19, 19, 19, 19, 24, 24, 24, 24, 20,
20, 20, 20, 19, 19, 18, 18, 25, 25, 25, 25, 19, 19, 19, 19, 20,
20, 20, 20, 23, 23, 23, 23, 19, 19), spoils_per_day = c(367.6,
453.1, 624.5, 326, 371.1, 364.4, 745.8, 349.9, 354.2, 301.1,
697.8, 249.9, 508.4, 589.7, 714.4, 216.1, 939, 642.5, 585.8,
313.3, 598.4, 537.4, 730.5, 240.5, 607.6, 465.8, 594.4, 229.2,
735.2, 470.3, 690.4, 185.2, 723, 479, 682.7, 246.6, 202.7, 109.7,
772.7, 328, 217.7, 84, 452.1, 229, 260.9, 142.2), sales_per_day = c(4673.1,
5012.2, 5540.2, 5055.3, 6344.5, 6456.8, 6477.6, 6098.1, 7296.3,
7431.3, 7606.1, 6868.4, 6557.3, 6853.2, 7804.5, 7227.5, 7125.4,
6921.6, 8088.8, 7131.2, 6792.5, 6890.9, 7580.5, 6589.6, 6871.3,
6588.6, 8300.8, 7614.2, 7189.1, 7175.1, 8248.2, 7121.8, 7201.2,
6820, 8061.4, 7248.6, 2480.5, 2222, 10146.7, 8833.7, 2372, 1988.3,
7058, 5754.6, 2612.3, 2321.4), products_available_per_day = c(762.833333333333,
1032.66666666667, 917.739130434783, 908.695652173913, 687.05,
930.1, 1078.3, 1056.5, 586.72, 799.28, 1129.65, 1025.05, 774.578947368421,
1032.73684210526, 1186.73684210526, 1084.10526315789, 634.333333333333,
832.75, 963.75, 843.916666666667, 776.5, 1036.15, 1177.85, 1034.6,
832.631578947368, 1109.73684210526, 1310.16666666667, 1175.94444444444,
706.76, 857.4, 1053.92, 866.8, 1054.21052631579, 1135.57894736842,
1390.31578947368, 1163.26315789474, 347.8, 372.7, 1332.85, 1114.95,
307.478260869565, 315.478260869565, 1121.34782608696, 958.739130434783,
378.368421052632, 385.210526315789), units_sold_per_day = c(3027.1,
3657.9, 3195.8, 3603.2, 3948.8, 4691.3, 3754.9, 4273.5, 4353.2,
5349.6, 4373.4, 4800.2, 3914.3, 4922.4, 4508.2, 5037.5, 4241,
4906.8, 4671.1, 4970.1, 3997.6, 4878.4, 4382.1, 4595.6, 4033.5,
4681.4, 4758.3, 5338.3, 4222.3, 5072.8, 4674.5, 5003.1, 4215.1,
4805.1, 4541, 5082.1, 1457.8, 1581.4, 5739.1, 6199.9, 1373.1,
1409.9, 4002, 4038.9, 1498.6, 1656.4), total_items = c(61105,
73998, 87867, 90371, 86397, 101114, 90013, 92467, 117685, 141266,
101424, 101003, 84032, 104731, 99229, 99819, 124319, 133183,
126167, 126802, 91919, 108316, 102251, 96722, 88182, 97798, 96350,
100216, 123936, 138579, 134122, 129708, 93824, 100397, 99250,
101245, 33209, 33822, 130235, 130559, 36590, 34359, 102444, 98162,
33431, 34172), percent_spoiled = c(10.8, 11, 16.3, 8.3, 8.6,
7.2, 16.6, 7.6, 7.5, 5.3, 13.8, 4.9, 11.5, 10.7, 13.7, 4.1, 18.1,
11.6, 11.1, 5.9, 13, 9.9, 14.3, 5, 13.1, 9.1, 11.1, 4.1, 14.8,
8.5, 12.9, 3.6, 14.6, 9.1, 13.1, 4.6, 12.2, 6.5, 11.9, 5, 13.7,
5.6, 10.1, 5.4, 14.8, 7.9)), row.names = c(NA, -46L), .internal.selfref = <pointer: 0x000001e7209b1ef0>, groups = structure(list(
Month = structure(c(1L, 1L, 2L, 2L, 3L, 4L, 4L, 5L, 5L, 6L,
6L, 7L, 7L, 8L, 8L, 9L, 9L, 10L, 10L, 11L, 11L, 12L, 12L), .Label = c("Apr",
"Aug", "Dec", "Feb", "Jan", "Jul", "Jun", "Mar", "May", "Nov",
"Oct", "Sep"), class = "factor"), Year = structure(c(1L,
2L, 1L, 2L, 1L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L, 1L, 2L, 1L, 2L, 1L, 2L), .Label = c("2018", "2019"), class = "factor"),
.rows = list(13:14, 15:16, 29:30, 31:32, 45:46, 5:6, 7:8,
1:2, 3:4, 25:26, 27:28, 21:22, 23:24, 9:10, 11:12, 17:18,
19:20, 41:42, 43:44, 37:38, 39:40, 33:34, 35:36)), row.names = c(NA,
-23L), class = c("tbl_df", "tbl", "data.frame"), .drop = TRUE), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"))
head(df)
Month Year Item sum_units_sold sum_of_sales Products_availa~ sum_of_spoils Op_days spoils_per_day sales_per_day products_availa~ units_sold_per_~
<fct> <fct> <fct> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 Jan 2018 Beve~ 54488 84116. 13731 6617 18 368. 4673. 763. 3027.
2 Jan 2018 Food 65842 90219. 18588 8156 18 453. 5012. 1033. 3658.
3 Jan 2019 Beve~ 73503 127424. 21108 14364 23 624. 5540. 918. 3196.
4 Jan 2019 Food 82874 116272. 20900 7497 23 326 5055. 909. 3603.
5 Feb 2018 Beve~ 78975 126890. 13741 7422 20 371. 6344. 687. 3949.
6 Feb 2018 Food 93826 129137. 18602 7288 20 364. 6457. 930. 4691.
Однако, когда я строю график с помощью заговора - мой xaxis находится в неправильном порядке. Имейте в виду, что я форматирую эту таблицу из глобального формата данных, который я отформатировал. Это мой код для моего гистограммы с использованием plotly:
## Plotting ##
f <- list(
family = "Courier New, monospace",
size = 20,
color = "black"
)
x1 <- list(
title = "Month",
titlefont = f
)
y1 <- list(
title = "Total units sold",
titlefont = f
)
## Year Plot ##
seasonal_plotly1 <- plot_ly(type = 'bar', x = final_sum_units_by_month1$Month, y = final_sum_units_by_month1$units_sold_per_day, color = final_sum_units_by_month1$Year, colors = "Set1",
text = ~paste("Operating Days: ", final_sum_units_by_month1$Op_days, "Category: ", final_sum_units_by_month1$Item, "Total Sales: ", final_sum_units_by_month1$sum_of_sales, "Total Units Sold: ", final_sum_units_by_month1$sum_units_sold)) %>%
layout(xaxis = x1, yaxis = y1, paper_bgcolor='white')
seasonal_plotly1
Когда я строю график - все месяцы перепутаны (апр, август, De c, февраль, январь, июль и др. c ...). Любая помощь будет принята с благодарностью !!