我正在嘗試對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"))
uj5u.com熱心網友回復:
為了回答您的問題并檢查答案,我不得不擴展您的初始資料集,但它應該可以與您的原始資料一起正常作業。
我已經使用reorder ave重新排列條形圖。基本上,ave您想將 的值Excellent與的每個值相關聯Variable_name(即使Rating不是Excellent)。這樣就reorder知道Variable_name應該平等對待相同的值。replace如果您沒有任何Excellent價值,則是必要的:它只是用零替換您將擁有的 NA。請注意,我必須setNames,那是因為我必須選擇Rating_prop與Rating Excellent.
# 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 包( v2.0.0 )于 2021 年 11 月 11 日創建
我試圖簡化一下你的代碼:
- 我用
scales::percent而不是paste - 我使用
geom_col代替geom_bar, with 允許我洗掉stat = "identity"引數 - I removed
coord_flipand I simply switchedxandy
Also, as final touches:
- I added
scale_x_continuousso to have percentages on the x axis - I added a
theme
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/358916.html
