0

all.

I need to plot selected one. I can plot all, but I can not fine how to plot what I select.

for example ...

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)

first of all, I made simple tiny dataset. and convert tsibble object. (my data is times series.)

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

and I use aggregate_key()function to make hierarchical time-series.

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

then.. plot it using autoplot() function plus facet_wrap()

the results shows 6 plots (aggregated + 5 categories), I need to plot selected category (among A to E) only.

thanks, all.

..updated

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)

just temporary.. using filter() function, I can plot what I selected. but it looks not good. I think there is more elegant way to handle it.

sean ahn
  • 11
  • 2
  • 1
    To be clear, which is the selected category you're referring to? The aggregated category only? – Desmond Dec 17 '21 at 08:57
  • @Desmond selected category. in case of this example, I would like to plot selected one among category A ~ E. NOT aggregate one. thanks! – sean ahn Dec 17 '21 at 09:38

1 Answers1

1
date <- rep(as.Date('2021-01-01') + 0:4, 5)
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)

tibble(date, category, product, value)|>
  tsibble(key = category, index = date)|>
  filter(category == "A" | category == "C" | category == "E") |> 
  autoplot(value) + facet_wrap(~ category, scales = "free_y" , ncol = 2)

Like this? :-)

Isaiah
  • 2,091
  • 3
  • 19
  • 28