在R中的坐标翻转后,顺序颠倒

在R中的坐标翻转后,顺序颠倒,r,ggplot2,axes,R,Ggplot2,Axes,来自dbv的数据示例: gender Sektion 1 m 5 2 m 5 3 w 3B 4 w 3B 5 w 3B 6 m 4 我有以下情节: Sekplot <- ggplot(dbv,aes(x=Sektion, fill=factor(gender),

来自dbv的数据示例:

  gender Sektion
1      m       5
2      m       5
3      w      3B
4      w      3B
5      w      3B
6      m       4
我有以下情节:

Sekplot <- ggplot(dbv,aes(x=Sektion,
                          fill=factor(gender),
                          stat="bin", 
                          label = paste(round((..count..)/sum(..count..)*100), "%"))) 
Sekplot <- Sekplot + geom_bar(position="fill")
Sekplot <- Sekplot + scale_y_continuous(labels = percent)
Sekplot <- Sekplot + labs(title = "test")
Sekplot <- Sekplot + scale_fill_discrete(name="test", breaks=c("m", "w", "k.A."), labels=c("m", "w", "k.A."))
Sekplot <- Sekplot + geom_hline(aes(yintercept = ges, linetype = "test"), colour = "black", size = 0.75, show_guide = T)
Sekplot <- last_plot() + coord_flip()
Sekplot <- Sekplot + guides(colour = guide_legend(override.aes = list(linetype = 0 )), 
                                    fill = guide_legend(override.aes = list(linetype = 0 )), 
                                    shape = guide_legend(override.aes = list(linetype = 0 )), 
                                    linetype = guide_legend()) + theme(legend.title=element_blank())

Sekplot 

Sekplot您可以使用
limits
参数添加
scale\x\u discrete
。你可以简单地按照你想要的顺序写出限制,但是当你有很多因素水平时,这就变得复杂了。相反,您可以从数据集中提取因子的级别,并利用
rev
将它们按相反顺序排列

它看起来像:
scale\u x\u discrete(limits=rev(levels(dbv$Sektion)))
正如@sindri\u baldur建议的那样,在指定美学的同时,添加fct\u rev(),如
ggplot(dbv,aes(x=fct\u rev(Sektion),fill=factor(gender),stat=“bin”,label=paste(四舍五入(…count.)/sum(…count.)


这应该是你所需要的。。。从您的代码中删除
scale\u x\u discrete()

您需要类似
scale\u x\u discrete(limits=rev(levels(dat$Sektion))的东西。
-非常感谢,它非常有效!我不能将这个问题标记为已解决,因为答案在注释中。我没有将它作为答案,因为我试图找到重复的答案。不过,我还没有找到一个很好的匹配项,所以将添加为答案。
forcats::fct_rev()
在这里很有用。@jzadra这对我来说仍然适用于ggplot2 3.2.1。
Sekplot <- last_plot() + coord_flip() + scale_x_reverse()