简体   繁体   English

ggplot2中的订购位置“道奇”

[英]Ordering position “dodge” in ggplot2

Seems simple but i couldnt find a solution. 似乎很简单,但我找不到解决方案。

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

My Code 我的密码

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

I just want the Registrants to be on the left not on the right. 我只希望注册人位于左侧而不是右侧。 I have tried Order, level, factor, and it is not working 我已经尝试过订单,水平,因素,但它不起作用

Thanks for your help. 谢谢你的帮助。

在此处输入图片说明

You have to decide on the ordering of the levels of a factor . 您必须决定一个factor的水平顺序。 Here's an example from ?geom_bar . 这是来自?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 has been updated since this question, so here's an answer that utilises a new feature from ggplot2. 自从这个问题以来,ggplot已经更新,所以这是一个利用ggplot2的新功能的答案。

Just add position_dodge2(reverse = TRUE) to the position attribute. 只需将position_dodge2(reverse = TRUE)添加到position属性。 Using OP's code: 使用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