R 刻面网格和按y值对列重新排序
我有一个包含国家名称、大陆、年份和一些福利指标变量的数据框架R 刻面网格和按y值对列重新排序,r,ggplot2,facet,R,Ggplot2,Facet,我有一个包含国家名称、大陆、年份和一些福利指标变量的数据框架 country year infant_mortality life_expectancy fertility population 1 Albania 1960 115.40 62.87 6.19 1636054 2 Algeria 1960 148.20 47.
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")
我如何为每个方面重新排序因子的级别?也许这会有所帮助?这里还有一个关于如何进行排序的教程