tidycesnus 1tidyverse는 인구조사(센서스, census)에도 예외가 아니다. tidycensus는 버전 1.0 로드맵에서 밝혔듯이 미국 인구통계를 깔끔한(tidy) 형태로 정리해서 tidyverse 생태계와 지리정보 sf 팩키지와 유기적으로 연결하는 것을 목적으로 개발되었다.
tidycensus를 이용하기 위해서는 Census API에 Request A Key 웹페이지에서 키를 하나 받아야 한다. 전달받은 키를 잘 간직하도록 한다.
get_acs() 함수로 텍사스주(TX), 자치주(county)별로 인구수(B01003_001)를 지리정보(geometry = TRUE)와 함께 가져온다.
# 0. 환경설정 -----
library(tidycensus)
library(tidyverse)
library(viridis)
library(sf)
library(leaflet)
# 1. 데이터 가져오기 -----
options(tigris_use_cache = TRUE)
census_api_key(tidycensus_key)
texas_pop <- get_acs(geography = "county",
variables = "B01003_001",
state = "TX",
geometry = TRUE)
texas_popSimple feature collection with 254 features and 5 fields
geometry type: MULTIPOLYGON
dimension: XY
bbox: xmin: -106.6456 ymin: 25.83738 xmax: -93.50829 ymax: 36.5007
epsg (SRID): 4269
proj4string: +proj=longlat +datum=NAD83 +no_defs
First 10 features:
GEOID NAME variable estimate moe
1 48175 Goliad County, Texas B01003_001 7463 NA
2 48185 Grimes County, Texas B01003_001 27140 NA
3 48215 Hidalgo County, Texas B01003_001 828334 NA
4 48225 Houston County, Texas B01003_001 22802 NA
5 48235 Irion County, Texas B01003_001 1631 135
6 48273 Kleberg County, Texas B01003_001 31877 NA
7 48277 Lamar County, Texas B01003_001 49626 NA
8 48317 Martin County, Texas B01003_001 5451 NA
9 48351 Newton County, Texas B01003_001 14138 NA
10 48361 Orange County, Texas B01003_001 83751 NA
geometry
1 MULTIPOLYGON (((-97.77853 2...
2 MULTIPOLYGON (((-96.18831 3...
3 MULTIPOLYGON (((-98.5853 26...
4 MULTIPOLYGON (((-95.77535 3...
5 MULTIPOLYGON (((-101.2719 3...
6 MULTIPOLYGON (((-97.3178 27...
7 MULTIPOLYGON (((-95.85772 3...
8 MULTIPOLYGON (((-102.211 32...
9 MULTIPOLYGON (((-93.91113 3...
10 MULTIPOLYGON (((-94.11796 3...
texas_pop는 지리정보가 포함된 sf 객체로 티블(tibble)를 반환한다.
get_decennial() 함수는 10년주기를 갖는 1990, 2000, 2010 US 인구통계 API에 접속할 수 있게 해주고, get_acs() 함수는 5년주기 미국 지역사회 조사(American Community Survey, ACS) API에 접속할 수 있게 해 준다.
인구통계조사에서 가져올 수 있는 데이터를 load_variables() 함수로 탐색한다. American Community Survey (ACS) 조사항목](https://www.census.gov/programs-surveys/acs/guidance/subjects.html)에 대해서는 추출가능한 데이터에 대한 정보를 사전 찾아볼 수 있다.
## 추출가능 데이터
v15 <- load_variables(2016, "acs5", cache = TRUE)텍사스의 경우도 대도시에 인구가 밀집되어 있어 10분위수를 ntile() 함수로 생성시키고 나서 이를 viridis의 도움으로 정적 시각화한다.
# 2. 시각화 -----
## 2.1. 정적 시각화
texas_pop %>%
mutate(estimate_decile = ntile(estimate, 10)) %>%
ggplot(aes(fill = estimate_decile)) +
geom_sf() +
scale_fill_viridis() +
theme_minimal() +
labs(fill = "10분위수")leaflet 팩키지를 활용하여 인터랙티브 시각화도 가능하다.
## 2.2. 동적 시각화
pal <- colorQuantile(palette = "viridis", domain = texas_pop$estimate, n = 10)
texas_pop %>%
st_transform(crs = "+init=epsg:4326") %>%
leaflet(width = "100%") %>%
addProviderTiles(provider = "CartoDB.Positron") %>%
addPolygons(popup = ~ paste0("자치주명: ", str_extract(NAME, "^([^,]*)"), "\n",
"인구수: ", estimate),
stroke = FALSE,
smoothFactor = 0,
fillOpacity = 0.7,
color = ~ pal(estimate)) %>%
addLegend("bottomright",
pal = pal,
values = ~ estimate,
title = "인구 백분위수",
opacity = 1)