# GADM 데이터 다운로드# gadm_data <- getData("GADM", country = "KOR", level = 1) #타임아웃# 데이터 확인# print(gadm_data)# sf 객체로 변환# gadm_sf <- st_as_sf(gadm_data)# 서울시 데이터 필터링# seoul_sf <- gadm_sf[gadm_sf$NAME_1 == "Seoul", ]# 데이터 확인# print(seoul_sf)# 서울시 지도 시각화# ggplot(data = seoul_sf) +geom_sf(fill = "lightblue", color = "black") + ggtitle("서울시 지도") + theme_minimal()
4. shape file을 사용한 지도 그리기
Code
# 필요한 패키지 설치# install.packages("sf")# install.packages("ggplot2")# 패키지 로드library(sf)library(ggplot2)# shapefile 경로 설정, 폴더의 sig 파일들이 같이 존재하도록 함shapefile_path <-"~/Desktop/R/sig.shp"# shapefile 읽기korea_s_sf <-st_read(shapefile_path, options ="ENCODING=CP949")
options: ENCODING=CP949
Reading layer `sig' from data source `/Users/leehyunjhin/Desktop/R/sig.shp' using driver `ESRI Shapefile'
Simple feature collection with 250 features and 3 fields
Geometry type: MULTIPOLYGON
Dimension: XY
Bounding box: xmin: 746110.3 ymin: 1458754 xmax: 1387948 ymax: 2068444
Projected CRS: PCS_ITRF2000_TM
Code
# ggplot2를 사용하여 시각화korea_sm <-ggplot() +geom_sf(data = korea_s_sf, fill ='white', color ='black')# 그래프 출력print(korea_sm)
Code
# 예시: korea_s가 SpatialPolygonsDataFrame인 경우----------------# SpatialPolygonsDataFrame 객체를 sf 객체로 변환# korea_s_sf <- st_as_sf(korea_s)# ggplot2를 사용하여 시각화# korea_sm <- ggplot() + geom_sf(data = korea_s_sf, fill = 'white', color = 'black')
5. shape file로 서울 지도 그리기
Code
library(dplyr)
Attaching package: 'dplyr'
The following objects are masked from 'package:raster':
intersect, select, union
The following objects are masked from 'package:stats':
filter, lag
The following objects are masked from 'package:base':
intersect, setdiff, setequal, union
Code
library(showtext)
Loading required package: sysfonts
Loading required package: showtextdb
Code
font_add_google("Noto Sans KR", "noto")showtext_auto() # 서울 지역 필터링seoul_s_sf <- korea_s_sf %>%filter(SIG_CD <=11740)# ggplot2를 사용하여 서울 지역 시각화seoul_map <-ggplot() +geom_sf(data = seoul_s_sf, aes(fill = SIG_KOR_NM), color ='black') +theme_minimal() +theme(text =element_text(family ="noto"))+labs(title ="서울 지도", fill ="구 이름")# 그래프 출력print(seoul_map)