按特定评级的最高值对堆叠条形图进行排序

sorting stacked bars by highest value of certain rating

我正在尝试对 ggplot 创建的堆叠条形图对象进行排序,方法是让具有最高 "Excellent" 值的项目排在第一位。我当前的代码似乎不是这样排序的:

R_POPS_testdataset_forstackedbars <- read_csv("R-POPS testdataset forstackedbars.csv") # dataset for testing purposes

RPOPS_ggchart01 <- ggplot(R_POPS_testdataset_forstackedbars, aes(x = Variable_name, y = Rating_prop, fill = factor(Rating, levels=c("Poor","Fair","Good","Very Good","Excellent")))) + 
    geom_bar(stat="identity", position ="fill", width=0.8) + coord_flip() + scale_fill_manual("legend", values = c("Excellent" = "#275E6B", "Very Good" = "#4AAAC4", "Good" = "#8CD4E5", "Fair" = "#F7963D", "Poor" = "#BE2327")) # color scheme for stacked bars
RPOPS_ggchart01 <- RPOPS_ggchart01 + geom_text(aes(label=paste0(sprintf("%1.f", Rating_prop*100),"%")),
                                               position=position_fill(vjust=0.5), color="white", size=9)

上面的代码让我得到这个:

我的数据结构如下:

structure(list(Rating = c("Excellent", "Fair", "Good", "Poor", 
"Very Good", "Excellent", "Fair", "Good", "Poor", "Very Good"
), Variable_name = c("Overall effectiveness of adminstrative support for the program", 
"Overall effectiveness of adminstrative support for the program", 
"Overall effectiveness of adminstrative support for the program", 
"Overall effectiveness of adminstrative support for the program", 
"Overall effectiveness of adminstrative support for the program", 
"Overall effectiveness of your Program Director", "Overall effectiveness of your Program Director", 
"Overall effectiveness of your Program Director", "Overall effectiveness of your Program Director", 
"Overall effectiveness of your Program Director"), Rating_prop = c(0.13, 
0.35, 0.17, 0.13, 0.22, 0.39, 0.09, 0.26, 0.09, 0.17)), row.names = c(NA, 
-10L), class = c("tbl_df", "tbl", "data.frame"))

为了回答您的问题并检查答案,我必须扩展您的初始数据集,但它应该可以正常使用您的原始数据。

我已经使用 reorder + ave 对条形图重新排序。基本上,对于 ave,您希望将 Excellent 的值与 Variable_name 的每个值相关联(即使 Rating 不是 Excellent)。这样reorder就知道同一个Variable_name的值应该被平等对待。如果您没有 Excellent 值,则 replace 是必需的:它只是用零替换您将拥有的 NA。请注意,我必须 setNames,那是因为我必须 select 与 Rating Excellent.

关联的 Rating_prop
# data
set.seed(1)
df <- data.frame(Variable_name = rep(LETTERS[1:5], each = 5),
                 Rating = c("Poor","Fair","Good","Very Good","Excellent"),
                 Rating_prop = runif(25))
df$Rating_prop <- ave(df$Rating_prop, df$Variable_name, FUN = function(x) x / sum(x))

# library
library(ggplot2)

# solution
ggplot(df, 
       aes(x = Rating_prop,
           y = reorder(Variable_name, ave(setNames(Rating_prop, Rating), Variable_name, FUN = function(x) replace(x["Excellent"], is.na(x["Excellent"]), 0))), 
           fill = factor(Rating, levels=c("Poor","Fair","Good","Very Good","Excellent")))) + 
  geom_col(position ="fill", width = 0.8) +
  scale_fill_manual("legend", values = c("Excellent" = "#275E6B", "Very Good" = "#4AAAC4", "Good" = "#8CD4E5", "Fair" = "#F7963D", "Poor" = "#BE2327")) +
  geom_text(aes(label=scales::percent(Rating_prop, accuracy = 1)),
            position=position_fill(vjust=0.5), color="white", size=9) +
  labs(y = "Variable_name") +
  scale_x_continuous(labels = scales::percent) +
  theme_classic()

reprex package (v2.0.0)

于 2021-11-11 创建

我试着简化了你的代码:

  • 我用了 scales::percent 而不是 paste
  • 我使用 geom_col 而不是 geom_bar,允许我删除 stat = "identity" 参数
  • 我删除了 coord_flip 并简单地切换了 xy

此外,作为最后的润色:

  • 我添加了 scale_x_continuous 以便在 x 轴上显示百分比
  • 我加了一个theme