2012-04-04 2 views
8

집의 집, 방 및 침대를 각각 나타내는 3 개의 shp 파일이 있습니다. R을 사용하여 그래프에 그래프를 그려서 서로 겹치도록해야합니다. 나는 plot 함수에서 line을 사용하여 기존 줄거리 위에 새로운 줄을 그릴 수 있음을 알아 냈습니다. spplot에 해당하는 항목이 있습니까? 감사.그래프에서 spplot을 사용하여 여러 shp 파일을 그릴 때 R

+0

기본 플롯으로 할 수없는 spplot의 어떤면이 필요합니까? sp 객체를 사용하면 첫 번째 플롯을 플롯 한 다음 add = TRUE로 플롯하여 나머지를 오버레이합니다. 왜 믹스에 spplot을 가져 왔습니까? – Spacedman

답변

0

당신은 spplotsp.layout 인수를 사용할 수 있습니다. 또는 ggplot2를 사용할 수 있습니다. 일부 예제 코드 (테스트되지 않은) : ggplot2에서

library(ggplot2) 
shp1_data.frame = fortify(shp1) 
shp1_data.frame$id = "shp1" 
shp2_data.frame = fortify(shp2) 
shp2_data.frame$id = "shp2" 
shp = rbind(shp1_data.frame, shp2_data.frame) 

ggplot(aes(x = x, y = y, group = group, col = id), data = shp) + geom_path() 

는 데이터의 열은 플롯의 그래픽 저울에 연결되어 있습니다. 이 경우 x은 x 좌표이고, y은 y 좌표이며, group은 data.frame shp에서 어떤 점이 속하는 폴리곤을 지정하는 열이고 col은 다각형의 색상입니다. 내가 사용한 지오메트리는 geom_path이며 폴리곤 입력 data.frame을 기반으로 일련의 선을 그립니다. 또 다른 방법은 geom_poly을 사용하는 것입니다.이 방법도 다각형 채우기를 지원합니다.

17

다음은 latticeExtra 패키지에서 멋진 layer() 기능을 사용하여, 하나의 접근 방식 : 또는

# (1) Load required libraries 
library(sp) 
library(rgeos)  # For its readWKT() function 
library(latticeExtra) # For layer() 

# (2) Prepare some example data 
sp1 = readWKT("POLYGON((0 0,1 0,1 1,0 1,0 0))") 
sp2 = readWKT("POLYGON((0 1,0.5 1.5,1 1,0 1))") 
sp3 = readWKT("POLYGON((0.5 0,0.5 0.5,0.75 0.5,0.75 0, 0.5 0))") 

# spplot provides "Plot methods for spatial data with attributes", 
# so at least the first object plotted needs a (dummy) data.frame attached to it. 
spdf1 <- SpatialPolygonsDataFrame(sp1, data=data.frame(1), match.ID=1) 

# (3) Plot several layers in a single panel 
spplot(spdf1, xlim=c(-0.5, 2), ylim=c(-0.5, 2), 
     col.regions="grey90", colorkey=FALSE) + 
layer(sp.polygons(sp2, fill="saddlebrown")) + 
layer(sp.polygons(sp3, fill="yellow")) 

enter image description here

, 당신은 spplot()sp.layout= 인수를 통해 동일한 결과를 얻을 수 있습니다. (first=FALSE을 지정하면 '지붕'과 '문'spplot()의 첫 번째 인수로 주어진 회색 사각형 위에/후 플롯됩니다 보장합니다.)

spplot(spdf1, xlim=c(-0.5, 2), ylim=c(-0.5, 2), 
     col.regions="grey90", colorkey=FALSE, 
     sp.layout = list(list(sp2, fill="saddlebrown", first=FALSE), 
         list(sp3, fill="yellow", first=FALSE))) 
관련 문제