2014-09-19 71 views
0

我正在同一圖上繪製幾個n邊多邊形。讓說:繪製具有不同顏色的多邊形,並且如果重疊不覆蓋以前的多邊形

的1/n = 3:繪製多邊形3層的邊緣,以 「粉紅色」

2/N = 6色它:與6個邊緣繪圖的多邊形,將與 「灰色」 的顏色。此時,我看到步驟1中的第一個多邊形與此疊加。在這種情況下,我只想保留第一個多邊形的「粉紅色」顏色,並將第二個多邊形的其餘「未重疊」區域用「灰色」顏色着色。

我試過一些代碼如下,但它總是顯示「灰色」多邊形,而不是「粉紅色」和「灰色」區域。

順便說一句,我也通過「首先繪製6邊多邊形(n = 6),然後繪製3邊多邊形(n = 3)」來解決這個問題。通過改變從最大多邊形到最小多邊形的繪製順序,我可以保持最大和最小多邊形的顏色。不過,我想按照我在這個問題開始時提到的步驟來做,所以當n(邊數)不斷增加時,我可以看到繪圖區域正在增加。

如果您有任何建議,請指教我。非常感謝你!

cat("\014") 
rm(list=ls()) 

############################# 
# first polygon 
#n=3 
xx3=c(0,-3,3);xx3 
yy3=c(1,1,-2);yy3 

#plot each intersection /vertex of polygon n=3 
plot (xx3, yy3,type = "p", xlim=c(-8,8), ylim=c(-8,8),col="blue",xlab = "x", ylab = "y") 
# display value of each point above 
text(xx3, yy3, paste("(",round(xx3, 2),"," ,round(yy3, 2), ")"), 
    cex=0.8,family="mono", font=1, adj=1.5, pos=3) 

#fill the shade area 
polygon(xx3, yy3, col = "pink", border = "pink") 
title("Plot n-edge polygon") 

############################# 
# RUN untill this point and stop. 
#And then run following part, you will see the 1st polygon is overlapping region 
#and is fully overwrited by the second polygon. 

############################# 
# Second polygon 
#n=6 
par(new=TRUE) 

xx=c(0,-15/11,-15/4,-45/11,-3, 3);xx 
yy=c(1,20/11,5/2,20/11,1,-2);yy 

#plot each intersection /vertex of polygon n=6 
points(xx, yy,type = "p", col="blue",xlab = "x", ylab = "y") 
# display value of each point above 
text(xx, yy, paste("(",round(xx, 2),"," ,round(yy, 2), ")"), 
    cex=0.8,family="mono", font=1, adj=1.5, pos=3) 

#fill the shade area 
polygon(xx, yy, col = "grey", border = "grey") 

#draw x=0,y=0 
abline(a=0,b=0,v=0) 
+0

你不能簡單地添加多邊形以相反的順序(即第一個灰粉色則)? – digEmAll 2014-09-19 10:00:25

+0

嗨@digEmAll,我已經嘗試過這個解決方案,你可以看到我在我的問題中提到過它。然而,我想從最小的多邊形到最大的多邊形,這樣我可以看到當我的情況下多邊形的邊緣數量增加時,繪圖區域的增加情況。 – 2014-09-19 10:07:09

+0

Sossy,我沒有注意到(我承認我讀得很急)...... – digEmAll 2014-09-19 12:15:44

回答

2

一種可能性是計算當前多邊形(較大)和前一個(較小)之間的差異。我不知道是否有一些簡單的方法來計算幾何圖形,而不是使用sp(空間對象)和rgeos

這裏有些代碼使用包sprgeos包。該方法包括通過空間對象計算多邊形差異並繪製它。這可能不是最優雅的方式,但至少它會做你想做的。

require(sp) 
require(rgeos) 

#test data 
xx3=c(0,-3,3);xx3 
yy3=c(1,1,-2);yy3 
xx=c(-5,-5,5,5);xx 
yy=c(-5,5,5,-5);yy 

#create a SpatialPolygons object for n = 3 
sp3 <- as.data.frame(cbind(xx3,yy3)) 
sp3 <- rbind(sp3, sp3[1,]) 
coordinates(sp3) <- c("xx3","yy3") 
p3 <- Polygon(sp3) 
ps3 <- Polygons(list(p3),1) 
sps3 <- SpatialPolygons(list(ps3)) 

#create a SpatialPolygons object for n = 6 
sp <- as.data.frame(cbind(xx,yy)) 
sp <- rbind(sp, sp[1,]) 
coordinates(sp) <- c("xx","yy") 
p <- Polygon(sp) 
ps <- Polygons(list(p),1) 
sps <- SpatialPolygons(list(ps)) 

#compute the difference (with rgeos) 
#between the current polygon (bigger) and the previous one (smaller) 
spsdiff <- gDifference(sps, sps3) 

爲繪製差異,2種方式:

#Plotting 1: based on sp-plot 
#=========== 
plot(sps, border="transparent") #to set some bigger extent 
plot(sps3, add=T, col = "pink") 
plot(spsdiff, add=T, col = "grey") 

#Plotting 2: use polygon and polypath base functions 
#=========== 
#preparing data for using polypath (polygons with hole) 
polys <- [email protected][[1]]@Polygons 
coords <- do.call("rbind", lapply(polys, function(x){ if([email protected]) [email protected] })) 
holes <- do.call("rbind", lapply(polys,function(x){ if([email protected]) rbind(rep(NA,2),[email protected]) })) 
poly.coords <- rbind(coords,holes) 

#plot it 
plot(xx, yy, col = "transparent") 
polygon(xx3, yy3, col = "pink") 
polypath(poly.coords[,1],poly.coords[,2],col="grey", rule="evenodd") 

如果您有重複這一點,你可以重新使用循環中此代碼反覆繪製多邊形的差異。

注:rgeos需要你在你的機器上安裝GEOS

+0

非常感謝@eblondel!我剛剛嘗試了你的代碼。多邊形和多邊形繪製我正在尋找的東西。我非常感謝你的大力支持。 – 2014-09-20 03:39:14