R 订购位置“;道奇;在ggplot2中
看起来很简单,但我找不到解决办法R 订购位置“;道奇;在ggplot2中,r,ggplot2,R,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 = TRU
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))
我只希望注册者在左边,而不是右边。
我尝试了顺序、级别、因子,但它不起作用
谢谢你的帮助
您必须决定
因子的级别顺序。下面是一个来自?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))
在应用它之后,我得到了这个。seq.default(h[1],h[2],length=n)中的错误:'to'不能是NA,NaN或无穷大。如果我制作的可复制示例不适合您的情况,您应该自己制作一个。否则我们无法诊断问题。我的示例完整且易于理解。这绝对是现在处理此问题的最佳方法,其他选项对我都不起作用。很好的解决方案。我不需要在我的代码中调用guides()
,订单仍然匹配。