Skip to content

Instantly share code, notes, and snippets.

@gkampolis
Created April 28, 2018 14:09
Show Gist options
  • Save gkampolis/0e647bd814a9f33903136358ec4a326d to your computer and use it in GitHub Desktop.
Save gkampolis/0e647bd814a9f33903136358ec4a326d to your computer and use it in GitHub Desktop.
library(gapminder)
library(tidyverse)
gapminder %<>% # note the reassignment...
select(country, year, continent, lifeExp) %>%
group_by(continent, country) %>%
mutate(le_delta = lifeExp - lag(lifeExp)) %>%
summarize(worst_le_delta = min(le_delta, na.rm = TRUE)) %>%
arrange(worst_le_delta) %>%
head(5)
# Arrange factors for an ordered plot:
gapminder$country <- factor(gapminder$country, levels = gapminder$country[order(gapminder$worst_le_delta)])
ggplot(data = gapminder, aes(country, worst_le_delta, fill = worst_le_delta)) +
geom_bar(stat = "identity") +
guides(fill = FALSE) +
labs(y = "Change in Life Expectancy (years)",
x = "Country",
caption = "Data from Gapminder",
title = "Greatest negative change in life expectancy",
subtitle = "All countries are located in Africa..."
) +
theme_bw()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment