ggplot 中条形的排序
我浏览了这个论坛的答案,但似乎找不到这个特定问题的答案。我有以下数据,想要创建一个条形图,其中条形按照“值”从最大到最小排序,而不是按字母顺序排列:
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)
然后是基本条形图:
c <- ggplot(breadth_data, aes(x=Stakeholder, y=Value))
c + geom_bar(stat="identity") + coord_flip() + scale_y_continuous('') + scale_x_discrete('')
我尝试了许多不同的重新排序我在 StackOverflow 上看到过一些转换,但我似乎找不到一个有效的。我确信这相当简单,但我将不胜感激任何帮助!
谢谢,
格雷格
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您想要函数
reorder()
:给出:
如果您想要其他方式圆形,一种简单的方法是在
reorder()
调用中对Value
使用order()
:You want function
reorder()
:Which gives:
If you want them the other way round, an easy way is just to use
order()
onValue
inside thereorder()
call:另一种选择是使用
fct_reorder
函数href="https://forcats.tidyverse.org" rel="nofollow noreferrer">forcats
包。这是一个可重现的示例:使用
fct_rev
反转顺序:创建于 2022 年 10 月 28 日,使用 reprex v2.0.2
Another option could be using the
fct_reorder
function fromforcats
package. Here is a reproducible example:Use
fct_rev
for reversing order:Created on 2022-10-28 with reprex v2.0.2