r - 使用 as.factor 时在 ggplot2 中重新排序条形图输出
问题描述
我希望有人能帮忙。
我有一个DF如下:
Year Winner
1930 Uruguay
1934 Italy
1938 Italy
1950 Uruguay
1954 Germany FR
1958 Brazil
1962 Brazil
1966 England
1970 Brazil
....
等等
我想要做的是用 ggplot2 创建一个条形图,但重新排序它,以便获胜者数量最多的国家排在第一位。
我用来生成当前图表的代码是:
ggplot(data, aes(x=as.factor(Winner), fill=as.factor(Winner) )) +
geom_bar() +
theme(legend.position = "none")
我知道有一些关于重新排序的事情,但我无法让它与 as.factor 参数一起工作。
谢谢
解决方案
我使用 forcats 解决了这个问题
require(forcats)
ggplot(data, aes(fct_infreq(Winner), fill=as.factor(Winner))) +
geom_bar()+
theme(legend.position = "none")
推荐阅读
- java - 如何在java中将双数组转换为短数组?
- python-3.x - Python 包装器/装饰器
- discord.js - Discord.js 12 语音频道无人时如何让bot离开?
- sql - SQL 选择列名,其中值大于零
- java - 使用 Checkstyle 强制执行精确的 4 空格缩进
- javascript - 运行while循环javascript时页面无响应
- javascript - React Redux - 映射数组,同时仅重新渲染包含更改对象的组件(在数组中)
- firebase - 参数类型“UserResult”不能分配给参数类型“UserSearch”
- javascript - 如果元素属于 A 类但不是 B 类,则悬停时 Jquery 调用函数
- javascript - 通过类名而不是 ID 获取元素