본문 바로가기

데이터 분석/R

Part ⅩⅠ: 지도 시각화

[ ⅰ. 미국 주별 범죄율 단계 구분도 ]

  1. 패키지 준비 → 데이터 준비

  2. 미국 주(state) 지도 데이터 준비

  3. 단계 구분도 작성

  4. 인터랙티브 단계 구분도

 

ggChropleth() data aes(fill= ) aes(map_id= ) map interactive
표현할 데이터 색으로 표현할
변수
지역 기준 변수 지도 데이터 마우스 움직임에 반응하는 구분도
더보기
### Step 1:
install.packages("mapproj")
install.packages("ggiraphExtra")
library(ggiraphExtra)
library(tibble)

str(USArrests)
head(USArrests)

# 변수로 처리되지 않은 지역명 → state로 변수화
crime <- rownames_to_column(USArrests, var="state")
crime$state <- tolower(crime$state)
head(crime)



### Step 2:
install.packages("maps")
library(ggplot2)

states_map <- map_data("state")
str(states_map)



### Step 3:
ggChoropleth(data = crime, aes(fill=Murder, map_id=state), map = states_map)



### Step 3:
ggChoropleth(data = crime, aes(fill=Murder, map_id=state), map = states_map, interactive = T)

 


[ ⅱ.  대한민국 시도별 인구/ 결핵환자 단계 구분도 ]

  1. 패키지 준비 → 데이터(인구/ 지도) 준비

  2. 단계 구분도 → 시각화
더보기
### Step 1:
install.packages("stringi") # for using kormaps2014 data
install.packages("devtools") # for using github download fn
devtools::install_github("cardiomoon/kormaps2014")
library(kormaps2014)
library(dplyr)

str(changeCode(korpop1))
str(changeCode(kormap1))

korpop1 <- rename(korpop1, pop='총인구_명', name='행정구역별_읍면동')

options(encoding = "UTF-8")
options(encoding = "CP949")

korpop1$name <- iconv(korpop1$name, "UTF-8", "CP949")

### Step 2:
library(ggplot2)
library(ggiraphExtra)

ggChoropleth(data = korpop1, aes(fill=pop, map_id=code, tooltip = name), map=kormap1, interactive = T)