订购堆叠条形图 ggplot2 的 'fill'

Ordering the 'fill' of a stacked bar chart ggplot2

有没有一种方法可以让我通过变量 The_Number_Of_Evaders 来排序堆叠条形图,而不必手动输入我想要的级别?

The_Number_Of_Evaders <- c(287, 76, 237, 44, 249, 149, 3)

The_Results_Of_Using_The_Line <- c('Allied servicemen reached Spain through the Pyrenees', 'Civilians reached Spain through the Pyrenees',
 'Airmen who were arrested during their evasion', 'Airmen who were handed over to other evasion lines',
 'Airmen who were kept in camps of the "Operation Marathon" in France and Belgium', 
 'Airmen who were hidden by their lodgers until Liberation', 
 'Airmen who were killed during their evasion')

Line <- rep("Comete", 7)

Comete_Line <- data.frame(The_Number_Of_Evaders, The_Results_Of_Using_The_Line, Line)

Comete_Line_Ordered <- Comete_Line[order(Comete_Line$The_Number_Of_Evaders),]

Comete_Line_Ordered %>%
  ggplot(aes(x = Line, y = The_Number_Of_Evaders, fill = The_Results_Of_Using_The_Line)) + 
  geom_bar(stat = "identity", width = 0.5)

尝试其中一个选项。您可以将 reorder()fill:

一起使用
library(ggplot2)
library(dplyr)
#Code 1
Comete_Line %>% ggplot(aes(x = Line,
                           y = The_Number_Of_Evaders,
                           fill = reorder(The_Results_Of_Using_The_Line,-The_Number_Of_Evaders))) +
  geom_bar(stat = "identity", width = 0.5)+
  labs(fill='Var')

输出:

或者这样:

#Code 2
Comete_Line %>% ggplot(aes(x = Line,
                           y = The_Number_Of_Evaders,
                           fill = reorder(The_Results_Of_Using_The_Line,The_Number_Of_Evaders))) +
  geom_bar(stat = "identity", width = 0.5)+
  labs(fill='Var')

输出: