2017-09-20 1 views
1

ggplot 코드 (코드 단순화 버전)를 사용하여 중국 내 학습 사이트의 래스터 객체 (월드 클림의 고도 데이터)로부터 입면도를 작성했습니다. 관련 래스터 객체는 worldclim.org에서 다운로드하여 래스터 패키지를 사용하여 data.frame으로 변환합니다. 이 플롯에 사용 된 데이터에 대한 link입니다. 명확성을 위해ggplot지도에서 osmar 객체의 도로를 플로팅

Picture of the map created in ggplot

# load library 
library("tidyverse") 
load(file = "gongga.RData") 

ggplot() + 
geom_raster(data = gongga, aes(x=x, y=y, fill = elev)) + 
coord_equal() + 
scale_fill_gradient(name = "Elevation", low = "grey0", high = "grey100") + 
scale_x_continuous(expand = c(0,0)) + 
scale_y_continuous(expand = c(0,0)) + 
theme(aspect.ratio=1/1, text = element_text(size=15)) 

나는지도에 도로를 추가하고 싶습니다. Openstreetmap에서 도로를 추출하는 osmar 패키지를 발견했습니다.

here의 코드를 사용하여 오른쪽 섹션의 도로를 추출하지만 기존 ggplot에 도로를 그릴 방법을 모르겠습니다.

# EXTRACT ROADS FROM OPENSTREETMAP AND PLOT THEM WITH RANDOM POINTS 
# Load libraries 
library('osmar') 
library('geosphere') 

# Define the spatial extend of the OSM data we want to retrieve 
moxi.box <- center_bbox(center_lon = 102.025, center_lat = 29.875, 
width = 10000, height = 10000) 

# Download all osm data inside this area 
api <- osmsource_api() 
moxi <- get_osm(moxi.box, source = api) 

# Find highways 
ways <- find(moxi, way(tags(k == "highway"))) 
ways <- find_down(moxi, way(ways)) 
ways <- subset(moxi, ids = ways) 

# SpatialLinesDataFrame object 
hw_lines <- as_sp(ways, "lines") 

# Plot points 
plot(hw_lines, xlab = "Lon", ylab = "Lat") 
box() 

개체가 ggplot에 플롯하기 위해 변환이 필요합니까? 아니면 osmar 패키지보다 나은 솔루션이 있습니까?

답변

0

당신은 SpatialLinesDataFramefortify 다음 ggplot

fortify(hw_lines) %>% 
    ggplot(aes(x = long, y = lat, group = group)) + 
    geom_path() 

group 미적 함께 하나 개의 긴 라인으로 모든 도로를 결합에서 ggplot을 중지하도록 플롯 할 수 있습니다.

+0

감사합니다. Richard. 완벽하게 작동합니다. – Aud