Sorting DataFrame for ggplot barplot [duplicate]

好久不见. 提交于 2019-12-08 13:55:41

问题


I have a data frame df1 and want to draw a barplot of AccountExecutive and their corresponding ClearRate where the bars are arranged so that it is decreasing from left to right.

I tried this code but the resulting graph still reflects AccountExecutive order as it appears in df1

ggplot(arrange(df1, -ClearRate), aes(x = AccountExecutive, y = ClearRate)) +
  geom_bar(stat="identity")

Can anyone help me correcting this code?

NOTE: Not a duplicate of the previous question because that one asks for an arbitrary positioning of the x axis labels. This question asks how to sort x-axis labels considering their y-axis values.


回答1:


Try this one the code below should reorder AE according to clearance rate

ggplot(df1,aes(x=reorder(AccountExecutive,-ClearRate),y=ClearRate))+geom_bar(stat"identity")

here is the more about reorder function Reorder bars in geom_bar ggplot2



来源:https://stackoverflow.com/questions/42184869/sorting-dataframe-for-ggplot-barplot

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!