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
输出:以错误的顺序绘制y轴
如何反转"Sektion"轴的顺序?我想有一个在顶部,8个在底部.
我尝试过,根据groupA$Date <- factor(groupA$Date, levels=rev(unique(groupA$Date)))
:
Sekplot <- last_plot() + coord_flip() + scale_x_reverse()
在几种味道,但找不到正确的方法.
您可以scale_x_discrete
使用limits
参数添加来执行此操作.您可以按照您想要的顺序写出限制,但是当您有许多因子级别时,这会变得复杂.相反,您可以从数据集中提取因子的级别,并利用rev
它们以相反的顺序放置它们.
它看起来像:
scale_x_discrete(limits = rev(levels(dbv$Sektion)))