我正在尝试根据“良好”百分比对 ggplot 进行排序。下面是我正在使用的数据框。还有我现在得到的以及我理想中想要拥有的。
library(ggplot2)
a <- c("Nevada", "Maine", "North Carolina", "South Carolina", "Texas", "Rhode Island",
"Nevada", "Maine", "North Carolina", "South Carolina", "Texas", "Rhode Island")
b <- c(0.81, 0.72, 0.65, 0.55, 0.45, 0.35, 0.19, 0.28, 0.35, 0.45, 0.55, 0.65)
d <- c("Good", "Good", "Good", "Good", "Good", "Good", "Bad", "Bad", "Bad", "Bad", "Bad", "Bad")
df <- data.frame(a,b,d)
names(df) <- c("State", "Percentage", "Condition")
ggplot(df, aes(x=State, y=Percentage, fill=Condition))+
geom_bar (position = "fill", stat="identity")+
coord_flip()
Run Code Online (Sandbox Code Playgroud)
我目前的结果是:。
当前堆叠条
理想情况下,我的结果会是这样的:
所需输出
我已阅读多个答案,但似乎没有任何效果。我认为我的数据表格式可能是问题的一部分,但是,我尝试了各种方法。任何指导表示赞赏。