## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  warning = FALSE,
  message = FALSE,
  out.width = "100%"
)

library(ggplot2)
library(sf)
library(dplyr)
library(tidyr)

## ----include = TRUE-----------------------------------------------------------
library(tidycensuskr)


## ----include = TRUE-----------------------------------------------------------
data(adm2_sf_2020)
print(length(unique(adm2_sf_2020$adm2_code)))


## ----include = TRUE-----------------------------------------------------------
df_2020 <- anycensus(year = 2020,
                     type = "mortality",
                     level = "adm2")
head(df_2020)


## ----include = TRUE-----------------------------------------------------------
df_2020_sido <- anycensus(year = 2020,
                          type = "mortality",
                          level = "adm1",
                          aggregator = mean,
                          na.rm = TRUE)
head(df_2020_sido)


## ----include = TRUE-----------------------------------------------------------
df_2020_sido_weighted <- anycensus(year = 2020,
                                   codes = "Seoul",
                                   type = "mortality",
                                   level = "adm1",
                                   aggregator = stats::weighted.mean,
                                   weight_type = "population",
                                   weight_column = "all households_total_prs",
                                   na.rm = TRUE)
head(df_2020_sido_weighted)


## ----include = TRUE-----------------------------------------------------------
df_2020_adm2_weighted_atn <- anycensus(year = 2020,
                                       codes = "Gyeonggi-do",
                                       type = "mortality",
                                       level = "adm2",
                                       adm2_type = "atn",
                                       aggregator = stats::weighted.mean,
                                       weight_type = "population",
                                       weight_column = "all households_total_prs",
                                       na.rm = TRUE)
head(df_2020_adm2_weighted_atn)


## ----include = TRUE-----------------------------------------------------------
df_2020_sido_weighted_atn <- anycensus(year = 2020,
                                       codes = "Gyeonggi-do",
                                       type = "mortality",
                                       level = "adm1",
                                       adm2_type = "atn",
                                       aggregator = stats::weighted.mean,
                                       weight_type = "population",
                                       weight_column = "all households_total_prs",
                                       na.rm = TRUE)
head(df_2020_sido_weighted_atn)


## ----include = TRUE-----------------------------------------------------------
df_2020_adm3 <- anycensus(year = 2020,
                          codes = "Seoul",
                          type = "mortality",
                          level = "adm3")
head(df_2020_adm3)

## ----include = TRUE-----------------------------------------------------------
data(censuskor)
head(censuskor)


## ----include = TRUE, fig.width=7, fig.height=4--------------------------------
ggplot(df_2020, aes(x = `all causes_male_p1p`, y = `all causes_female_p1p`)) +
  geom_point() +
  labs(
    x = "Male mortality (per 100,000 population)",
    y = "Female mortality (per 100,000 population)",
    title = "Male vs. Female Age-standardized Mortality Rates in South Korea (2020)"
  ) +
  theme_minimal(base_size = 10)

