knitr::opts_chunk$set(echo = TRUE)
웹R의 운영자 문건웅입니다. 2014년 한국행정지도를 이용하여 단계구분도를 쉽게 만들 수 있게 해 주는 kormaps2014 패키지를 소개합니다. Shape데이타를 데이타프레임으로 미리 변환하였으므로 ggplot2등을 이용하여 쉽게 단계구분도를 그릴 수 있습니다. 제가 만든 다른 피키지 moonBook2에 있는 ggChoropleth()함수를 이용하면 보다 쉽게 단계구분도를 그릴 수 있습니다. static image뿐만 아니라 tooltip 사용 및 축소 확대 가능한 interactive plot도 쉽게 그릴 수 있습니다.
kormaps패키지는 github에 있습니다. CRAN에 올릴 수는 있으나 한글을 모두 unicode로 변환해야 해서 사용하기 번거로와 github에 올렸습니다. github에 있는 패키지를 설치하려면 devtools 패키지에 있는 install_github()함수를 사용합니다.
install.packages("devtools") devtools::install_github("cardiomoon/kormaps2014")
moonBook2패키지를 설치하시려면 다음과 같이 합니다. 처음 설치하는 경우 많은 패키지를 설치하므로 시간이 조금 걸립니다.
devtools::install_github("cardiomoon/moonBook2")
패키지를 사용하려면 다음과 같이 패키지를 불러옵니다.
library(kormaps2014) library(moonBook2)
kormaps 패키지에는 areacode, kormap1, kormap2, kormap3, korpop1, korpop2, korpop3, tbc데이타가 포함되어 있습니다.
areacode
str(kormap1) str(kormap2) str(kormap3)
1.korpop1 : 2015년 센서스데이터(시도별) 2.korpop2 : 2015년 센서스데이터(시군구별) 3.korpop3 : 2015년 센서스데이터(읍면동)
str(korpop1) str(korpop2) str(korpop3)
다음으로는 ggplot2를 이용한 단계구분도를 그려보겠습니다. 2015년 인구총조사 중 "총인구_명"으로 단계구분도를 그리려면 다음과 같이 합니다.
library(ggplot2) theme_set(theme_gray(base_family="NanumGothic")) ggplot(korpop1,aes(map_id=code,fill=총인구_명))+ geom_map(map=kormap1,colour="black",size=0.1)+ expand_limits(x=kormap1$long,y=kormap1$lat)+ scale_fill_gradientn(colours=c('white','orange','red'))+ ggtitle("2015년도 시도별 인구분포도")+ coord_map()
moonBook2패키지에 있는 ggChoropleth()함수를 이용하면 훨씬 간편하게 단계구분도를 그릴 수 있습니다. 다음은 시군구별, 읍면동별 단계구분도의 예제입니다.
ggChoropleth(korpop2,kormap2,fillvar="남자_명") ggChoropleth(korpop3,kormap3,fillvar="주택_계_호")
ggChoropleth()함수의 subarea인수를 이용하면 지도의 일부를 쉽게 그릴 수 있습니다. 예를 들어 호남지방의 단계구분도만 그리려면 다음과같이 할수 있습니다.
ggChoropleth(korpop3,kormap3,fillvar="총인구_명",subarea=c("전라","광주"))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.