簡體   English   中英

如何繪制圖表以比較 R 中兩個不同數據集的數據

[英]How to draw a graph for comparing data from two different datasets in R

有2個數據集

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是分組變量。 forecast列在月份預測( ds列)。 Fact是真實的數據。 按組分別執行的每個預測(子列)

也是第二個數據集

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))

在這個數據集中相同的結構,但這里有列manager_forecast這是經理認為會賣出多少件。

因此,對於每個組(子),我需要繪制我預測將售出多少件與實際售出多少件(數據集預測)和經理假設(manager_forecast)之間的相關性圖表

我想為每個子得到類似這張圖的東西(我在 excel 中做了這個) 在此處輸入圖像描述

每個子獲取此類圖形並將其保存到C: / 1 / plots文件夾的最簡單方法是什么。

感謝您的幫助

您需要根據子和日期合並兩個數據幀,然后將 pivot 合並為長格式。 繪圖非常簡單,按子分面,並進行一些主題調整以獲得與 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')

路徑/到/我的/plot.png

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM