I have a dataframe with country names, continent, year and some welfare indicators variables.
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
I would like to plot the life expectancy for each African country in 1970 and 2010 and re-order the columns for each year in ascending order: the idea is to see whether there has been a change in the ranking.
This is what I have done.
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")
How can I re-order the levels of the factor for each facet?