如何在 R 中更改条形图中条形的顺序?
可以通过按我们希望的顺序设置变量的等级来做到这一点。
示例
> data <- data.frame(Class=c("Highschool","Highschool","Graduate","Graduate", "Graduate","Graduate","Masters","Masters","Masters","PhD"))
按递减顺序设置等级
> data <- within(data, Class <- factor(Class, levels=names(sort(table(Class), decreasing=TRUE)))) > library(ggplot2) > ggplot(data, aes(x = Class)) + geom_bar()
按递增顺序设置等级
> data <- within(data, Class <- factor(Class, levels=names(sort(table(Class), decreasing=TRUE)))) > ggplot(data, aes(x = Class)) + geom_bar()
广告