r - facet_grid 并按 y 值重新排序列
问题描述
我有一个包含国名、大陆、年份和一些福利指标变量的数据框。
country year infant_mortality life_expectancy fertility population
1 Albania 1960 115.40 62.87 6.19 1636054
2 Algeria 1960 148.20 47.50 7.65 11124892
3 Angola 1960 208.00 35.98 7.32 5270844
4 Antigua and Barbuda 1960 NA 62.97 4.43 54681
5 Argentina 1960 59.87 65.39 3.11 20619075
6 Armenia 1960 NA 66.86 4.55 1867396
gdp continent region
1 NA Europe Southern Europe
2 13828152297 Africa Northern Africa
3 NA Africa Middle Africa
4 NA Americas Caribbean
5 108322326649 Americas South America
6 NA Asia Western Asia
我想绘制每个非洲国家在 1970 年和 2010 年的预期寿命,并按升序重新排列每一年的列:这个想法是看看排名是否发生了变化。
这就是我所做的。
gapminder %>% dplyr::filter(continent %in% "Africa" & year %in% c(1970, 2010)) %>%
mutate(country = reorder(country, life_expectancy, FUN = median)) %>%
ggplot(aes(x= country, y = life_expectancy, fill = region)) +
geom_col() +
facet_grid(.~year) +
theme(axis.text.x = element_text(angle = 90, hjust =1), legend.position = "none")
如何重新排序每个方面的因子水平?
解决方案
推荐阅读
- r - 将数字四舍五入到最接近的数字,所有数字均为零,但第一位数字
- jenkins - Jenkins multibranch - 如何扫描 jenkinsfile 放置在子模块上的分支?
- android - 从 com.crashlytics.sdk.android:crashlytics:2.9.8 更新到 2.9.9 时出现 Crashlytics 问题
- if-statement - 使用 IF 语句连接字符串和数字
- php - 如何从大型语料库中找到每个单词的词频?
- c# - 如何在 gac 中查询程序集文件
- reactjs - TypeScript - 是否可以根据模式匹配甚至长度验证字符串类型?
- azure - 从 arm 模板向 DSC 传递凭证
- java - 如何从非方形图像文件创建非方形 BufferedImage?
- sql - 累积值直到最新