Skip to content

Instantly share code, notes, and snippets.

@ranjiGT
Last active May 2, 2021 09:51
Show Gist options
  • Save ranjiGT/ae5d28ac816e68df66966340193a6036 to your computer and use it in GitHub Desktop.
Save ranjiGT/ae5d28ac816e68df66966340193a6036 to your computer and use it in GitHub Desktop.
A basic pipeline on gapminder data
my_gapminder <- gapminder %>%
# filter observations were the country is either Africa, Asia or Europe
filter(continent %in% c("Africa", "Asia", "Europe")) %>%
# compute mean country population per continent and year
group_by(continent, year) %>%
summarize(mean_pop = mean(pop)) %>%
ungroup() %>%
# compute population growth as relative difference to continent population
# in 1958
group_by(continent) %>%
mutate(rel_pop_growth = (mean_pop - mean_pop[1]) / mean_pop[1]) %>%
ungroup()
ggplot(my_gapminder, aes(x = year, y = rel_pop_growth, fill = rel_pop_growth)) +
# add barchart geom
geom_col() +
# divide data into subplots by continent
facet_wrap(~ continent, nrow = 1) +
# label y-axis text in percentage format
scale_y_continuous(labels = scales::percent) +
# add continuous fill color scale from gray to purple
scale_fill_continuous(low = "gray80", high = "purple4") +
# remove fill colorbar
guides(fill = FALSE) +
# add y-axis and plot titles
labs(x = NULL, y = "Avg. Population Growth",
title = "Avg. Population Growth per Continent, 1958-2007")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment