通过随时间变化的第二个变量对堆积条形图进行排序

Ordering a stacked bar graph by second variable changing over time

我查看了很多答案 (, , here),但其中 none 得到了我想要的结果。随着时间的推移,我有一个行业数量的数据集。我想要每个月按数量订购的堆叠条。这意味着如果该月内的相对量发生变化,则每个月的堆积条应该有不同的顺序。

以下是数据的截断示例:

test <- structure(list(Date = structure(c(18506, 18506, 18506, 18506, 
18506, 18506, 18536, 18536, 18536, 18536, 18536, 18536, 18567, 
18567, 18567, 18567, 18567, 18567), class = "Date"), Industry = c("Investment", 
"Telecoms", "Mortgage & Loans", "Banking", "Insurance", "Credit Cards", 
"Telecoms", "Investment", "Mortgage & Loans", "Banking", "Credit Cards", 
"Insurance", "Investment", "Telecoms", "Mortgage & Loans", "Credit Cards", 
"Insurance", "Banking"), volume = c(775349, 811294, 3144684, 
4427814, 7062691, 9377254, 1210194, 1735033, 3539406, 6952688, 
8858649, 9076391, 670934, 869452, 3542294, 5132132, 6953113, 
6954535)), row.names = c(NA, -18L), groups = structure(list(Date = structure(c(18506, 
18536, 18567), class = "Date"), .rows = structure(list(1:6, 7:12, 
    13:18), ptype = integer(0), class = c("vctrs_list_of", "vctrs_vctr", 
"list"))), row.names = c(NA, -3L), class = c("tbl_df", "tbl", 
"data.frame"), .drop = TRUE), class = c("grouped_df", "tbl_df", 
"tbl", "data.frame"))

这是到目前为止的图表:

#A. Library
library(dplyr)
library(ggplot)
library(ggtext)
library(scales)

#B. Graph
graph <- test %>%
    
    ggplot(aes(x=Date)) +
    
    ##1. Bar graph
    geom_bar(aes(x=Date, y=volume, fill = Industry), stat="identity") +
    
    ##2. Graph title and Axis labels
    ggtitle(label = "**Volume**",
            subtitle = "By Industry") +
    ylab("Volume (Millions)") + 
    xlab("") +
    
    ##3. Scales
    scale_fill_manual(values=c("#e3120b", "#336666", "#FB9851", "#acc8d4", 
                               "#dbcc98", "#36E2BD")) +
    scale_x_date(date_breaks = "month", labels = scales::label_date_short()) +
    scale_y_continuous(labels = unit_format(unit = "M", scale = 1e-6, 
                                            accuracy = 1)) + 
    
    #4. Theme
    guides(col = guide_legend(ncol = 2, nrow = 3)) +
    theme_minimal() +
    theme(text = element_text(family = "Georgia"),
          panel.border=element_blank(), 
          axis.line=element_line(), 
          plot.title = element_markdown(color="black", size=14, hjust = .5),
          plot.subtitle = element_text(hjust = .5),
          axis.title.x = element_text(size = 9, color = "grey30"), 
          axis.title.y = element_text(size = 9, color = "grey30"), 
          legend.box.background = element_rect(color="black", size=.5),
          legend.title = element_blank(),
          legend.text = element_text(size = 6),
          legend.position = "bottom",
          strip.background = element_rect(linetype="solid",),
          panel.grid.minor.y = element_line(color = NA),
          panel.grid.minor.x = element_line(color = NA),
          plot.caption = ggtext::element_markdown(hjust = 1, size = 7, 
                                                  color = "#7B7D7D"))  

据我了解,ggplot 按因子顺序对堆叠条形图进行排序。我尝试了 test %>% arrange(Date, volume),但后来陷入了如何按月更改因素而不仅仅是因素的静态顺序的问题。我可以使用单独的因素为每个月创建一个单独的条形图,但如果我想在图表中添加多年,这会变得很麻烦。

感谢任何帮助!

我冒昧地将您的示例归结为基本要素。根据评论,我认为没有办法分别定义每个月的因子水平。但是您可以在一个函数中执行此操作,创建一个列表,并利用 ggplot 对象的列表字符。

这种方式是可扩展的,这意味着,无论你有多少个月,它都会保持相同的代码...:)

library(tidyverse)
library(lubridate)

test <- 
  test %>% 
  ## it's probably not necessary to order the data and 
  ## create the factor levels explicitly, but it gives more control
  arrange(Date) %>%
  mutate(year_mo = fct_inorder(paste(year(Date), month(Date), sep = "_")))

## split the new data by month and create different factor levels
ls_test <- 
  test %>%
  split(., .$year_mo) %>%
  map(function(x) {x$Industry <- fct_reorder(x$Industry, x$volume); x})

## make your geom_col list (geom_col is equivalent to geom_bar(stat= "identity")
ls_p_col <- map(ls_test, function(x){
  geom_col(data = x, mapping = aes(x=year_mo, y=volume, fill = Industry))
})

# Voilà!
ggplot() +
  ls_p_col +
  scale_fill_brewer() +
  scale_x_discrete(limits = unique(test$year_mo)) # to force the correct order of your x