繁体   English   中英

ggplot2中的订购位置“道奇”

[英]Ordering position “dodge” in ggplot2

似乎很简单,但我找不到解决方案。

names(AllCoursesReg)
[1] "name"   "Course" "Status"

我的密码

ggplot(AllCoursesReg, aes(Course, fill = Status)) + 
geom_bar(aes(order = Status), position = "dodge", colour = "black") + theme_bw()+
guides(fill = guide_legend(reverse = TRUE)) 

我只希望注册人位于左侧而不是右侧。 我已经尝试过订单,水平,因素,但它不起作用

谢谢你的帮助。

在此处输入图片说明

您必须决定一个factor的水平顺序。 这是来自?geom_bar的示例。

# example from ?geom_bar
ggplot(diamonds, aes(clarity, fill=cut)) + geom_bar(position="dodge")
# reorder cut using levels = rev(levels(cut))
ggplot(diamonds, aes(clarity, fill=factor(cut, levels = rev(levels(cut))))) + 
  geom_bar(position="dodge") + 
  scale_fill_discrete('cut') # change name back to cut

自从这个问题以来,ggplot已经更新,所以这是一个利用ggplot2的新功能的答案。

只需将position_dodge2(reverse = TRUE)添加到position属性。 使用OP的代码:

ggplot(AllCoursesReg, aes(Course, fill = Status)) + 
geom_bar(aes(order = Status), position=position_dodge2(reverse = TRUE), colour = "black") + theme_bw()+
guides(fill = guide_legend(reverse = TRUE)) 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM