Search code examples
rggplot2facet-wrap

Modify the plot - separate one categorical data vs the remaining in a facet wrap plot


I have a data set where my item_id column has categorical data, one of which is FOODS_1_012. It has sales figures which are significantly higher than the other categories, I want to separate it into it's own plot, while keeping rest of categories in its own plot within a facet wrap. Is this possible?

Here is my code:

#facet wrap of monthly sales by item and store.
grocery_tsibble %>%
  filter(state_id == "TX") %>% 
  group_by(time = month, store_id, item_id) %>% 
  summarise(sales=sum(sales)) %>% 
  ggplot(aes(x=as.Date(month), y=sales, color = item_id)) +
  geom_line() +
  facet_wrap(~store_id) +
  theme_clean() +
  ggtitle("Line Plot") +
  xlab("Year - Months") + ylab("Sales") +
  scale_x_date(date_breaks = "5 months", labels = scales::date_format(format = "%y-%m")) +
  scale_y_continuous(n.breaks = 10) +
  theme(legend.position="bottom",
        legend.text = element_text(size=7),
        legend.title = element_text(size=7, vjust = 0.7),
        legend.key.size = unit(0.5, 'cm'),
        axis.text.x = element_text(angle = 90, hjust = 1),
        panel.grid.major.x = element_line(color = "gray",
                                          size = 0.5,
                                          linetype = 3),
        plot.margin = margin(.35,.75,.35,.35, "cm"),
        plot.title = element_text(color="#6b5b5a", size=14,
                                  face="bold.italic",hjust = 0.5),
        axis.title.x = element_text(color="#6b5b5a", 
                                    size=10, 
                                    face="bold"),
        axis.title.y = element_text(color="#6b5b5a", 
                                    size=10, 
                                    face="bold"),
        strip.background =element_rect(fill="#494d54"),
        strip.text = element_text(colour = 'white'))

Here is the output, as you can see FOOD_1_012 is very high, so I want to separate it into its own plot and compare it within each store. So for example TX_1 and the rest etc. would have two plots under each of them. One is for FOOD_1_012 and the other plot has the remaining categorical data.

enter image description here


Solution

  • Perhaps you can start with this & see where it takes you:

    grocery_tsibble %>%
      filter(state_id == "TX") %>% 
      group_by(time = month, store_id, item_id) %>% 
      summarise(sales=sum(sales)) %>% 
      mutate(facet.sales = ifelse(item_id == "FOODS_1_012", 
                                  "High sales volume", 
                                  "Low sales volume")) %>%
      ggplot(aes(x=as.Date(month), y=sales, color = item_id)) +
      geom_line() +
      facet_grid(rows = vars(facet.sales),
                 cols = vars(store_id),
                 scales = "free_y") # + all the scale / theme specifications in your code