Skip to content

Instantly share code, notes, and snippets.

@dreidpath
Last active January 21, 2017 07:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dreidpath/40d6cf5381ac6269f5b5b53a5c1f7903 to your computer and use it in GitHub Desktop.
Save dreidpath/40d6cf5381ac6269f5b5b53a5c1f7903 to your computer and use it in GitHub Desktop.
Changes in country level life expectancy 1870-2016 based on data from Gapminder.org
######################################################################################################################################
# This is a small piece of R-Code to support a recent blog-post of mine on country-level inequalities in life expectancy. #
# If you are so inclined, you can read the blog-post here: http://wp.me/p8fOE1-13 #
# #
# For the purposes of re-use, the code is released under an MIT License (https://choosealicense.com/licenses/mit/) #
# The code is provided "as is" and without warranties. #
# #
####### Notes #
# 1. For the "How To" on accessing Gapminder data, look at David Comfort's post: http://bit.ly/2jXkclu #
# 2. I use the googlesheets package of Bryan and Zhao (2017) to extract the data #
# 3. For a great discussion of changes in countries' life expectancy over time see https://ourworldindata.org/life-expectancy/ #
# #
# #
# Daniel D Reidpath, 21 January 2017 #
######################################################################################################################################
library(googlesheets)
library(dplyr)
# indicator life_expectancy_at_birth -- Public Sheet
le_data_url <- "https://docs.google.com/spreadsheets/d/1H3nzTwbn8z4lJ5gJ_WfDgCeGEXK3PVGcNjQ_U5og8eo/pub?gid=0#"
# Access a Gapminder Google Sheet by the URL and get some information about the Google Sheet:
gs_url(le_data_url, lookup = FALSE)
le_data_key <- extract_key_from_url(le_data_url)
# Download the liofe expectancy data (quite slow)
data <- gs_key(le_data_key, verbose=FALSE, lookup = FALSE) %>% gs_read(ws = "Data", range = "A1:HJ261")
# Extract the life expectancy data into a data matrix. It simplifies the manipulation (at least in my head)
mat_data <- as.matrix(data[, 72:218]) # only take the data from 1870 to 2016
# Calculate the deciles of country life expectancy in each year
quantile_data <- apply(mat_data, 2, quantile, probs = seq(0.1, 0.9, 0.1), na.rm=T)
# Set up difference colors for each decile's line
quantile_col <- c("#0000FF", "#8989FF", "#BCBCFF", "#E1E1FF", "#FFFFFF", "#FFE1E1", "#FFBCBC", "#FF8989", "#FF0000")
### Plot the deciles of life expectancy
# Create an empty plot of the right dimensions, with appropriate labels
plot(x = 1870:2016, y = rep(1, length(1870:2016)), type = "n",
ylim = c(0, 100),
ylab = "Life Expectancy at Birth",
xlab = "Year",
main = "Deciles of countries' national life expectancy at birth\n(1870 \u2013 2016)")
# Plot the 9 deciles of of life expectancy as different line
for(i in 1:9){
lines(1870:2016, quantile_data[i, ],
lwd = 2,
col = quantile_col[i])
}
### Plot the 90/10 decile difference in life expectancy
decile_diff <- quantile_data[9, ] - quantile_data[1, ] # Calculate the 90/10 decile difference
plot(x = 1870:2016, y = decile_diff,
type = "l", lwd = 2, col = "purple",
ylim = c(10, 40),
ylab = "Difference",
xlab = "Year",
main = "The 9th/1st decile difference in national life expectancy at birth\n(1870 \u2013 2016)")
### Plot the 90/10 decile ratio in life expectancy
decile_ratio <- quantile_data[9, ] / quantile_data[1, ]
plot(x = 1870:2016, y = decile_ratio,
type = "l", lwd = 2, col = "Brown",
ylim = c(1, 3.5),
ylab = "Decile Ratio",
xlab = "Year",
main = "The 9th/1st decile ration in national life expectancy at birth\n(1870 \u2013 2016)")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment