r语言 - 当柱状图表示出现次数时,如何对柱状图进行排序?



我在R中画了一个条形图:

ggplot(data, aes(x=rating, fill=rating)) +
geom_bar(stat="count") +
ggtitle("Rating in stories")+
coord_flip()+
xlab("rating")+
ylab("number of stories")+
theme(legend.position="none")

结果在这里。条形图表示特定值(M、T、K或K+)在评级变量中出现的次数。如何按递减顺序排序?

好了,我找到我要找的了。我需要在变量上使用fct_rev(fct_infreq())

ggplot(data, aes(forcats::fct_rev(fct_infreq(rating)), fill=rating)) +
geom_bar(stat = "count") +
ggtitle("Rating in stories")+
coord_flip()+
xlab("rating")+
ylab("number of stories")+
theme(legend.position="none")

最新更新