0

全部。

我需要绘制选定的一个。我可以全部绘制,但我不能很好地绘制我选择的内容。

例如 ...

date <- as.Date('2021-01-01') + 0:4
category <- c(rep("A",5), rep("B",5), rep("C",5), rep("D",5), rep("E",5))
product <- c("A1","A2","A3","A4","A5",
             "B1","B2","B3","B4","B5",
             "C1","C2","C3","C4","C5",
             "D1","D2","D3","D4","D5",
             "E1","E2","E3","E4","E5")
value <- rnorm(25,10,1)
df <- data.frame(date, category, product, value)

首先,我制作了简单的小数据集。并转换 tsibble 对象。(我的数据是时间序列。)

df <- tsibble(df, key = c(category, product), index = date)
df_h <- df %>% aggregate_key(category / product, value = sum(value))

我使用aggregate_key()函数来制作分层时间序列。

df_h %>% autoplot(value) + facet_wrap(~ category, scales = "free_y")

然后.. 使用autoplot()函数 plus绘制它facet_wrap()

结果显示 6 个图(聚合 + 5 个类别),我只需要绘制所选类别(在 A 到 E 中)。

谢谢大家。

..更新

date <- as.Date('2021-01-01') + 0:4
category <- c(rep("A",5), rep("B",5), rep("C",5), rep("D",5), rep("E",5))
product <- c("A1","A2","A3","A4","A5",
             "B1","B2","B3","B4","B5",
             "C1","C2","C3","C4","C5",
             "D1","D2","D3","D4","D5",
             "E1","E2","E3","E4","E5")
value <- rnorm(25,10,1)


df <- data.frame(date, category, product, value)


df <- tsibble(df, key = c(category, product), index = date)


df_h <- df %>% aggregate_key(category / product, value = sum(value))


df_h %>% 
    filter(category == "A" | category == "C" | category == "E") %>% 
    autoplot(value) + facet_wrap(~ category, scales = "free_y" , ncol = 2)

只是暂时的..使用filter()功能,我可以绘制我选择的内容。但它看起来不太好。我认为有更优雅的方式来处理它。

4

0 回答 0