Skip to content

Instantly share code, notes, and snippets.

@ferblape
Created July 29, 2019 12:51
Show Gist options
  • Save ferblape/676f6dec5b5e7c37935aeef3601d6a43 to your computer and use it in GitHub Desktop.
Save ferblape/676f6dec5b5e7c37935aeef3601d6a43 to your computer and use it in GitHub Desktop.
Sample of historical population analysis using Populate Data
# Load dependencies
library(tidyverse)
library(httr)
library(ggplot2)
library(bbplot)
# Set seed
set.seed(14723)
# Set working directory
setwd(dirname(rstudioapi::getActiveDocumentContext()$path))
# Populate Data parameters
dataset <- "ds-censo-municipal-poblacion-derecho"
account <- Sys.getenv('POPULATE_DATA_ACCOUNT')
token <- Sys.getenv('POPULATE_DATA_TOKEN')
origin <- Sys.getenv('POPULATE_DATA_ORIGIN')
# Load 2 years of data
df <- NULL
years <- c(1877, 2011)
for(year in years){
url <- paste0("https://data.populate.tools/", account , "/datasets/", dataset, ".csv?include=province,municipality&filter_by_year=", year)
r <- GET(url, add_headers(authorization = paste0("Bearer ", token), origin = origin))
partial_df <- content(r, "parsed")
if(nrow(partial_df) == 0) { break }
df <- rbind(df, partial_df)
}
# Plot differences between 1877 and 2011
df %>% group_by(municipality_name, date) %>% summarize(total = sum(value)) %>%
mutate(total0 = first(total), total1 = last(total)) %>%
select(municipality_name, total0, total1) %>%
distinct(municipality_name, total0, total1) %>%
ungroup() %>%
top_n(50, total0) %>%
ggplot(aes(y=reorder(municipality_name, total0), x=total0, xend=total1)) +
bbc_style() +
geom_dumbbell(colour = "#dddddd",
size = 3,
colour_x = "#FAAB18",
colour_xend = "#1380A1"
) +
scale_x_continuous(breaks = c(0, 1000000, 2000000, 3000000),
labels = c(0, "1M", "2M", "3M"),
position="top") +
theme(
axis.text.x = element_text(size = 11),
axis.text.y = element_text(size = 10),
panel.grid.major.y = element_blank(),
panel.grid.major.x = element_line(color="#eaeaea")
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment