R ggplot在条形图中更改堆栈顺序
在下面的图中,我需要将蓝色条(失败)放到顶部。我尝试了R ggplot在条形图中更改堆栈顺序,r,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_lege
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"))
这是我的数据
Var1 Var2 value
pass Maths 865
fail Maths 135
pass Reading 910
fail Reading 90
pass Writing 886
fail Writing 114
我建议重新设置因子的级别,如下所示
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"))
你能发布你的数据集样本吗?@HarroCyranka添加的数据是你的Var1 a因子吗?我无法用数据重现错误。@HarroCyranka yes Val1、Val2是可能重复的因素