首页 > 解决方案 > 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")

如何重新排序每个方面的因子水平?

标签: rggplot2facet

解决方案


推荐阅读