Skip to content

Instantly share code, notes, and snippets.

View guyabel's full-sized avatar

Guy Abel guyabel

View GitHub Profile
@guyabel
guyabel / un_stock.R
Last active October 12, 2023 01:54
Read in UN DESA bilateral stock data (2020) into R
library(tidyverse)
library(readxl)
download.file(url = "https://www.un.org/development/desa/pd/sites/www.un.org.development.desa.pd/files/undesa_pd_2020_ims_stock_by_sex_destination_and_origin.xlsx",
destfile = "undesa_pd_2020_ims_stock_by_sex_destination_and_origin.xlsx", mode = "wb")
d <- read_excel(path = "undesa_pd_2020_ims_stock_by_sex_destination_and_origin.xlsx",
sheet = 2, skip = 10, na = "..", guess_max = 1e5) %>%
select(-1, -3, -5) %>%
pivot_longer(cols = -(1:4), names_to = "year", values_to = "stock") %>%
@guyabel
guyabel / pop_total.R
Created November 14, 2022 15:07
Animated population tree map plot for UN WPP2022 data used in https://guyabel.com/post/animated-population-treemaps/ blog post.
library(tidyverse)
library(treemapify)
library(countrycode)
library(gganimate)
library(magick)
# devtools::install_github("PPgp/wpp2022")
library(wpp2022)
# load data
data(pop1dt)
name region order colour gap name_zh
Beijing East 1 #B61416 2 北京
Tianjin East 2 #C01517 2 天津
Hebei East 3 #CA1718 2 河北
Shandong East 4 #D4181A 2 山东
Jiangsu East 5 #DE191B 2 江苏
Shanghai East 6 #E91A1C 2 上海
Zhejiang East 7 #F31B1D 2 浙江
Fujian East 8 #FD1C1F 2 福建
Guangdong East 9 #FF1E20 2 广东
@guyabel
guyabel / china_flow_2010_2015.csv
Last active May 29, 2021 06:45
five year migration flows between chinese provinces 2010-2015, as used in https://doi.org/10.1177/0308518X17718375. See https://guyabel.com/post/chinese-migration-chord-diagram/ for more details.
orig dest flow
Beijing Beijing 0
Tianjin Beijing 67355
Hebei Beijing 864387
Shanxi Beijing 225032
Inner Mongolia Beijing 103355
Liaoning Beijing 155032
Jilin Beijing 104774
Heilongjiang Beijing 194387
Shanghai Beijing 26581
@guyabel
guyabel / migrant_sankey.R
Last active June 13, 2021 17:00
Animated sankey plots for UN migrant stock data (IMS2020) related to https://guyabel.com/post/animated-sankey/ blog post
library(tidyverse)
library(readxl)
library(tweenr)
library(ggforce)
library(gridExtra)
library(animation)
library(magick)
# download the migrant stock origin-destination xlsx file from the UN site
x <- tempfile(fileext = ".xlsx")
ipums_clean_dta <- function(
d = NULL, numeric_convert = NULL, string_as_factors = TRUE, clean_labels = FALSE,
mis_lab = c("unknown", "niu (not in universe)", "not reported/missing",
"unknown/missing", "response suppressed", "unclassifiable")
){
d %>%
{if(clean_labels) dplyr::mutate_if(is.labelled, ipumsr::lbl_clean) else .} %>%
dplyr::mutate_if(is.labelled, ~ipumsr::lbl_na_if(., ~.lbl %in% mis_lab)) %>%
dplyr::mutate_at({numeric_convert}, haven::zap_labels) %>%
dplyr::mutate_if(is.labelled, haven::as_factor) %>%
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<table>
<tbody>
<tr>
@guyabel
guyabel / cd.R
Last active May 1, 2018 18:46
GuyAbelUW
## ----setup, include=FALSE------------------------------------------------
library(knitr)
knitr::opts_chunk$set(prompt= TRUE, collapse = TRUE, comment = NA)
knitr::opts_chunk$set(fig.width = 5, fig.height = 5)
knitr::knit_hooks$set(purl = hook_purl)
## ---- message=FALSE------------------------------------------------------
# install.packages("migest")
library(tidyverse)
d0 <- read_csv(system.file("imr", "reg_flow.csv", package = "migest"))
@guyabel
guyabel / sweden1993.csv
Created July 6, 2016 04:24
Life table data for Sweden 1993 from Preston, Samuel H, Patrick Heuveline, and Michel Guillot. (1991), Demography: Measuring and Modeling Population Processes., New York: Blackwell
age x Nx_m Nx_f Lx_m Lx_f Fx Mx_f
00--05 0 310189 293395 496754 497487 0 6840
05--10 5 261963 248369 496297 497138 0 4150
10--15 10 252046 240012 495989 496901 0 3365
15--20 15 274711 261346 495113 496531 0.012 5270
20--25 20 296679 285209 493460 495902 0.0908 9240
25--30 25 333726 314388 491475 495168 0.1499 8230
30--35 30 296774 281290 489325 494213 0.1125 5470
35--40 35 299391 286923 486487 492760 0.0441 3155
40--45 40 314295 304108 482392 490447 0.0074 1770
@guyabel
guyabel / thesis.tex
Created March 15, 2014 21:30
thesis.tex
\documentclass[11pt]{report}
\usepackage[a4paper,margin=2cm, bindingoffset=2cm]{geometry}
\usepackage{appendix}
\usepackage{amsmath}
\usepackage{booktabs}
\usepackage{threeparttable}
\usepackage{natbib}
\bibliographystyle{chicago}