setting

R 버젼 확인

mapview 패키지는 최신 R에서만 설치됨.

package_version(R.version) 

시스템 설정

sf, rgdal, sp 설치시 시스템 설정해야 하는 것이 있음.

GDAL 설치:

GDAL설치시 맥킨토시에서 문제가 많았음. GDAL: 콘다로 제대로 설치 안되서 brew로 설치함.

brew install pkg-config
brew install gdal

PROJ 설치:

conda install -c conda-forge proj-data

MAC OS의 R에서 명령어:

install.packages("rgdal", type = "source",
                 configure.args = c("--with-proj-include=/usr/local/include",
                                    "--with-proj-lib=/usr/local/lib”))

Setup packages

install.packages("sp")
install.packages("rgdal")
install.packages("raster)
install.packages("sf")
install.packages("tigris")
install.packages("tidycensus")
install.packages("tidyverse")
install.packages("mapview")

라이브러리 로딩

library(sp)
library(rgdal)
library(raster)
library(sf)
library(tigris)
library(mapview)
library(tidyverse)
library(tidycensus)

tidycensus option: API key 입력

참고: API key는 개인정보이므로 공개하면 안됨

census_api_key("너의 API 코드 ") 
# 설치된 api key 확인 
Sys.getenv("CENSUS_API_KEY") # Check your API key

tigris option

tigris_cache_dir("/Users/miyoungyoon/Google 드라이브/연습_RStudio/map") # Set the cache directory
options(tigris_use_cache=TRUE)# Set the tigris_use_cache option
getOption("tigris_use_cache") # Check to see that you've modified the option correctly

# Get data from tigris as simple features: 
# shpae file 을 simple file 로 변환해야 ggplot으로 그릴 수 있음 
options(tigris_class = "sf")

#tidycensus 데이터 테이블 보기

# tidycensus variable import
v19 <- load_variables(2019, "acs5", cache = TRUE)
# 전체 변수를 필터링해서 검색 가능
head(v19) 
## # A tibble: 6 x 3
##   name       label                                   concept   
##   <chr>      <chr>                                   <chr>     
## 1 B01001_001 Estimate!!Total:                        SEX BY AGE
## 2 B01001_002 Estimate!!Total:!!Male:                 SEX BY AGE
## 3 B01001_003 Estimate!!Total:!!Male:!!Under 5 years  SEX BY AGE
## 4 B01001_004 Estimate!!Total:!!Male:!!5 to 9 years   SEX BY AGE
## 5 B01001_005 Estimate!!Total:!!Male:!!10 to 14 years SEX BY AGE
## 6 B01001_006 Estimate!!Total:!!Male:!!15 to 17 years SEX BY AGE

tidycensus 패키지로 US census 데이터 가져오기

# Get a dataset of median home values from the 1-year ACS
state_value <- get_acs(geography = "state", 
                       variables = "B25077_001", 
                       survey = "acs1", 
                       geometry = TRUE, 
                       shift_geo = TRUE)
## Warning: The `shift_geo` argument is deprecated and will be removed in a future
## release. We recommend using `tigris::shift_geometry()` instead.
## The 1-year ACS provides data for geographies with populations of 65,000 and greater.
## Getting data from the 2019 1-year ACS
## Using feature geometry obtained from the albersusa package
## Please note: Alaska and Hawaii are being shifted and are not to scale.
# Plot the dataset to view the shifted geometry
plot(state_value["estimate"])

ggplot으로 tidycensus 데이터 그리기

demographic mapping: US - median housing values

centers <- st_centroid(state_value) # st_centroid() tool in the sf package 
## Warning in st_centroid.sf(state_value): st_centroid assumes attributes are
## constant over geometries of x
# US지도에 동그라미 크기로 표현됨 
ggplot() + 
  geom_sf(data = state_value, fill = "white") + 
  geom_sf(data = centers, aes(size = estimate), shape = 21, 
          fill = "lightblue", alpha = 0.7, show.legend = "point") + 
  scale_size_continuous(range = c(1, 20))

tidycensus 패키지로 데이터 가져오기

variable에 필요한 데이터 값 번호 입력 variable = "B25077_001: Milwuakee county - median housing values

# median_value: B25077_001
# Milwuakee county -  median housing values "   Estimate!!Median value (dollars)"

mk_county_median_income <- get_acs(geography = "tract", 
                                   state = "WI",
                                   county = "Milwaukee",
                                   variable = "B25077_001",
                                   geometry = TRUE)
## 
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |=                                                                     |   2%
  |                                                                            
  |==                                                                    |   3%
  |                                                                            
  |===                                                                   |   4%
  |                                                                            
  |====                                                                  |   5%
  |                                                                            
  |=====                                                                 |   7%
  |                                                                            
  |======                                                                |   8%
  |                                                                            
  |=======                                                               |  10%
  |                                                                            
  |========                                                              |  11%
  |                                                                            
  |=========                                                             |  13%
  |                                                                            
  |==========                                                            |  14%
  |                                                                            
  |===========                                                           |  15%
  |                                                                            
  |============                                                          |  17%
  |                                                                            
  |=============                                                         |  18%
  |                                                                            
  |==============                                                        |  20%
  |                                                                            
  |================                                                      |  22%
  |                                                                            
  |=================                                                     |  24%
  |                                                                            
  |=================                                                     |  25%
  |                                                                            
  |===================                                                   |  26%
  |                                                                            
  |===================                                                   |  27%
  |                                                                            
  |====================                                                  |  29%
  |                                                                            
  |======================                                                |  31%
  |                                                                            
  |======================                                                |  32%
  |                                                                            
  |=======================                                               |  33%
  |                                                                            
  |========================                                              |  34%
  |                                                                            
  |=========================                                             |  36%
  |                                                                            
  |==========================                                            |  38%
  |                                                                            
  |===========================                                           |  38%
  |                                                                            
  |============================                                          |  40%
  |                                                                            
  |=============================                                         |  41%
  |                                                                            
  |==============================                                        |  43%
  |                                                                            
  |===============================                                       |  45%
  |                                                                            
  |================================                                      |  45%
  |                                                                            
  |=================================                                     |  47%
  |                                                                            
  |==================================                                    |  48%
  |                                                                            
  |===================================                                   |  50%
  |                                                                            
  |====================================                                  |  51%
  |                                                                            
  |=====================================                                 |  52%
  |                                                                            
  |======================================                                |  54%
  |                                                                            
  |======================================                                |  55%
  |                                                                            
  |========================================                              |  56%
  |                                                                            
  |=========================================                             |  58%
  |                                                                            
  |=========================================                             |  59%
  |                                                                            
  |===========================================                           |  61%
  |                                                                            
  |===========================================                           |  62%
  |                                                                            
  |============================================                          |  63%
  |                                                                            
  |==============================================                        |  65%
  |                                                                            
  |==============================================                        |  66%
  |                                                                            
  |===============================================                       |  68%
  |                                                                            
  |================================================                      |  68%
  |                                                                            
  |=================================================                     |  70%
  |                                                                            
  |==================================================                    |  72%
  |                                                                            
  |===================================================                   |  73%
  |                                                                            
  |====================================================                  |  75%
  |                                                                            
  |=====================================================                 |  75%
  |                                                                            
  |======================================================                |  77%
  |                                                                            
  |=======================================================               |  79%
  |                                                                            
  |========================================================              |  80%
  |                                                                            
  |=========================================================             |  81%
  |                                                                            
  |==========================================================            |  82%
  |                                                                            
  |===========================================================           |  84%
  |                                                                            
  |============================================================          |  86%
  |                                                                            
  |=============================================================         |  86%
  |                                                                            
  |==============================================================        |  88%
  |                                                                            
  |==============================================================        |  89%
  |                                                                            
  |================================================================      |  91%
  |                                                                            
  |=================================================================     |  93%
  |                                                                            
  |===================================================================   |  95%
  |                                                                            
  |===================================================================   |  96%
  |                                                                            
  |====================================================================  |  98%
  |                                                                            
  |======================================================================|  99%
  |                                                                            
  |======================================================================| 100%
head(mk_county_median_income)
## Simple feature collection with 6 features and 5 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -88.02764 ymin: 43.03472 xmax: -87.90517 ymax: 43.105
## Geodetic CRS:  NAD83
##         GEOID                                           NAME   variable
## 1 55079007000   Census Tract 70, Milwaukee County, Wisconsin B25077_001
## 2 55079003300   Census Tract 33, Milwaukee County, Wisconsin B25077_001
## 3 55079006100   Census Tract 61, Milwaukee County, Wisconsin B25077_001
## 4 55079006400   Census Tract 64, Milwaukee County, Wisconsin B25077_001
## 5 55079014800  Census Tract 148, Milwaukee County, Wisconsin B25077_001
## 6 55079186400 Census Tract 1864, Milwaukee County, Wisconsin B25077_001
##   estimate   moe                       geometry
## 1    59500 10741 MULTIPOLYGON (((-87.91693 4...
## 2   115800  6142 MULTIPOLYGON (((-88.02752 4...
## 3   113000 23050 MULTIPOLYGON (((-87.97244 4...
## 4    60200 23838 MULTIPOLYGON (((-87.94732 4...
## 5       NA    NA MULTIPOLYGON (((-87.94288 4...
## 6       NA    NA MULTIPOLYGON (((-87.94499 4...
plot(mk_county_median_income["estimate"])

Milwuakee county - median housing values

# Milwuakee county -  median housing values "   Estimate!!Median value (dollars)"
centers_mk <- st_centroid(mk_county_median_income)
## Warning in st_centroid.sf(mk_county_median_income): st_centroid assumes
## attributes are constant over geometries of x
ggplot() +
  geom_sf(data = mk_county_median_income, fill="white") +
  geom_sf(data=centers_mk, aes(size=estimate), shape=21, 
          fill="red", alpha=0.3, show.legend="point")+
  scale_size_continuous(range=c(0.1,20))
## Warning: Removed 9 rows containing missing values (geom_sf).

head(centers_mk )
## Simple feature collection with 6 features and 5 fields
## Geometry type: POINT
## Dimension:     XY
## Bounding box:  xmin: -88.0173 ymin: 43.03688 xmax: -87.91123 ymax: 43.10105
## Geodetic CRS:  NAD83
##         GEOID                                           NAME   variable
## 1 55079007000   Census Tract 70, Milwaukee County, Wisconsin B25077_001
## 2 55079003300   Census Tract 33, Milwaukee County, Wisconsin B25077_001
## 3 55079006100   Census Tract 61, Milwaukee County, Wisconsin B25077_001
## 4 55079006400   Census Tract 64, Milwaukee County, Wisconsin B25077_001
## 5 55079014800  Census Tract 148, Milwaukee County, Wisconsin B25077_001
## 6 55079186400 Census Tract 1864, Milwaukee County, Wisconsin B25077_001
##   estimate   moe                   geometry
## 1    59500 10741 POINT (-87.91123 43.07425)
## 2   115800  6142  POINT (-88.0173 43.10105)
## 3   113000 23050 POINT (-87.96848 43.06791)
## 4    60200 23838 POINT (-87.94235 43.07422)
## 5       NA    NA POINT (-87.94021 43.04213)
## 6       NA    NA POINT (-87.93049 43.03688)

Joining data from tigris and tidycensus: Milwaukee school district

# Joining data from tigris and tidycensus: Milwaukee school district 
# B19013_001: Estimate!!Median household income in the past 12 months (in 2019 inflation-adjusted dollars)
wi_income <- get_acs(geography="school district (unified)",
                     variables = "B19013_001",
                     state = "WI")
wi_school <- school_districts(state = "WI", type="unified", class="sf")
## 
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |                                                                      |   1%
  |                                                                            
  |=                                                                     |   1%
  |                                                                            
  |=                                                                     |   2%
  |                                                                            
  |==                                                                    |   2%
  |                                                                            
  |==                                                                    |   3%
  |                                                                            
  |===                                                                   |   4%
  |                                                                            
  |===                                                                   |   5%
  |                                                                            
  |====                                                                  |   5%
  |                                                                            
  |====                                                                  |   6%
  |                                                                            
  |=====                                                                 |   7%
  |                                                                            
  |=====                                                                 |   8%
  |                                                                            
  |======                                                                |   8%
  |                                                                            
  |======                                                                |   9%
  |                                                                            
  |=======                                                               |  10%
  |                                                                            
  |=======                                                               |  11%
  |                                                                            
  |========                                                              |  11%
  |                                                                            
  |========                                                              |  12%
  |                                                                            
  |=========                                                             |  12%
  |                                                                            
  |=========                                                             |  13%
  |                                                                            
  |==========                                                            |  14%
  |                                                                            
  |==========                                                            |  15%
  |                                                                            
  |===========                                                           |  15%
  |                                                                            
  |===========                                                           |  16%
  |                                                                            
  |============                                                          |  17%
  |                                                                            
  |============                                                          |  18%
  |                                                                            
  |=============                                                         |  18%
  |                                                                            
  |=============                                                         |  19%
  |                                                                            
  |==============                                                        |  19%
  |                                                                            
  |==============                                                        |  20%
  |                                                                            
  |===============                                                       |  21%
  |                                                                            
  |===============                                                       |  22%
  |                                                                            
  |================                                                      |  22%
  |                                                                            
  |================                                                      |  23%
  |                                                                            
  |================                                                      |  24%
  |                                                                            
  |=================                                                     |  24%
  |                                                                            
  |=================                                                     |  25%
  |                                                                            
  |==================                                                    |  25%
  |                                                                            
  |==================                                                    |  26%
  |                                                                            
  |===================                                                   |  27%
  |                                                                            
  |===================                                                   |  28%
  |                                                                            
  |====================                                                  |  28%
  |                                                                            
  |====================                                                  |  29%
  |                                                                            
  |=====================                                                 |  29%
  |                                                                            
  |=====================                                                 |  30%
  |                                                                            
  |=====================                                                 |  31%
  |                                                                            
  |======================                                                |  31%
  |                                                                            
  |======================                                                |  32%
  |                                                                            
  |=======================                                               |  32%
  |                                                                            
  |=======================                                               |  33%
  |                                                                            
  |========================                                              |  34%
  |                                                                            
  |========================                                              |  35%
  |                                                                            
  |=========================                                             |  35%
  |                                                                            
  |=========================                                             |  36%
  |                                                                            
  |==========================                                            |  37%
  |                                                                            
  |==========================                                            |  38%
  |                                                                            
  |===========================                                           |  38%
  |                                                                            
  |===========================                                           |  39%
  |                                                                            
  |============================                                          |  39%
  |                                                                            
  |============================                                          |  40%
  |                                                                            
  |=============================                                         |  41%
  |                                                                            
  |=============================                                         |  42%
  |                                                                            
  |==============================                                        |  42%
  |                                                                            
  |==============================                                        |  43%
  |                                                                            
  |===============================                                       |  44%
  |                                                                            
  |===============================                                       |  45%
  |                                                                            
  |================================                                      |  45%
  |                                                                            
  |================================                                      |  46%
  |                                                                            
  |=================================                                     |  47%
  |                                                                            
  |=================================                                     |  48%
  |                                                                            
  |==================================                                    |  48%
  |                                                                            
  |==================================                                    |  49%
  |                                                                            
  |===================================                                   |  50%
  |                                                                            
  |===================================                                   |  51%
  |                                                                            
  |====================================                                  |  51%
  |                                                                            
  |====================================                                  |  52%
  |                                                                            
  |=====================================                                 |  52%
  |                                                                            
  |=====================================                                 |  53%
  |                                                                            
  |======================================                                |  54%
  |                                                                            
  |======================================                                |  55%
  |                                                                            
  |=======================================                               |  55%
  |                                                                            
  |=======================================                               |  56%
  |                                                                            
  |========================================                              |  57%
  |                                                                            
  |========================================                              |  58%
  |                                                                            
  |=========================================                             |  58%
  |                                                                            
  |=========================================                             |  59%
  |                                                                            
  |==========================================                            |  59%
  |                                                                            
  |==========================================                            |  60%
  |                                                                            
  |===========================================                           |  61%
  |                                                                            
  |===========================================                           |  62%
  |                                                                            
  |============================================                          |  62%
  |                                                                            
  |============================================                          |  63%
  |                                                                            
  |============================================                          |  64%
  |                                                                            
  |=============================================                         |  64%
  |                                                                            
  |=============================================                         |  65%
  |                                                                            
  |==============================================                        |  65%
  |                                                                            
  |==============================================                        |  66%
  |                                                                            
  |===============================================                       |  67%
  |                                                                            
  |===============================================                       |  68%
  |                                                                            
  |================================================                      |  68%
  |                                                                            
  |================================================                      |  69%
  |                                                                            
  |=================================================                     |  69%
  |                                                                            
  |=================================================                     |  70%
  |                                                                            
  |=================================================                     |  71%
  |                                                                            
  |==================================================                    |  71%
  |                                                                            
  |==================================================                    |  72%
  |                                                                            
  |===================================================                   |  72%
  |                                                                            
  |===================================================                   |  73%
  |                                                                            
  |====================================================                  |  74%
  |                                                                            
  |====================================================                  |  75%
  |                                                                            
  |=====================================================                 |  75%
  |                                                                            
  |=====================================================                 |  76%
  |                                                                            
  |======================================================                |  77%
  |                                                                            
  |======================================================                |  78%
  |                                                                            
  |=======================================================               |  78%
  |                                                                            
  |=======================================================               |  79%
  |                                                                            
  |========================================================              |  79%
  |                                                                            
  |========================================================              |  80%
  |                                                                            
  |=========================================================             |  81%
  |                                                                            
  |=========================================================             |  82%
  |                                                                            
  |==========================================================            |  82%
  |                                                                            
  |==========================================================            |  83%
  |                                                                            
  |===========================================================           |  84%
  |                                                                            
  |===========================================================           |  85%
  |                                                                            
  |============================================================          |  85%
  |                                                                            
  |============================================================          |  86%
  |                                                                            
  |=============================================================         |  87%
  |                                                                            
  |=============================================================         |  88%
  |                                                                            
  |==============================================================        |  88%
  |                                                                            
  |==============================================================        |  89%
  |                                                                            
  |===============================================================       |  89%
  |                                                                            
  |===============================================================       |  90%
  |                                                                            
  |===============================================================       |  91%
  |                                                                            
  |================================================================      |  91%
  |                                                                            
  |================================================================      |  92%
  |                                                                            
  |=================================================================     |  92%
  |                                                                            
  |=================================================================     |  93%
  |                                                                            
  |==================================================================    |  94%
  |                                                                            
  |==================================================================    |  95%
  |                                                                            
  |===================================================================   |  95%
  |                                                                            
  |===================================================================   |  96%
  |                                                                            
  |====================================================================  |  97%
  |                                                                            
  |====================================================================  |  98%
  |                                                                            
  |===================================================================== |  98%
  |                                                                            
  |===================================================================== |  99%
  |                                                                            
  |======================================================================| 100%
str(wi_income)
## tibble [369 × 5] (S3: tbl_df/tbl/data.frame)
##  $ GEOID   : chr [1:369] "5500016" "5500017" "5500030" "5500056" ...
##  $ NAME    : chr [1:369] "Blair-Taylor School District, Wisconsin" "River Ridge School District, Wisconsin" "Abbotsford School District, Wisconsin" "Gresham School District, Wisconsin" ...
##  $ variable: chr [1:369] "B19013_001" "B19013_001" "B19013_001" "B19013_001" ...
##  $ estimate: num [1:369] 54889 52533 50601 58036 45549 ...
##  $ moe     : num [1:369] 4495 2507 6030 5830 3515 ...
str(wi_school)
## Classes 'sf' and 'data.frame':   369 obs. of  15 variables:
##  $ STATEFP : chr  "55" "55" "55" "55" ...
##  $ UNSDLEA : chr  "09660" "03030" "13830" "00210" ...
##  $ GEOID   : chr  "5509660" "5503030" "5513830" "5500210" ...
##  $ NAME    : chr  "Mineral Point School District" "Cuba City School District" "Shullsburg School District" "Alma Center School District" ...
##  $ LSAD    : chr  "00" "00" "00" "00" ...
##  $ LOGRADE : chr  "PK" "PK" "PK" "PK" ...
##  $ HIGRADE : chr  "12" "12" "12" "12" ...
##  $ MTFCC   : chr  "G5420" "G5420" "G5420" "G5420" ...
##  $ SDTYP   : chr  NA NA NA NA ...
##  $ FUNCSTAT: chr  "E" "E" "E" "E" ...
##  $ ALAND   : num  3.46e+08 2.05e+08 2.50e+08 3.59e+08 9.27e+08 ...
##  $ AWATER  : num  250893 258432 0 847352 16033326 ...
##  $ INTPTLAT: chr  "+42.8510666" "+42.6209709" "+42.5611933" "+44.4860642" ...
##  $ INTPTLON: chr  "-090.1497347" "-090.4944397" "-090.2049416" "-090.9093804" ...
##  $ geometry:sfc_MULTIPOLYGON of length 369; first list element: List of 5
##   ..$ :List of 1
##   .. ..$ : num [1:7, 1:2] -90 -90 -90 -90 -90 ...
##   ..$ :List of 1
##   .. ..$ : num [1:19, 1:2] -90 -90 -90 -90 -90 ...
##   ..$ :List of 1
##   .. ..$ : num [1:24, 1:2] -90 -90 -90 -90 -90 ...
##   ..$ :List of 1
##   .. ..$ : num [1:9, 1:2] -90.2 -90.2 -90.2 -90.2 -90.2 ...
##   ..$ :List of 7
##   .. ..$ : num [1:825, 1:2] -90.3 -90.3 -90.3 -90.3 -90.3 ...
##   .. ..$ : num [1:46, 1:2] -90.3 -90.3 -90.3 -90.3 -90.3 ...
##   .. ..$ : num [1:13, 1:2] -90.3 -90.3 -90.3 -90.3 -90.3 ...
##   .. ..$ : num [1:20, 1:2] -90.2 -90.2 -90.2 -90.2 -90.2 ...
##   .. ..$ : num [1:81, 1:2] -90.2 -90.2 -90.2 -90.2 -90.2 ...
##   .. ..$ : num [1:41, 1:2] -90.2 -90.2 -90.2 -90.2 -90.2 ...
##   .. ..$ : num [1:24, 1:2] -90.3 -90.3 -90.3 -90.3 -90.3 ...
##   ..- attr(*, "class")= chr [1:3] "XY" "MULTIPOLYGON" "sfg"
##  - attr(*, "sf_column")= chr "geometry"
##  - attr(*, "agr")= Factor w/ 3 levels "constant","aggregate",..: NA NA NA NA NA NA NA NA NA NA ...
##   ..- attr(*, "names")= chr [1:14] "STATEFP" "UNSDLEA" "GEOID" "NAME" ...
##  - attr(*, "tigris")= chr "unsd"
# Join the income dataset to the boundaries dataset
wi_school_joined <- left_join(wi_school, wi_income, by = "GEOID")
plot(wi_school_joined["estimate"])

head(wi_school_joined )
## Simple feature collection with 6 features and 18 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -91.0652 ymin: 42.50642 xmax: -87.88419 ymax: 44.59626
## Geodetic CRS:  NAD83
##   STATEFP UNSDLEA   GEOID                        NAME.x LSAD LOGRADE HIGRADE
## 1      55   09660 5509660 Mineral Point School District   00      PK      12
## 2      55   03030 5503030     Cuba City School District   00      PK      12
## 3      55   13830 5513830    Shullsburg School District   00      PK      12
## 4      55   00210 5500210   Alma Center School District   00      PK      12
## 5      55   11790 5511790    Pittsville School District   00      PK      12
## 6      55   05610 5505610       Grafton School District   00      PK      12
##   MTFCC SDTYP FUNCSTAT     ALAND   AWATER    INTPTLAT     INTPTLON
## 1 G5420  <NA>        E 345854244   250893 +42.8510666 -090.1497347
## 2 G5420  <NA>        E 204731863   258432 +42.6209709 -090.4944397
## 3 G5420  <NA>        E 249775168        0 +42.5611933 -090.2049416
## 4 G5420  <NA>        E 359206625   847352 +44.4860642 -090.9093804
## 5 G5420  <NA>        E 927303552 16033326 +44.3935064 -090.2768746
## 6 G5420  <NA>        E  54819159   853927 +43.3223155 -087.9277005
##                                     NAME.y   variable estimate   moe
## 1 Mineral Point School District, Wisconsin B19013_001    60463  5579
## 2     Cuba City School District, Wisconsin B19013_001    61131  5319
## 3    Shullsburg School District, Wisconsin B19013_001    55625 12133
## 4   Alma Center School District, Wisconsin B19013_001    49907  4810
## 5    Pittsville School District, Wisconsin B19013_001    58523  3982
## 6       Grafton School District, Wisconsin B19013_001    80185  4944
##                         geometry
## 1 MULTIPOLYGON (((-89.96697 4...
## 2 MULTIPOLYGON (((-90.28938 4...
## 3 MULTIPOLYGON (((-90.21101 4...
## 4 MULTIPOLYGON (((-91.0652 44...
## 5 MULTIPOLYGON (((-90.55875 4...
## 6 MULTIPOLYGON (((-87.96731 4...
# 
wi_school_joined_interactive <- mapview(wi_school_joined, zcol="estimate", legend=TRUE, 
                                        layer.name="chool district shpae에 2019년 median household income 맵핑")
# mapview로 인터랙티브하게 보기:
# 위스콘신 주를 school district shpae 위에 2019년 median household income
wi_school_joined_interactive@map

boundary 그리기예시

# dot-density mapping with ggplot2
wi_boundary <- counties(state ="WI", year=2019) 
## 
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |                                                                      |   1%
  |                                                                            
  |=                                                                     |   1%
  |                                                                            
  |=                                                                     |   2%
  |                                                                            
  |==                                                                    |   2%
  |                                                                            
  |==                                                                    |   3%
  |                                                                            
  |==                                                                    |   4%
  |                                                                            
  |===                                                                   |   4%
  |                                                                            
  |===                                                                   |   5%
  |                                                                            
  |====                                                                  |   5%
  |                                                                            
  |====                                                                  |   6%
  |                                                                            
  |=====                                                                 |   6%
  |                                                                            
  |=====                                                                 |   7%
  |                                                                            
  |=====                                                                 |   8%
  |                                                                            
  |======                                                                |   8%
  |                                                                            
  |======                                                                |   9%
  |                                                                            
  |=======                                                               |   9%
  |                                                                            
  |=======                                                               |  10%
  |                                                                            
  |=======                                                               |  11%
  |                                                                            
  |========                                                              |  11%
  |                                                                            
  |========                                                              |  12%
  |                                                                            
  |=========                                                             |  12%
  |                                                                            
  |=========                                                             |  13%
  |                                                                            
  |=========                                                             |  14%
  |                                                                            
  |==========                                                            |  14%
  |                                                                            
  |==========                                                            |  15%
  |                                                                            
  |===========                                                           |  15%
  |                                                                            
  |===========                                                           |  16%
  |                                                                            
  |============                                                          |  16%
  |                                                                            
  |============                                                          |  17%
  |                                                                            
  |============                                                          |  18%
  |                                                                            
  |=============                                                         |  18%
  |                                                                            
  |=============                                                         |  19%
  |                                                                            
  |==============                                                        |  19%
  |                                                                            
  |==============                                                        |  20%
  |                                                                            
  |==============                                                        |  21%
  |                                                                            
  |===============                                                       |  21%
  |                                                                            
  |===============                                                       |  22%
  |                                                                            
  |================                                                      |  22%
  |                                                                            
  |================                                                      |  23%
  |                                                                            
  |================                                                      |  24%
  |                                                                            
  |=================                                                     |  24%
  |                                                                            
  |=================                                                     |  25%
  |                                                                            
  |==================                                                    |  25%
  |                                                                            
  |==================                                                    |  26%
  |                                                                            
  |===================                                                   |  26%
  |                                                                            
  |===================                                                   |  27%
  |                                                                            
  |===================                                                   |  28%
  |                                                                            
  |====================                                                  |  28%
  |                                                                            
  |====================                                                  |  29%
  |                                                                            
  |=====================                                                 |  29%
  |                                                                            
  |=====================                                                 |  30%
  |                                                                            
  |=====================                                                 |  31%
  |                                                                            
  |======================                                                |  31%
  |                                                                            
  |======================                                                |  32%
  |                                                                            
  |=======================                                               |  32%
  |                                                                            
  |=======================                                               |  33%
  |                                                                            
  |=======================                                               |  34%
  |                                                                            
  |========================                                              |  34%
  |                                                                            
  |========================                                              |  35%
  |                                                                            
  |=========================                                             |  35%
  |                                                                            
  |=========================                                             |  36%
  |                                                                            
  |==========================                                            |  36%
  |                                                                            
  |==========================                                            |  37%
  |                                                                            
  |==========================                                            |  38%
  |                                                                            
  |===========================                                           |  38%
  |                                                                            
  |===========================                                           |  39%
  |                                                                            
  |============================                                          |  39%
  |                                                                            
  |============================                                          |  40%
  |                                                                            
  |============================                                          |  41%
  |                                                                            
  |=============================                                         |  41%
  |                                                                            
  |=============================                                         |  42%
  |                                                                            
  |==============================                                        |  42%
  |                                                                            
  |==============================                                        |  43%
  |                                                                            
  |==============================                                        |  44%
  |                                                                            
  |===============================                                       |  44%
  |                                                                            
  |===============================                                       |  45%
  |                                                                            
  |================================                                      |  45%
  |                                                                            
  |================================                                      |  46%
  |                                                                            
  |=================================                                     |  46%
  |                                                                            
  |=================================                                     |  47%
  |                                                                            
  |=================================                                     |  48%
  |                                                                            
  |==================================                                    |  48%
  |                                                                            
  |==================================                                    |  49%
  |                                                                            
  |===================================                                   |  49%
  |                                                                            
  |===================================                                   |  50%
  |                                                                            
  |===================================                                   |  51%
  |                                                                            
  |====================================                                  |  51%
  |                                                                            
  |====================================                                  |  52%
  |                                                                            
  |=====================================                                 |  52%
  |                                                                            
  |=====================================                                 |  53%
  |                                                                            
  |=====================================                                 |  54%
  |                                                                            
  |======================================                                |  54%
  |                                                                            
  |======================================                                |  55%
  |                                                                            
  |=======================================                               |  55%
  |                                                                            
  |=======================================                               |  56%
  |                                                                            
  |========================================                              |  56%
  |                                                                            
  |========================================                              |  57%
  |                                                                            
  |========================================                              |  58%
  |                                                                            
  |=========================================                             |  58%
  |                                                                            
  |=========================================                             |  59%
  |                                                                            
  |==========================================                            |  59%
  |                                                                            
  |==========================================                            |  60%
  |                                                                            
  |==========================================                            |  61%
  |                                                                            
  |===========================================                           |  61%
  |                                                                            
  |===========================================                           |  62%
  |                                                                            
  |============================================                          |  62%
  |                                                                            
  |============================================                          |  63%
  |                                                                            
  |============================================                          |  64%
  |                                                                            
  |=============================================                         |  64%
  |                                                                            
  |=============================================                         |  65%
  |                                                                            
  |==============================================                        |  65%
  |                                                                            
  |==============================================                        |  66%
  |                                                                            
  |===============================================                       |  66%
  |                                                                            
  |===============================================                       |  67%
  |                                                                            
  |===============================================                       |  68%
  |                                                                            
  |================================================                      |  68%
  |                                                                            
  |================================================                      |  69%
  |                                                                            
  |=================================================                     |  69%
  |                                                                            
  |=================================================                     |  70%
  |                                                                            
  |=================================================                     |  71%
  |                                                                            
  |==================================================                    |  71%
  |                                                                            
  |==================================================                    |  72%
  |                                                                            
  |===================================================                   |  72%
  |                                                                            
  |===================================================                   |  73%
  |                                                                            
  |===================================================                   |  74%
  |                                                                            
  |====================================================                  |  74%
  |                                                                            
  |====================================================                  |  75%
  |                                                                            
  |=====================================================                 |  75%
  |                                                                            
  |=====================================================                 |  76%
  |                                                                            
  |======================================================                |  76%
  |                                                                            
  |======================================================                |  77%
  |                                                                            
  |======================================================                |  78%
  |                                                                            
  |=======================================================               |  78%
  |                                                                            
  |=======================================================               |  79%
  |                                                                            
  |========================================================              |  79%
  |                                                                            
  |========================================================              |  80%
  |                                                                            
  |========================================================              |  81%
  |                                                                            
  |=========================================================             |  81%
  |                                                                            
  |=========================================================             |  82%
  |                                                                            
  |==========================================================            |  82%
  |                                                                            
  |==========================================================            |  83%
  |                                                                            
  |==========================================================            |  84%
  |                                                                            
  |===========================================================           |  84%
  |                                                                            
  |===========================================================           |  85%
  |                                                                            
  |============================================================          |  85%
  |                                                                            
  |============================================================          |  86%
  |                                                                            
  |=============================================================         |  86%
  |                                                                            
  |=============================================================         |  87%
  |                                                                            
  |=============================================================         |  88%
  |                                                                            
  |==============================================================        |  88%
  |                                                                            
  |==============================================================        |  89%
  |                                                                            
  |===============================================================       |  89%
  |                                                                            
  |===============================================================       |  90%
  |                                                                            
  |===============================================================       |  91%
  |                                                                            
  |================================================================      |  91%
  |                                                                            
  |================================================================      |  92%
  |                                                                            
  |=================================================================     |  92%
  |                                                                            
  |=================================================================     |  93%
  |                                                                            
  |=================================================================     |  94%
  |                                                                            
  |==================================================================    |  94%
  |                                                                            
  |==================================================================    |  95%
  |                                                                            
  |===================================================================   |  95%
  |                                                                            
  |===================================================================   |  96%
  |                                                                            
  |====================================================================  |  96%
  |                                                                            
  |====================================================================  |  97%
  |                                                                            
  |====================================================================  |  98%
  |                                                                            
  |===================================================================== |  98%
  |                                                                            
  |===================================================================== |  99%
  |                                                                            
  |======================================================================|  99%
  |                                                                            
  |======================================================================| 100%
ggplot()+
  geom_sf(data= wi_boundary, color=NA, fill="white")

# 참고: places()는 인구와 사이즈에 따라 city, village 등의 정의가 다름 

tigris 로 지형 데이터 가져오기: roads, waters

# tigris / roads
mk_roads<-roads(state="WI", county="Milwaukee", year=NULL)%>%
  filter(RTTYP %in% c("I", "S", "U"))
## 
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |=                                                                     |   1%
  |                                                                            
  |=                                                                     |   2%
  |                                                                            
  |==                                                                    |   3%
  |                                                                            
  |===                                                                   |   4%
  |                                                                            
  |===                                                                   |   5%
  |                                                                            
  |====                                                                  |   5%
  |                                                                            
  |====                                                                  |   6%
  |                                                                            
  |=====                                                                 |   6%
  |                                                                            
  |=====                                                                 |   7%
  |                                                                            
  |======                                                                |   8%
  |                                                                            
  |======                                                                |   9%
  |                                                                            
  |=======                                                               |   9%
  |                                                                            
  |=======                                                               |  10%
  |                                                                            
  |=======                                                               |  11%
  |                                                                            
  |========                                                              |  11%
  |                                                                            
  |========                                                              |  12%
  |                                                                            
  |=========                                                             |  13%
  |                                                                            
  |==========                                                            |  14%
  |                                                                            
  |==========                                                            |  15%
  |                                                                            
  |===========                                                           |  15%
  |                                                                            
  |===========                                                           |  16%
  |                                                                            
  |============                                                          |  17%
  |                                                                            
  |=============                                                         |  18%
  |                                                                            
  |=============                                                         |  19%
  |                                                                            
  |==============                                                        |  20%
  |                                                                            
  |===============                                                       |  21%
  |                                                                            
  |===============                                                       |  22%
  |                                                                            
  |================                                                      |  22%
  |                                                                            
  |================                                                      |  23%
  |                                                                            
  |=================                                                     |  24%
  |                                                                            
  |=================                                                     |  25%
  |                                                                            
  |==================                                                    |  26%
  |                                                                            
  |===================                                                   |  27%
  |                                                                            
  |===================                                                   |  28%
  |                                                                            
  |====================                                                  |  28%
  |                                                                            
  |====================                                                  |  29%
  |                                                                            
  |=====================                                                 |  29%
  |                                                                            
  |=====================                                                 |  30%
  |                                                                            
  |======================                                                |  31%
  |                                                                            
  |======================                                                |  32%
  |                                                                            
  |=======================                                               |  32%
  |                                                                            
  |=======================                                               |  33%
  |                                                                            
  |=======================                                               |  34%
  |                                                                            
  |========================                                              |  34%
  |                                                                            
  |========================                                              |  35%
  |                                                                            
  |=========================                                             |  36%
  |                                                                            
  |==========================                                            |  37%
  |                                                                            
  |==========================                                            |  38%
  |                                                                            
  |===========================                                           |  38%
  |                                                                            
  |===========================                                           |  39%
  |                                                                            
  |============================                                          |  39%
  |                                                                            
  |============================                                          |  40%
  |                                                                            
  |=============================                                         |  41%
  |                                                                            
  |==============================                                        |  42%
  |                                                                            
  |==============================                                        |  43%
  |                                                                            
  |===============================                                       |  44%
  |                                                                            
  |===============================                                       |  45%
  |                                                                            
  |================================                                      |  45%
  |                                                                            
  |================================                                      |  46%
  |                                                                            
  |=================================                                     |  47%
  |                                                                            
  |=================================                                     |  48%
  |                                                                            
  |==================================                                    |  48%
  |                                                                            
  |==================================                                    |  49%
  |                                                                            
  |===================================                                   |  50%
  |                                                                            
  |====================================                                  |  51%
  |                                                                            
  |====================================                                  |  52%
  |                                                                            
  |=====================================                                 |  52%
  |                                                                            
  |=====================================                                 |  53%
  |                                                                            
  |======================================                                |  54%
  |                                                                            
  |======================================                                |  55%
  |                                                                            
  |=======================================                               |  55%
  |                                                                            
  |=======================================                               |  56%
  |                                                                            
  |========================================                              |  57%
  |                                                                            
  |========================================                              |  58%
  |                                                                            
  |=========================================                             |  59%
  |                                                                            
  |==========================================                            |  60%
  |                                                                            
  |==========================================                            |  61%
  |                                                                            
  |===========================================                           |  61%
  |                                                                            
  |===========================================                           |  62%
  |                                                                            
  |============================================                          |  62%
  |                                                                            
  |============================================                          |  63%
  |                                                                            
  |=============================================                         |  64%
  |                                                                            
  |==============================================                        |  65%
  |                                                                            
  |==============================================                        |  66%
  |                                                                            
  |===============================================                       |  67%
  |                                                                            
  |===============================================                       |  68%
  |                                                                            
  |================================================                      |  68%
  |                                                                            
  |================================================                      |  69%
  |                                                                            
  |=================================================                     |  70%
  |                                                                            
  |=================================================                     |  71%
  |                                                                            
  |==================================================                    |  71%
  |                                                                            
  |==================================================                    |  72%
  |                                                                            
  |===================================================                   |  72%
  |                                                                            
  |===================================================                   |  73%
  |                                                                            
  |====================================================                  |  74%
  |                                                                            
  |=====================================================                 |  75%
  |                                                                            
  |=====================================================                 |  76%
  |                                                                            
  |======================================================                |  77%
  |                                                                            
  |======================================================                |  78%
  |                                                                            
  |=======================================================               |  78%
  |                                                                            
  |=======================================================               |  79%
  |                                                                            
  |========================================================              |  80%
  |                                                                            
  |=========================================================             |  81%
  |                                                                            
  |=========================================================             |  82%
  |                                                                            
  |==========================================================            |  83%
  |                                                                            
  |===========================================================           |  84%
  |                                                                            
  |===========================================================           |  85%
  |                                                                            
  |============================================================          |  85%
  |                                                                            
  |============================================================          |  86%
  |                                                                            
  |=============================================================         |  87%
  |                                                                            
  |==============================================================        |  88%
  |                                                                            
  |==============================================================        |  89%
  |                                                                            
  |===============================================================       |  89%
  |                                                                            
  |===============================================================       |  90%
  |                                                                            
  |===============================================================       |  91%
  |                                                                            
  |================================================================      |  91%
  |                                                                            
  |================================================================      |  92%
  |                                                                            
  |=================================================================     |  93%
  |                                                                            
  |=================================================================     |  94%
  |                                                                            
  |==================================================================    |  94%
  |                                                                            
  |==================================================================    |  95%
  |                                                                            
  |===================================================================   |  95%
  |                                                                            
  |===================================================================   |  96%
  |                                                                            
  |====================================================================  |  97%
  |                                                                            
  |===================================================================== |  98%
  |                                                                            
  |======================================================================|  99%
  |                                                                            
  |======================================================================| 100%
str(mk_roads)
## Classes 'sf' and 'data.frame':   122 obs. of  5 variables:
##  $ LINEARID: chr  "1106073834243" "1106088114422" "1104492729479" "1106087287954" ...
##  $ FULLNAME: chr  "State Hwy 100" "US Hwy 41" "State Hwy 24" "I- 94" ...
##  $ RTTYP   : chr  "S" "U" "S" "I" ...
##  $ MTFCC   : chr  "S1200" "S1100" "S1200" "S1100" ...
##  $ geometry:sfc_LINESTRING of length 122; first list element:  'XY' num [1:92, 1:2] -88 -88 -88 -88 -88 ...
##  - attr(*, "sf_column")= chr "geometry"
##  - attr(*, "agr")= Factor w/ 3 levels "constant","aggregate",..: NA NA NA NA
##   ..- attr(*, "names")= chr [1:4] "LINEARID" "FULLNAME" "RTTYP" "MTFCC"
##  - attr(*, "tigris")= chr "road"
plot(mk_roads$geometry, col="lightgray")

# tigris / water 
mk_water <- area_water("WI", "Milwaukee")
## 
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |=========                                                             |  13%
  |                                                                            
  |==================                                                    |  26%
  |                                                                            
  |====================                                                  |  29%
  |                                                                            
  |==============================                                        |  43%
  |                                                                            
  |=====================================                                 |  52%
  |                                                                            
  |==============================================                        |  66%
  |                                                                            
  |=======================================================               |  79%
  |                                                                            
  |============================================================          |  86%
  |                                                                            
  |===================================================================== |  99%
  |                                                                            
  |======================================================================| 100%
plot(mk_water$geometry, col="lightblue")

tidycensus 로 인종별 인구수 가져오기 1: breakdown

breakdown The population breakdown used when product = “characteristics”. Acceptable values are “AGEGROUP”, “RACE”, “SEX”, and “HISP”, for Hispanic/Not Hispanic. These values can be combined in a vector, returning population estimates in the value column for all combinations of these breakdowns. year The data year (defaults to 2019)

# breakdown The population breakdown used when product = "characteristics". 
# Acceptable values are "AGEGROUP", "RACE", "SEX", and "HISP", for Hispanic/Not
# Hispanic. These values can be combined in a vector, 
# returning population estimates in the value column for all combinations of these breakdowns.
# year The data year (defaults to 2019)
mk_estimate <- get_estimates(geography = "county", 
                             product = "characteristics", 
                             breakdown = c("SEX", "AGEGROUP", "RACE"),  
                             breakdown_labels = TRUE, 
                             state = "WI", 
                             county = "Milwaukee")
str(mk_estimate)
## tibble [723 × 6] (S3: tbl_df/tbl/data.frame)
##  $ GEOID   : chr [1:723] "55079" "55079" "55079" "55079" ...
##  $ NAME    : chr [1:723] "Milwaukee County, Wisconsin" "Milwaukee County, Wisconsin" "Milwaukee County, Wisconsin" "Milwaukee County, Wisconsin" ...
##  $ value   : num [1:723] 945726 607355 257616 9295 44089 ...
##  $ SEX     : chr [1:723] "Both sexes" "Both sexes" "Both sexes" "Both sexes" ...
##  $ AGEGROUP: Factor w/ 32 levels "All ages","Age 0 to 4 years",..: 1 1 1 1 1 1 1 1 1 1 ...
##  $ RACE    : chr [1:723] "All races" "White alone" "Black alone" "American Indian and Alaska Native alone" ...
unique(mk_estimate$RACE)
##  [1] "All races"                                                         
##  [2] "White alone"                                                       
##  [3] "Black alone"                                                       
##  [4] "American Indian and Alaska Native alone"                           
##  [5] "Asian alone"                                                       
##  [6] "Native Hawaiian and Other Pacific Islander alone"                  
##  [7] "Two or more races"                                                 
##  [8] "White alone or in combination"                                     
##  [9] "Black alone or in combination"                                     
## [10] "American Indian and Alaska Native alone or in combination"         
## [11] "Asian alone or in combination"                                     
## [12] "Native Hawaiian and Other Pacific Islander alone or in combination"
unique(mk_estimate$AGEGROUP)
##  [1] All ages               Age 0 to 4 years       Age 5 to 9 years      
##  [4] Age 10 to 14 years     Age 15 to 19 years     Age 20 to 24 years    
##  [7] Age 25 to 29 years     Age 30 to 34 years     Age 35 to 39 years    
## [10] Age 40 to 44 years     Age 45 to 49 years     Age 50 to 54 years    
## [13] Age 55 to 59 years     Age 60 to 64 years     Age 65 to 69 years    
## [16] Age 70 to 74 years     Age 75 to 79 years     Age 80 to 84 years    
## [19] Age 85 years and older 18 to 64 years         Under 18 years        
## [22] 5 to 13 years          14 to 17 years         18 to 24 years        
## [25] 25 to 44 years         45 to 64 years         18 years and over     
## [28] 65 years and over      85 years and over      16 years and over     
## [31] 15 to 44 years         Median age            
## 32 Levels: All ages Age 0 to 4 years Age 5 to 9 years ... Median age
head(mk_estimate)
## # A tibble: 6 x 6
##   GEOID NAME                value SEX      AGEGROUP RACE                        
##   <chr> <chr>               <dbl> <chr>    <fct>    <chr>                       
## 1 55079 Milwaukee County,… 945726 Both se… All ages All races                   
## 2 55079 Milwaukee County,… 607355 Both se… All ages White alone                 
## 3 55079 Milwaukee County,… 257616 Both se… All ages Black alone                 
## 4 55079 Milwaukee County,…   9295 Both se… All ages American Indian and Alaska …
## 5 55079 Milwaukee County,…  44089 Both se… All ages Asian alone                 
## 6 55079 Milwaukee County,…    431 Both se… All ages Native Hawaiian and Other P…
# ggplot(mk_estimate, aes(value, color=factor(RACE)) +geom_bar()

tidycensus 로 인종별 인구수 가져오기 2

# 인종만 가져오기
mk_estimate_race <- get_estimates(geography = "county", 
                                 product = "characteristics", 
                                 breakdown = c("RACE"),  
                                 breakdown_labels = TRUE, 
                                 state = "WI", 
                                 county = "Milwaukee",
                                 geometry = TRUE) #좌표값 다운로드=true
## 
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |                                                                      |   1%
  |                                                                            
  |=                                                                     |   1%
  |                                                                            
  |=                                                                     |   2%
  |                                                                            
  |==                                                                    |   2%
  |                                                                            
  |==                                                                    |   3%
  |                                                                            
  |==                                                                    |   4%
  |                                                                            
  |===                                                                   |   4%
  |                                                                            
  |===                                                                   |   5%
  |                                                                            
  |====                                                                  |   5%
  |                                                                            
  |====                                                                  |   6%
  |                                                                            
  |=====                                                                 |   7%
  |                                                                            
  |=====                                                                 |   8%
  |                                                                            
  |======                                                                |   8%
  |                                                                            
  |======                                                                |   9%
  |                                                                            
  |=======                                                               |   9%
  |                                                                            
  |=======                                                               |  10%
  |                                                                            
  |=======                                                               |  11%
  |                                                                            
  |========                                                              |  11%
  |                                                                            
  |========                                                              |  12%
  |                                                                            
  |=========                                                             |  12%
  |                                                                            
  |=========                                                             |  13%
  |                                                                            
  |=========                                                             |  14%
  |                                                                            
  |==========                                                            |  14%
  |                                                                            
  |==========                                                            |  15%
  |                                                                            
  |===========                                                           |  15%
  |                                                                            
  |===========                                                           |  16%
  |                                                                            
  |============                                                          |  16%
  |                                                                            
  |============                                                          |  17%
  |                                                                            
  |============                                                          |  18%
  |                                                                            
  |=============                                                         |  18%
  |                                                                            
  |=============                                                         |  19%
  |                                                                            
  |==============                                                        |  19%
  |                                                                            
  |==============                                                        |  20%
  |                                                                            
  |==============                                                        |  21%
  |                                                                            
  |===============                                                       |  21%
  |                                                                            
  |===============                                                       |  22%
  |                                                                            
  |================                                                      |  22%
  |                                                                            
  |================                                                      |  23%
  |                                                                            
  |=================                                                     |  24%
  |                                                                            
  |=================                                                     |  25%
  |                                                                            
  |==================                                                    |  25%
  |                                                                            
  |==================                                                    |  26%
  |                                                                            
  |===================                                                   |  26%
  |                                                                            
  |===================                                                   |  27%
  |                                                                            
  |===================                                                   |  28%
  |                                                                            
  |====================                                                  |  28%
  |                                                                            
  |====================                                                  |  29%
  |                                                                            
  |=====================                                                 |  29%
  |                                                                            
  |=====================                                                 |  30%
  |                                                                            
  |=====================                                                 |  31%
  |                                                                            
  |======================                                                |  31%
  |                                                                            
  |======================                                                |  32%
  |                                                                            
  |=======================                                               |  32%
  |                                                                            
  |=======================                                               |  33%
  |                                                                            
  |=======================                                               |  34%
  |                                                                            
  |========================                                              |  34%
  |                                                                            
  |========================                                              |  35%
  |                                                                            
  |=========================                                             |  35%
  |                                                                            
  |=========================                                             |  36%
  |                                                                            
  |==========================                                            |  37%
  |                                                                            
  |==========================                                            |  38%
  |                                                                            
  |===========================                                           |  38%
  |                                                                            
  |===========================                                           |  39%
  |                                                                            
  |============================                                          |  39%
  |                                                                            
  |============================                                          |  40%
  |                                                                            
  |============================                                          |  41%
  |                                                                            
  |=============================                                         |  41%
  |                                                                            
  |=============================                                         |  42%
  |                                                                            
  |==============================                                        |  42%
  |                                                                            
  |==============================                                        |  43%
  |                                                                            
  |==============================                                        |  44%
  |                                                                            
  |===============================                                       |  44%
  |                                                                            
  |===============================                                       |  45%
  |                                                                            
  |================================                                      |  45%
  |                                                                            
  |================================                                      |  46%
  |                                                                            
  |=================================                                     |  47%
  |                                                                            
  |=================================                                     |  48%
  |                                                                            
  |==================================                                    |  48%
  |                                                                            
  |==================================                                    |  49%
  |                                                                            
  |===================================                                   |  49%
  |                                                                            
  |===================================                                   |  50%
  |                                                                            
  |===================================                                   |  51%
  |                                                                            
  |====================================                                  |  51%
  |                                                                            
  |====================================                                  |  52%
  |                                                                            
  |=====================================                                 |  52%
  |                                                                            
  |=====================================                                 |  53%
  |                                                                            
  |======================================                                |  54%
  |                                                                            
  |======================================                                |  55%
  |                                                                            
  |=======================================                               |  55%
  |                                                                            
  |=======================================                               |  56%
  |                                                                            
  |========================================                              |  56%
  |                                                                            
  |========================================                              |  57%
  |                                                                            
  |========================================                              |  58%
  |                                                                            
  |=========================================                             |  58%
  |                                                                            
  |=========================================                             |  59%
  |                                                                            
  |==========================================                            |  59%
  |                                                                            
  |==========================================                            |  60%
  |                                                                            
  |==========================================                            |  61%
  |                                                                            
  |===========================================                           |  61%
  |                                                                            
  |===========================================                           |  62%
  |                                                                            
  |============================================                          |  62%
  |                                                                            
  |============================================                          |  63%
  |                                                                            
  |=============================================                         |  64%
  |                                                                            
  |=============================================                         |  65%
  |                                                                            
  |==============================================                        |  65%
  |                                                                            
  |==============================================                        |  66%
  |                                                                            
  |===============================================                       |  66%
  |                                                                            
  |===============================================                       |  67%
  |                                                                            
  |===============================================                       |  68%
  |                                                                            
  |================================================                      |  68%
  |                                                                            
  |================================================                      |  69%
  |                                                                            
  |=================================================                     |  69%
  |                                                                            
  |=================================================                     |  70%
  |                                                                            
  |=================================================                     |  71%
  |                                                                            
  |==================================================                    |  71%
  |                                                                            
  |==================================================                    |  72%
  |                                                                            
  |===================================================                   |  72%
  |                                                                            
  |===================================================                   |  73%
  |                                                                            
  |====================================================                  |  74%
  |                                                                            
  |====================================================                  |  75%
  |                                                                            
  |=====================================================                 |  75%
  |                                                                            
  |=====================================================                 |  76%
  |                                                                            
  |======================================================                |  76%
  |                                                                            
  |======================================================                |  77%
  |                                                                            
  |======================================================                |  78%
  |                                                                            
  |=======================================================               |  78%
  |                                                                            
  |=======================================================               |  79%
  |                                                                            
  |========================================================              |  79%
  |                                                                            
  |========================================================              |  80%
  |                                                                            
  |========================================================              |  81%
  |                                                                            
  |=========================================================             |  81%
  |                                                                            
  |=========================================================             |  82%
  |                                                                            
  |==========================================================            |  82%
  |                                                                            
  |==========================================================            |  83%
  |                                                                            
  |==========================================================            |  84%
  |                                                                            
  |===========================================================           |  84%
  |                                                                            
  |===========================================================           |  85%
  |                                                                            
  |============================================================          |  85%
  |                                                                            
  |============================================================          |  86%
  |                                                                            
  |=============================================================         |  86%
  |                                                                            
  |=============================================================         |  87%
  |                                                                            
  |=============================================================         |  88%
  |                                                                            
  |==============================================================        |  88%
  |                                                                            
  |==============================================================        |  89%
  |                                                                            
  |===============================================================       |  89%
  |                                                                            
  |===============================================================       |  90%
  |                                                                            
  |===============================================================       |  91%
  |                                                                            
  |================================================================      |  91%
  |                                                                            
  |================================================================      |  92%
  |                                                                            
  |=================================================================     |  92%
  |                                                                            
  |=================================================================     |  93%
  |                                                                            
  |=================================================================     |  94%
  |                                                                            
  |==================================================================    |  94%
  |                                                                            
  |==================================================================    |  95%
  |                                                                            
  |===================================================================   |  95%
  |                                                                            
  |===================================================================   |  96%
  |                                                                            
  |====================================================================  |  96%
  |                                                                            
  |====================================================================  |  97%
  |                                                                            
  |====================================================================  |  98%
  |                                                                            
  |===================================================================== |  98%
  |                                                                            
  |===================================================================== |  99%
  |                                                                            
  |======================================================================|  99%
  |                                                                            
  |======================================================================| 100%
head(mk_estimate_race)
## Simple feature collection with 6 features and 4 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -88.06992 ymin: 42.84212 xmax: -87.82713 ymax: 43.19258
## Geodetic CRS:  NAD83
## # A tibble: 6 x 5
##   GEOID NAME         value RACE                                         geometry
##   <chr> <chr>        <dbl> <chr>                              <MULTIPOLYGON [°]>
## 1 55079 Milwaukee … 945726 All races       (((-88.06959 42.86726, -88.06959 42.…
## 2 55079 Milwaukee … 607355 White alone     (((-88.06959 42.86726, -88.06959 42.…
## 3 55079 Milwaukee … 257616 Black alone     (((-88.06959 42.86726, -88.06959 42.…
## 4 55079 Milwaukee …   9295 American India… (((-88.06959 42.86726, -88.06959 42.…
## 5 55079 Milwaukee …  44089 Asian alone     (((-88.06959 42.86726, -88.06959 42.…
## 6 55079 Milwaukee …    431 Native Hawaiia… (((-88.06959 42.86726, -88.06959 42.…
simple_race <- c("White alone", "Black alone", "Asian alone", "American Indian and Alaska Native alone")
mk_estimate_race_simple <-mk_estimate_race %>% group_by(RACE) %>% filter(RACE %in% simple_race)
head(mk_estimate_race_simple)
## Simple feature collection with 4 features and 4 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -88.06992 ymin: 42.84212 xmax: -87.82713 ymax: 43.19258
## Geodetic CRS:  NAD83
## # A tibble: 4 x 5
## # Groups:   RACE [4]
##   GEOID NAME         value RACE                                         geometry
##   <chr> <chr>        <dbl> <chr>                              <MULTIPOLYGON [°]>
## 1 55079 Milwaukee … 607355 White alone    (((-88.06959 42.86726, -88.06959 42.8…
## 2 55079 Milwaukee … 257616 Black alone    (((-88.06959 42.86726, -88.06959 42.8…
## 3 55079 Milwaukee …   9295 American Indi… (((-88.06959 42.86726, -88.06959 42.8…
## 4 55079 Milwaukee …  44089 Asian alone    (((-88.06959 42.86726, -88.06959 42.8…

tidycensus 로 데이터 인종별 인구수 가져와 점으로 표현하기

mk_dots <- map(c("White alone", "Black alone", "Asian alone", "American Indian and Alaska Native alone"), 
               function(group) {
                 mk_estimate_race %>% 
                   filter(RACE == group) %>% 
                   st_sample(., size = .$value / 100) %>% 
                   st_sf() %>% 
                   mutate(group = group)
               }) %>% 
  reduce(rbind) %>% 
  group_by(group) %>% 
  summarize()
## Warning in st_sample.sfc(st_geometry(x), size, ...): size is not an integer

## Warning in st_sample.sfc(st_geometry(x), size, ...): size is not an integer

## Warning in st_sample.sfc(st_geometry(x), size, ...): size is not an integer

## Warning in st_sample.sfc(st_geometry(x), size, ...): size is not an integer
## Warning in st_sample.sfc(x = x, size = size, ..., type = type, exact = FALSE):
## size is not an integer
## Warning in st_sample.sfc(x, size = diff, ..., type, exact = FALSE, by_polygon =
## by_polygon): size is not an integer
str(mk_dots)
## sf [4 × 2] (S3: sf/tbl_df/tbl/data.frame)
##  $ group   : chr [1:4] "American Indian and Alaska Native alone" "Asian alone" "Black alone" "White alone"
##  $ geometry:sfc_MULTIPOINT of length 4; first list element:  'XY' num [1:92, 1:2] -87.9 -87.9 -87.9 -87.9 -88 ...
##  - attr(*, "sf_column")= chr "geometry"
##  - attr(*, "agr")= Factor w/ 3 levels "constant","aggregate",..: NA
##   ..- attr(*, "names")= chr "group"

Final map: tigris 지형위에 데이터 오버랩핑

ggplot()+
  geom_sf(data= mk_county_median_income, color=NA, fill="white")+
  geom_sf(data=mk_dots, aes(color=group, fill=group), size=0.1) +
  geom_sf(data = mk_water, color="blue", fill="lightblue") +
  geom_sf(data= mk_roads, color="black") +
  labs(title = "The racial geography of Milwaukee county, Wisconsin", 
       subtitle = "2019 decennial U.S. Census", 
       fill = "", 
       caption = "1 dot = approximately 100 people.\nData acquired with the R tidycensus and tigris packages.")