2016-04-21 106 views
3

我想用透明填充顏色來繪製類SpatialPolygons的對象。從SpatialPolygons-Class的幫助頁稍微更改示例:sp R包SpatialPolygons-Class plot transparent

# simple example, from vignette("sp"): 
Sr1 = Polygon(cbind(c(2,4,4,1,2),c(2,3,5,4,2))) 
Sr2 = Polygon(cbind(c(5,4,2,5),c(2,3,2,2))) 
Sr3 = Polygon(cbind(c(4,4,5,10,4),c(5,3,2,5,5))) 
Sr4 = Polygon(cbind(c(5,6,6,5,5),c(4,4,3,3,4)), hole = TRUE) 

Srs1 = Polygons(list(Sr1), "s1") 
Srs2 = Polygons(list(Sr2), "s2") 
Srs3 = Polygons(list(Sr3, Sr4), "s3/4") 
SpP = SpatialPolygons(list(Srs1,Srs2,Srs3), 1:3) 
plot(1,1,type="n",ylim=c(0,10),xlim=c(0,10),xlab="",ylab="",yaxt="n",xaxt="n") 
plot(SpP, col = paste0(c("#FF0000","#00FF00","#0000FF"),"55"), pbg="white",add=TRUE) 

工作正常。但是,如果我要放大,情節SpatialPolygons級方法無法處理的透明瞭:

plot(1,1,type="n",ylim=c(0,7),xlim=c(0,7),xlab="",ylab="",yaxt="n",xaxt="n") 
plot(SpP, col = paste0(c("#FF0000","#00FF00","#0000FF"),"55"), pbg="white",add=TRUE) 

有沒有辦法得到這個工作? (如果可能,我想保留傳統的plot函數)

回答

2

解決此問題的一種方法是剪切多邊形。

c1 <- Polygon(cbind(c(0,7,7,0, 0),c(0,0,7,7,0))) 
c2 <- Polygons(list(c1), "sclip") 
Pclip <- SpatialPolygons(list(c2)) 
library(rgeos) 
SpPclip <- gIntersection(SpP, Pclip, byid = T) 
plot(SpPclip, col = paste0(c("#FF0000","#00FF00","#0000FF"),"55"), pbg="white", 
    ylim = c(-1,10), xlim = c(-1,10)) 
plot(Pclip, add = T) 

輸出看起來是這樣的: Output of the clipping

+1

謝謝!這解決了它。注意:爲了得到與我的例子相同的圖形,必須擴展'Pclip'多邊形默認的繪圖區域的4%擴展:'c1 < - Polygon(cbind(c(-0.28,7.28,7.28, - 0.28,-0.28),c(-0.28,-0.28,7.28,7.28,-0.28)))'並且沒有限制的繪圖。 – Chris