ggplot更改barplot中的堆栈

时间:2018-12-04 22:22:53

标签: r

我有以下绘图,需要在其中使蓝色条(失败)到达顶部。我尝试使用order,但未进行预期的更改。订单有什么问题?

ggplot(a, aes(fill=Var1, y=value, x=Var2, order("pass","fail"))) 
       +geom_bar( stat="identity", position="fill") + labs(x = "Subject", y="Pass/Fail Percentage") 
       + guides(fill=guide_legend(title="Result"))

enter image description here

这是我的数据

Var1    Var2    value
pass    Maths   865     
fail    Maths   135     
pass    Reading 910     
fail    Reading 90      
pass    Writing 886     
fail    Writing 114

1 个答案:

答案 0 :(得分:0)

我建议像这样重新调整因子。

a %>% mutate(Var1 = factor(Var1, levels = c("fail", "pass"))) %>%
  ggplot(aes(fill=Var1, y=value, x=Var2)) + 
  geom_bar( stat="identity", position="fill") + labs(x = "Subject", y="Pass/Fail Percentage") + 
  guides(fill=guide_legend(title="Result"))

enter image description here