May, 2018
if(!require(maps)){install.packages("maps") ;library(maps)}
if(!require(mapdata)){install.packages("mapdata") ;library(mapdata)}
map 데이터를 이용하기 위한 패키지 호출
* map library는 지도 정보와 지도를 그려주는 함수가 내장되어 있다. * mapdata library는 지도정보와 몇가지 예제 데이터가 있다.
par(mfrow = c(1,2)) map(database = "usa") map(database = "county")
par(mfrow = c(1,2))
map(database = 'world', region = 'South Korea')
map('world2Hires', 'South Korea')
data(us.cities) head(us.cities)
## name country.etc pop lat long capital ## 1 Abilene TX TX 113888 32.45 -99.74 0 ## 2 Akron OH OH 206634 41.08 -81.52 0 ## 3 Alameda CA CA 70069 37.77 -122.26 0 ## 4 Albany GA GA 75510 31.58 -84.18 0 ## 5 Albany NY NY 93576 42.67 -73.80 2 ## 6 Albany OR OR 45535 44.62 -123.09 0
map("state", "GEORGIA")
map.cities(us.cities, country = "GA")
map('world', fill = TRUE, col = rainbow(30))
* `world` 데이터 베이스에는 국가별 경계에 대한 위-경도 정보가 있음
* `fill=TRUE` 는 국가별 경계의 안쪽을 색칠하는 옵션임
* `col = rainbow(30)` 은 색칠할 팔레트 정보임.
+ 무지개 색을 30개로 분할하여 사용함.
* 색깔이 칠해지는 순서는 `world` 데이터 베이스에 저장된 국가의 폴리곤 지표 순이며,
`rainbow(30)` 팔레트에서 색을 하나씩 가져와서 칠함.
+ 미국의 알래스카와 미국 색깔이 다름
* 30개 국가경계의 안쪽이가 칠해지고 난 후 31번째는 다시 첫번째 색을 가져와서 칠함
data(unemp) data(county.fips) head(unemp,3)
## fips pop unemp ## 1 1001 23288 9.7 ## 2 1003 81706 9.1 ## 3 1005 9703 13.4
head(county.fips,3)
## fips polyname ## 1 1001 alabama,autauga ## 2 1003 alabama,baldwin ## 3 1005 alabama,barbour
* `unemp` 데이터는 county 식별번호(fips), 인구(pop), 실업률(enemp) 데이터를 가지고 있다. * `county.fips` 데이터는 county 식별번호(fips)를 가지고 있다. 그리고 이 식별번호는 `county` 데이터베이스가 가진 맵폴리곤 데이터의 그룹 인덱스(polyname)의 순서와 같다. * `county` 는 지도위에 그림을 그릴 폴리곤 데이터 베이스다. * `county`의 경계 안쪽을 칠하기 위해서 `county.fips` 식별번호 옆에 필요한 통계량을 순서대로 정리하면 된다.
unemp$colorBuckets <- as.numeric(cut(unemp$unemp,
c(0, 2, 4, 6, 8, 10, 100)))
colorsmatched <- unemp$colorBuckets[match(county.fips$fips, unemp$fips)]
* `cut()` 함수를 이용해서 실업율(0,2,4,6,8,10,100)에 따라서 범주를 생성한다.
* `colorsmatched`는 `fips`데이터가 몇 번째 범주에 속하는지 표시한다.
+ match 함수는 `county.fips$fips`에 들어 있는 값 하나하나가
unemp$fips에 몇 번째에 위치하는지 알려준다.
+ `unemp$colorBuckets[match(county.fips$fips, unemp$fips)]` 를 실행하면
우리가 사용하게 될 `unemp$colorBuckets`의 값을 `county.fips$fips`의 순서에
맞춰 정렬하게 된다.
+ unemp 데이터의 colorBuckets을 colorsmatched에 저장
colors = c("#F1EEF6","#D4B9DA","#C994C7","#DF65B0","#DD1C77","#980043")
if(!require(mapproj)){install.packages("mapproj") ;library(mapproj)}
map("county", col = colors[colorsmatched], fill = TRUE,
resolution = 0, lty = 0, projection = "polyconic")
* `col` 은 우리가 범주를 6개로 나누었으므로 6개로 잡는다.
* `map` 함수의 `col`과 `fill=TRUE` 옵션을 이용해서 county별 실업률을 도식한다.
* `resolution=0` 는 `county` DB 경계정보를 모두 사용한다.
* `lty=0` 을 사용해 경계를 표시하지 않는다.
* `projection` 은 구면좌표를 평면으로 정사형하는 방법을 결정한다.
(See mapproject {mapproj})
map("county", col = colors[colorsmatched], fill = TRUE,
resolution = 0, lty = 0, projection = "polyconic")
map("state", col = "white", fill = FALSE, add = TRUE, lty = 1,
lwd = 0.2,projection = "polyconic")
title("unemployment by county, 2009")
* `map` 함수를 이용하여 state의 경계를 추가
* `title` 함수를 이용하여 plot의 제목을 추가
* `add = TRUE` 로 덮어쓸 수 있다.
world나 world2에 있는 지역을 확인해보자if(!require(dplyr)){install.packages("dplyr") ;library(dplyr)}
if(!require(ggplot2)){install.packages("ggplot2") ;library(ggplot2)}
wm <- ggplot2::map_data('world')
wm %>% dplyr::select(region) %>% unique()%>%head()
## region ## 1 Aruba ## 12 Afghanistan ## 423 Angola ## 763 Anguilla ## 770 Albania ## 884 Finland
map('world','Angola', fill = T, col = 'orange')
ur <- wm %>% dplyr::select(region)%>%unique() grep( "Korea", ur$region )
## [1] 125 185
ur$region[c(125,185)]
## [1] "South Korea" "North Korea"
map("world", ur$region[c(125,185)],fill = T,
col = "blue")
if(!require(mapplots)){install.packages("mapplots") ;library(mapplots)}
if(!require(ggmap)){install.packages("ggmap") ;library(ggmap)}
if(!require(mapdata)){install.packages("mapdata") ;library(mapdata)}
map('worldHires', 'South Korea')
seoul_loc = geocode('seoul')
busan_loc = geocode('Busan')
add.pie(z=1:2,labels = c('a','b'),
x = seoul_loc$lon, y = seoul_loc$lat, radius = 0.5)
add.pie(z=4:3,labels = c('a','b'),
x = busan_loc$lon, y = busan_loc$lat, radius = 0.5)
* `ggmap` 패키지의 `geocode` 함수를 이용하여 서울과 부산의 위경도 좌표를 받음
* 저장된 위경도 좌표는 데이터프레임으로 위도(lat), 경도(log) 열을 가지고 있다
* `mapplots` 패키지의 `add.pie` 함수를 이용해 지도위에 직접 pie chart를 생성
* `radius` 옵션은 pie chart의 크기를 정해준다.