Code
library(tidyverse)
library(magrittr)
library(DT)
library(gt)
library(gtExtras)
# install with remotes::install_github('jimjam-slam/ggflags')
library(countrycode)
library(ggflags)
load(url("https://github.com/robertwwalker/robertwwalker.github.io/raw/main/posts/HumanRightsTable/data/HumanRightsProtectionScores_v4.01.RData"))
HR.Data <- x
rm(x)
HR.Data <- HR.Data %>%
left_join(., codelist, by = c(COW = "cown"))
library(countrycode)
Flags <- read.csv(url("https://github.com/robertwwalker/robertwwalker.github.io/raw/main/posts/HumanRightsTable/data/flags_iso.csv"))
HR.Summary <- HR.Data %>%
group_by(country.name.en) %>%
mutate(Obs = n()) %>%
filter(Obs == 74) %>%
arrange(YEAR) %>%
summarise(Obs = mean(Obs), Mean = mean(theta_mean, na.rm = TRUE), SD = sd(theta_mean),
hr_data = list(theta_mean), .groups = "drop")
CCs <- codelist %>%
select(country.name.en, iso3c)
HR.Summary <- HR.Summary %>%
left_join(., CCs) %>%
left_join(., Flags, by = c(iso3c = "Alpha.3.code")) %>%
select(-iso3c, -Country, -Alpha.2.code) %>%
relocate(., URL, .after = country.name.en)
My.Table.1 <- HR.Summary %>%
filter(substr(country.name.en, 1, 1) %in% LETTERS[1:9]) %>%
gt() %>%
# format the numeric output to 3 digit rounding
fmt_number(columns = c(Mean, SD), decimals = 3) %>%
# create nice labels for a few ugly variable names
cols_label(country.name.en = "Country", SD = "Std. Dev.", hr_data = "Time Series Plot",
URL = "Flag") %>%
# Plot the sparklines from the list column
gt_plt_sparkline(hr_data, type = "ref_median", same_limit = TRUE) %>%
# Plot the flags from the included URL's in the data
gt_img_rows(URL, img_source = "web", height = 30) %>%
# use the guardian's table theme
gt_theme_guardian() %>%
# give hulk coloring to the Mean Human Rights Score
gt_hulk_col_numeric(Mean) %>%
# create a header and subheader
tab_header(title = "Human Rights Data Summary", subtitle = "Data from Fariss (2020): 1st Half") %>%
# create the spanner heading
tab_spanner(label = "Human Rights Measures", columns = c(Mean, SD, hr_data))
My.Table.1
Human Rights Data Summary | |||||
Data from Fariss (2020): 1st Half | |||||
Country | Flag | Obs | Human Rights Measures | ||
---|---|---|---|---|---|
Mean | Std. Dev. | Time Series Plot | |||
Afghanistan | 74 | −0.808 | 1.584 | ||
Albania | 74 | −0.734 | 1.132 | ||
Argentina | 74 | −0.110 | 0.866 | ||
Australia | 74 | 1.496 | 0.482 | ||
Austria | 74 | 1.363 | 0.716 | ||
Belgium | 74 | 1.950 | 0.870 | ||
Bhutan | 74 | 1.640 | 0.895 | ||
Bolivia | 74 | 0.031 | 0.600 | ||
Brazil | 74 | −0.987 | 0.438 | ||
Bulgaria | 74 | −0.141 | 0.816 | ||
Canada | 74 | 1.514 | 1.087 | ||
Chile | 74 | −0.116 | 1.098 | ||
China | 74 | −1.690 | 0.587 | ||
Colombia | 74 | −1.549 | 0.337 | ||
Costa Rica | 74 | 1.407 | 0.589 | ||
Cuba | 74 | −0.780 | 0.677 | ||
Czechia | 74 | 0.234 | 1.800 | ||
Denmark | 74 | 2.409 | 0.799 | ||
Dominican Republic | 74 | −0.353 | 0.478 | ||
Ecuador | 74 | −0.066 | 0.409 | ||
Egypt | 74 | −0.954 | 0.339 | ||
El Salvador | 74 | −0.457 | 1.026 | ||
Ethiopia | 74 | −1.251 | 0.934 | ||
Finland | 74 | 2.234 | 0.985 | ||
France | 74 | 0.733 | 0.761 | ||
Greece | 74 | 0.344 | 0.677 | ||
Guatemala | 74 | −1.243 | 1.057 | ||
Haiti | 74 | −0.701 | 0.783 | ||
Honduras | 74 | −0.005 | 0.601 | ||
Hungary | 74 | 0.317 | 1.178 | ||
Iceland | 74 | 4.449 | 0.772 | ||
Iran | 74 | −1.490 | 0.593 | ||
Iraq | 74 | −1.759 | 0.644 | ||
Ireland | 74 | 1.715 | 0.830 | ||
Italy | 74 | 0.748 | 0.578 |