There are 2 dataset
Forecast=structure(list(sub = c("01.01.01 Automatic switches ARMAT", "01.01.01 Automatic switches ARMAT",
"01.01.01 Automatic switches ARMAT", "01.01.02 Automatic switches KARAT",
"01.01.02 Automatic switches KARAT", "01.01.02 Automatic switches KARAT"
), ds = c("01.05.2022", "01.06.2022", "01.07.2022", "01.05.2022",
"01.06.2022", "01.07.2022"), forecast = c(30696L, 30696L, 30696L,
1089910L, 1089910L, 1089910L), fct = c(22590L, 29243L, 30521L,
803535L, 1430316L, 1312260L)), class = "data.frame", row.names = c(NA,
-6L))
sub
is grouping variable. forecast
column is forecast on month(ds
column).Fact
is real data.
Each forecast performed by group separately(sub column)
Also second dataset
manager_forecast=structure(list(sud = c("01.01.01 Automatic switches ARMAT", "01.01.01 Automatic switches ARMAT",
"01.01.02 Automatic switches KARAT", "01.01.02 Automatic switches KARAT",
"", ""), ds = c("01.06.2022", "01.07.2022", "01.06.2022", "01.07.2022",
"", ""), manager_forecast = c(76011.04671, 113534.0746, 1802431.458,
1399089.059, NA, NA)), class = "data.frame", row.names = c(NA,
-6L))
In this dataset same structure ,but here there is column manager_forecast
this is how much pieces the manager thought would be sold.
Thus, for each group (sub) I need to draw a graph of correlations between how many pieces I predicted will be sold and how many were in fact (dataset forecast) and how much the manager assumed (manager_forecast)
I would like to get something like this graph for each sub (I did this in excel)
What is the easiest way for each sub to get such graphics and save them to the C: / 1 / plots
folder.
thanks for your help
CodePudding user response:
You need to merge your two data frames according to sub and date, then pivot into long format. Plotting is then fairly straightforward, faceting by sub, and some theme tweaks to get a similar look to the Excel plot:
library(tidyverse)
Forecast %>%
left_join(manager_forecast, by = c(sub = 'sud', ds = 'ds')) %>%
pivot_longer(forecast:manager_forecast) %>%
mutate(ds = lubridate::mdy(ds)) %>%
ggplot(aes(ds, value, color = name))
geom_line(size = 1)
scale_y_continuous(labels = scales::dollar, name = NULL)
scale_color_manual(values = c('dodgerblue', '#FF7820', 'gray95'), name = NULL)
facet_wrap(sub~., nrow = 2)
labs(x = 'Date')
theme_dark(base_size = 16)
theme(text = element_text(color = 'gray85'),
plot.background = element_rect(fill = '#303030'),
panel.background = element_rect(fill = '#303030'),
legend.position = 'top',
legend.background = element_blank(),
axis.text = element_text(color = 'gray85'),
legend.key = element_blank(),
panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank())
ggsave('path/to/my/plot.png')
path/to/my/plot.png