Faceting my time series graphs by category

I would like create three plots on the top of each other with a common axis (months-year) for each of my three clients NM, F, and NCF.

I would like only the completed part of my data.

I originally have daily data which I converted to monthly data. I would like to show my monthly times series graphs on the top of each other (one for each client). Later I will do this for my ggseasonal plot and other time series graphs. This will help my audience understand the patterns I am talking about for each of the clients and how they compare to each other.

Here is what my code looks like:

# Data 

status <- c('completed', 'completed', 'cancelled', 'completed', 'cancelled', 'completed', 'completed','completed', 'cancelled')
client <- c('NM','NM',  'F','F','F','NM' ,'NCF', 'NCF', 'F')
audit_date <- c('2017-01-01', '2017-01-03','2018-03-02', '2018-03-03', '2019-02-04', '2019-02-03', '2019-04-05',
'2019-04-05','2019-04-08')                              # Daily data to be converted to monthly data

d00 <- tibble(status, client, audit_date)

d11 <- d00

d11 <- d00 %>%
  mutate(year.month = substr(audit_date, 0 , 7)) %>%
  group_by(year.month, client) %>%
  summarize( total.audits = sum(status == 'completed'))

# Converting to time series    (NOT working)

d11.ts <- d11 %>%
  as_tsibble(index = year.month)                             


# Plot
# I want to have an autoplot for each of the clients. I can do this for each of the clients alone like below
# However to get the graph below, I separated my data set into three parts where each part deals with one of the three customers. 
# This is the graph for F. It won't show. 
# I would like this graph for each of the customers above (three plots but with the same axis)

comp_audits.F <- autoplot(ds.ts.F, color = 'red', geom = 'point') +
    date_labels = "%b %y",
    breaks = '1 month') +
    breaks = seq(min(ds.ts.F$total.audits), 
                 by = 100),
    limits = c(0, 700)
  ) +
  theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
  #  geom_point(col = 'green', size = 3, pch = 16) +
  # geom_smooth(method = 'loess', se = F) +
  labs(subtitle = 'subtitle',
       y = "CA",
       X = "Month-Year",
       title = "Time Series Plot for F") 


Thank you!

A tsibble must have one row for each combination of time index and key. In this example, the client is a key variable and the audit_date is a time stamp. As you want monthly data, you will need to convert audit_date to a yearmonth object for the time index


d00 <- tibble(
  status = c("completed", "completed", "cancelled", "completed", "cancelled",
             "completed", "completed", "completed", "cancelled"),
  client = c("NM", "NM", "F", "F", "F", "NM", "NCF", "NCF", "F"),
  audit_date = c("2017-01-01", "2017-01-03", "2018-03-02", "2018-03-03", 
                 "2019-02-04", "2019-02-03", "2019-04-05", "2019-04-05", 
d11 <- d00 %>%
  mutate(year.month = yearmonth(as.Date(audit_date))) %>%
  group_by(year.month, client) %>%
  summarize(total.audits = sum(status == "completed")) %>%
d11.ts <- d11 %>%
  as_tsibble(index = year.month, key = client) %>%
  fill_gaps(total.audits = 0)

The last line fills in any implicit gaps with 0s.

You say you want to use autoplot(), presumably the function from the feasts package designed for tsibble objects. However, that only produces line plots and you seem to want to use geom_point. So we will have to use ggplot() directly:

d11.ts %>%
  ggplot(aes(x = year.month, y = total.audits)) +
  geom_point() +
  facet_grid(client ~ .) +
  scale_x_yearmonth(breaks="1 month", date_labels="%b %y") +
  theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
  labs(subtitle = 'subtitle',
       y = "CA",
       x = "Month-Year",
       title = "Time Series Plot for F") 

Created on 2020-07-21 by the reprex package (v0.3.0)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.