【问题标题】:Plot polygons with different colors and not overwrite the previous polygons if overlapped绘制不同颜色的多边形,如果重叠,则不覆盖先前的多边形
【发布时间】:2014-09-19 09:48:57
【问题描述】:

我在同一个图上绘制几个 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)

【问题讨论】:

  • 你不能简单地以相反的顺序添加多边形(即先灰色然后粉色)吗?
  • 嗨@digEmAll,我已经尝试过这个解决方案,你可以看到我在我的问题中提到了它。但是,我想从最小的多边形到最大的多边形,这样我就可以看到当多边形的边缘数量增加时,绘图区域是如何增加的。
  • Sossy,我没注意到(我承认我读的很匆忙)......

标签: r


【解决方案1】:

一种可能性是计算当前多边形(较大)与前一个多边形(较小)之间的差异。除了使用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)

为了绘制差异,有两种方法:

#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 <- spsdiff@polygons[[1]]@Polygons
coords <- do.call("rbind", lapply(polys, function(x){ if(x@hole) x@coords }))
holes <- do.call("rbind", lapply(polys,function(x){ if(!x@hole) rbind(rep(NA,2),x@coords) }))
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

【讨论】:

  • 非常感谢@eblondel!我刚刚尝试过你的代码。多边形和多路径正是我正在寻找的东西。非常感谢您的大力支持。
猜你喜欢
  • 2018-03-26
  • 2021-11-10
  • 1970-01-01
  • 2014-10-03
  • 1970-01-01
  • 1970-01-01
  • 2015-10-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多