如何更改ggplot2中条形图中的堆叠顺序?显示如何反转堆叠顺序,但该解决方案还会更改图例中显示的顺序.我想更改堆叠顺序而不影响图例顺序,这样图例中的顶级类也是堆叠中的顶级类.
library(ggplot2)
data(mtcars)
ggplot(mtcars, aes(factor(cyl), fill=gear)) + geom_bar()
Run Code Online (Sandbox Code Playgroud)
您可以使用
scale_fill_discrete以下方式撤消图例订单:
Run Code Online (Sandbox Code Playgroud)
ggplot(mtcars, aes(factor(cyl), fill=gear)) + geom_bar() +
scale_fill_discrete(guide=guide_legend(reverse=T))