Constructing a smoothed area on a map from a set of points in R

how to build a plot around a lot of points on a map in R? eg.

map('world') map.axes() p <- matrix(c(50, 50, 80, 100, 70, 40, 25, 60), ncol=2) # make some points points(p, pch=19, col="red") polygon(p, col="blue") 

... which gives me a polygon with a vertex at each point, but looks pretty shitty. Is there a way to β€œsmooth” a polygon into some kind of curve?

+4
source share
2 answers

One option is to make a polygon bounded by a Bezier curve using the bezier function in the Hmisc package. However, I cannot get the start / end point to integrate neatly. For instance:

 ## make some points p <- matrix(c(50, 50, 80, 100, 70, 40, 25, 60), ncol=2) ## add the starting point to the end p2 <- cbind(1:5,p[c(1:4,1),]) ## linear interpolation between these points t.coarse <- seq(1,5,0.05) x.coarse <- approx(p2[,1],p2[,2],xout=t.coarse)$y y.coarse <- approx(p2[,1],p2[,3],xout=t.coarse)$y ## create a Bezier curve library(Hmisc) bz <- bezier(x.coarse,y.coarse) library(maps) map('world') map.axes() polygon(bz$x,bz$y, col=rgb(0,0,1,0.5),border=NA) 
+3
source

Here is one way, draw a polygon and make it as beautiful as you like. It really has nothing to do with areas on maps, more about how you create the vertices of your polygon.

  library(maps) p <- matrix(c(50, 50, 80, 100, 70, 40, 25, 60), ncol=2) plot(p, pch = 16, col = "red", cex = 3, xlim = range(p[,1]) + c(-10,10), ylim = range(p[,2]) + c(-5, 5)) map(add = TRUE) #click until happy, right-click "stop" to finish p <- locator(type = "l") map() polygon(cbind(p$x, p$y), col = "blue") 

Otherwise, you could interpolate the intermediate vertices and smooth them somehow, and in the context of the lon / lat map, perhaps using reprogramming to get more realistic line segments, but depending on your purpose.

0
source

All Articles