如何在R中使用ggplot对双杠进行排序?

我正在学习r,我在按升序或降序对双杠进行排序时遇到问题,我想在图的顶部设置图例,用两种颜色分别表示一行和两列,例如:

The title Time

box color Breakfast box color Dinner

还有这里的情节

这是我的数据框:

dat <- data.frame(
  time = factor(c("Breakfast","Breakfast","Breakfast","Breakfast","Breakfast","Lunch","Lunch","Lunch","Lunch","Lunch","Lunch","Dinner","Dinner","Dinner","Dinner","Dinner","Dinner","Dinner"), levels=c("Breakfast","Lunch","Dinner")),
  class = c("a","a","b","b","c","a","b","b","c","c","c","a","a","b","b","b","c","c"))

这是我进行更改的代码:

dat %>% 
  filter(time %in% c("Breakfast", "Dinner")) %>%
  droplevels %>%
  count(time, class) %>% 
  group_by(time) %>% 
  mutate(prop = n/sum(n)) %>%
  ggplot(aes(x = class, y = prop, fill = time, label = scales::percent(prop))) +
  geom_col(position = 'dodge') +
  geom_text(position = position_dodge(width = 0.9), vjust = 0.5, size = 3) + 
  scale_y_continuous(labels = scales::percent)+
  coord_flip()

任何帮助,将不胜感激。