데이터 시각화

R을 이용하여 용인시 수지구 지도 그리기

r-code-for-data-analysis 2023. 11. 1. 23:16

지도 데이터 다운로드
 
http://www.gisdeveloper.co.kr/?p=2332

대한민국 최신 행정구역(SHP) 다운로드 – GIS Developer

www.gisdeveloper.co.kr

 
1. 시도  : ctprvn.shp
2. 시군구 : sig.shp
3. 읍면동 : emd.shp
 
 
 
[패키지 로드 / 데이터 로드]

library(tidyverse)
library("rgdal")
library(magrittr)


map1 <-  readOGR("ctprvn.shp", encoding = 'CP949')#시도
map2<-  readOGR("sig.shp", encoding = 'CP949')#구
map3 <-  readOGR("emd.shp", encoding = 'CP949')#동


sido_map_info = map1@data
sigu_map_info = map2@data
dong_map_info = map3@data


sido_map = fortify(map1)
sigu_map = fortify(map2)
dong_map = fortify(map3)

sido_map_info %<>% 
  rownames_to_column('id')

sigu_map_info %<>% 
  rownames_to_column('id')

dong_map_info %<>% 
  rownames_to_column('id')

 
 
[전국 지도 그리기]

#시도 지도
ggplot(data = sido_map,
       aes(x = long, y = lat, 
           group = group, color = id)) + 
  geom_polygon(fill = "#FFFFFF") + 
  theme(legend.position = "none")


#시군구 지도
ggplot(data = sigu_map,
       aes(x = long, y = lat, 
           group = group, color = id)) + 
  geom_polygon(fill = "#FFFFFF") + 
  theme(legend.position = "none")

#읍면동 지도
ggplot(data = dong_map,
       aes(x = long, y = lat, 
           group = group, color = id)) + 
  geom_polygon(fill = "#FFFFFF")

 
 
[용인시 수지구 지도 그리기]
 

## 용인시

sido_map_info %>% filter(str_detect(CTP_KOR_NM ,"경기도"))
sigu_map_info %>% filter(str_detect(SIG_KOR_NM , "용인시"))
dong_map_info %>% filter(str_detect(EMD_KOR_NM , "죽전동"))

dong_map_info %>% filter(str_detect(EMD_CD , "41465")) %>% select(id) %>%
pull() %>% as.numeric()-> yongin
dong_map_info %>% filter(str_detect(EMD_CD , "41465")) -> sujigu

left_join(sujigu, dong_map %>% filter(id %in% sujigu$id)) %>% 
  group_by(EMD_KOR_NM,id) %>%  reframe(x=mean(long), y=mean(lat)) -> add



ggplot(data = dong_map %>% filter(id %in% sujigu$id),
       aes(x = long, y = lat,  fill = id)) + 
  geom_polygon() + 
  theme(legend.position = "none")+
  geom_text(data = add , aes(x = x, y = y, label = EMD_KOR_NM))+
  labs(title = "용인시 수지구 지도", x="", y="")+
  theme_bw()+
  theme(legend.position = "none")+
  theme(axis.text.x=element_blank(), 
        axis.text.y=element_blank())

 
 
[시각화]

 
 

728x90
반응형