r - Plot polygons with different colors and not overwrite the previous polygons if overlapped -


i graphing several n-edge polygons on same plot. let say:

1/ n=3: draw polygon 3 edges, color "pink"

2/ n=6: draw polygon 6 edges, color "grey". @ point, see first polygon in step 1 overlapped one. in case, want keep "pink" color of first polygon , color rest "un-overlapped" area of 2nd polygon "grey" color.

i have tried code follow, display "grey" polygon, instead of "pink" , "grey" areas.

btw, walked around problem "draw 6-edge polygon (n=6) first, , draw 3-edges polygon (n=3)". changing drawing order biggest polygon smallest one, can keep color of biggest , smallest polygons @ end. however, steps mentioned @ beginning of questions can see plotting areas increasing when n (number of edges) keeps increasing.

if have suggestions, please advice me. thank much!

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 shade area polygon(xx3, yy3, col = "pink", border = "pink") title("plot n-edge polygon")  ############################# # run untill point , stop.  #and run following part, see 1st polygon overlapping region  #and overwrited 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 shade area polygon(xx, yy, col = "grey", border = "grey")  #draw x=0,y=0 abline(a=0,b=0,v=0) 

one possibility compute difference between current polygon (bigger), , previous 1 (smaller). don't know if there easy way compute geometries other using sp (spatial objects) , rgeos.

here code uses packages sp , rgeos packages. approach consists compute polygonal difference, means of spatial objects, , plot it. might not elegant way, @ least want.

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 spatialpolygons object 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 spatialpolygons object 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 difference (with rgeos) #between current polygon (bigger) , previous 1 (smaller) spsdiff <- gdifference(sps, sps3) 

for plotting difference, 2 ways:

#plotting 1: based on sp-plot #=========== plot(sps, border="transparent") #to set bigger extent plot(sps3, add=t, col = "pink") plot(spsdiff, add=t, col = "grey")   #plotting 2: use polygon , polypath base functions #=========== #preparing data using polypath (polygons 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 plot(xx, yy, col = "transparent") polygon(xx3, yy3, col = "pink") polypath(poly.coords[,1],poly.coords[,2],col="grey", rule="evenodd") 

if have repeat this, can re-use code within loop iteratively plot polygon differences.

note: rgeos requires install geos library on machine


Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -