在ggplot中排序条 [英] Ordering of bars in ggplot
问题描述
我已经浏览了这个论坛的答案,但似乎无法找到这个特定问题的答案。我有以下数据,并希望创建一个条形图,其中条形图按值从大到小排序,而不是按字母顺序排列:
breadth_data< - read.table(textConnection(利益相关者价值
'Grantseekers'0.90
'捐助者'0.89
'社区'0.55
'飓风救助基金'0.24
'媒体'0.19
'求职者0.12
'分支机构'0.10
'青年'0.09
'女性'0.02
'前董事会成员'0.01),header = TRUE)
然后基本的条形图:
c < - ggplot(breadth_data,aes(x =利益相关者,y =值))
c + geom_bar stat =identity)+ coord_flip()+ scale_y_continuous('')+ scale_x_discrete('')
我已经尝试了很多我在StackOverflow上看到的重新排序和转换,但我似乎无法找到一个可行的方法。我相信这很简单,但我会感谢任何帮助!
谢谢,
Greg < >
您想要函数 reorder()
:
breadth_data< - transform(breadth_data,
Stakeholder = reorder(Stakeholder,Value))
给出:
如果你想让他们反过来,一个简单的方法就是使用在
on reorder()
调用中的order() Value
breadth_data< - transform(breadth_data,
Stakeholder = reorder(Stakeholder,
order(Value,decrease = TRUE)) )
I have looked through the answers in this forum but cannot seem to find an answer to this specific problem. I have the following data and want to create a bar chart where the bars are ordered from largest to smallest in terms of "Value", rather than having them in alphabetical order:
breadth_data <- read.table(textConnection("Stakeholder Value
'Grantseekers' 0.90
'Donors' 0.89
'Community' 0.55
'Hurricane Relief Fund' 0.24
'Media' 0.19
'Employment Seekers' 0.12
'Affiliates' 0.10
'Youth' 0.09
'Women' 0.02
'Former Board Members' 0.01"), header=TRUE)
Then the basic bar chart:
c <- ggplot(breadth_data, aes(x=Stakeholder, y=Value))
c + geom_bar(stat="identity") + coord_flip() + scale_y_continuous('') + scale_x_discrete('')
I have tried many of the different reorderings and transformations I've seen on StackOverflow but I cannot seem to find one that works. I am sure this is fairly simple, but I would appreciate any help!
Thanks,
Greg
You want function reorder()
:
breadth_data <- transform(breadth_data,
Stakeholder = reorder(Stakeholder, Value))
Which gives:
If you want them the other way round, an easy way is just to use order()
on Value
inside the reorder()
call:
breadth_data <- transform(breadth_data,
Stakeholder = reorder(Stakeholder,
order(Value, decreasing = TRUE)))
这篇关于在ggplot中排序条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!