2010-08-04 63 views
4

如何繪製R中地圖上一組點的周圍區域?例如從地圖上的一組點繪製平滑區域R

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") 

......這給了我一個頂點在每個點的多邊形,但它看起來相當糟糕。有什麼辦法可以將多邊形「平滑」成某種曲線嗎?

回答

3

一個選項是使用Hmisc包中的bezier函數製作一個由貝塞爾曲線界定的多邊形。不過,我無法讓起點/終點齊整。例如:

## 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) 
0

以下是一種方法,繪製多邊形並使其儘可能美觀。這與地圖上的區域無關,更多關於如何生成多邊形頂點的信息。

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") 

否則,你可以插值頂點中間,不知怎麼順利地把它們,並在LON的上下文/ LAT利用重投影映射可能得到更真實的線段 - 而是取決於你的目的。