mapview 패키지는 최신 R에서만 설치됨.
package_version(R.version)
sf, rgdal, sp 설치시 시스템 설정해야 하는 것이 있음.
GDAL설치시 맥킨토시에서 문제가 많았음. GDAL: 콘다로 제대로 설치 안되서 brew로 설치함.
brew install pkg-config
brew install gdal
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”))
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)
참고: API key는 개인정보이므로 공개하면 안됨
census_api_key("너의 API 코드 ")
# 설치된 api key 확인
Sys.getenv("CENSUS_API_KEY") # Check your API key
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
# 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"])
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))
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 " 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
# 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