geo_data

1. rnaturalearth패키지로 세계지도, 국가지도 그리기

Code
# install.packages("sf")
library(rnaturalearth)
library(sf)
Linking to GEOS 3.11.0, GDAL 3.5.3, PROJ 9.1.0; sf_use_s2() is TRUE
Code
library(ggplot2)

# 세계지도 데이터, 우리나라 데이터 불러오기 (sf 객체로 반환)
ne_map <- ne_countries(scale = "small", returnclass = "sf")
ne_map_k <- ne_countries(scale = "medium", returnclass = "sf",country = "South Korea")
# 지도 그리기
ggplot(data = ne_map) + 
  geom_sf(fill = "#FFFFFF", color = "blue") + ggtitle("World Map")

Code
ggplot(data = ne_map_k) + 
  geom_sf(fill = "#FFFFFF", color = "red") + ggtitle("Korea Map")

Code
# 서울시 지도 그리기
seoul <- subset(korea, name == "Seoul")
Error in eval(expr, envir, enclos): object 'korea' not found
Code
ggplot(data = seoul) +
  geom_sf(fill = "lightblue", color = "black") +
  ggtitle("Seoul, South Korea") #단위, 경계 문제로 안그려짐  
Error in eval(expr, envir, enclos): object 'seoul' not found

2. geodata 패키지 활용: 버전 불일치

Code
install.packages("geodata") # 버전 안맞음 
Error in contrib.url(repos, "source"): trying to use CRAN without setting a mirror
Code
library(geodata)
Error in library(geodata): there is no package called 'geodata'
Code
library(sf)
library(ggplot2)

# geodata 패키지를 사용하여 대한민국의 GADM 데이터를 다운로드
gadm_data <- geodata::gadm("KOR", level = 2, path = tempdir())
Error in loadNamespace(x): there is no package called 'geodata'
Code
# sf 객체로 변환
gadm_sf <- st_as_sf(gadm_data)
Error in eval(expr, envir, enclos): object 'gadm_data' not found
Code
# 서울시 데이터 필터링
seoul_sf <- gadm_sf[gadm_sf$NAME_1 == "Seoul", ]
Error in eval(expr, envir, enclos): object 'gadm_sf' not found
Code
# 데이터 확인
print(seoul_sf)
Error in eval(expr, envir, enclos): object 'seoul_sf' not found
Code
# 서울시 지도 시각화
ggplot(data = seoul_sf) +
  geom_sf(fill = "lightblue", color = "black") +
  ggtitle("서울시 지도") +
  theme_minimal()
Error in eval(expr, envir, enclos): object 'seoul_sf' not found

3. raster package> get data 활용: 안됨.

Code
#install.packages("sf")
#install.packages("ggplot2")
#install.packages("raster")

library(sf)
library(ggplot2)
library(raster)
Loading required package: sp
Code
# 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)