2016-12-08 41 views
-1

因此,我已使用下面的命令有什麼方法可以在R中的一組點上繪製邊界嗎?

plot(1, 1, xlim = c(min(al_comm$PC1),max(al_comm$PC1)), ylim = c(min(al_comm$PC2),max(al_comm$PC2)), type = 'n', xlab = '', ylab = '') 
points(DW_PC1,DW_PC2,pch = 0, col = "red", cex = 1.1) 
points(WW_PC1,WW_PC2,pch = 10, col = "blue", cex = 1.1) 
points(DS_PC1,DS_PC2,pch = 5, col = "magenta", cex = 1.1) 

現在我想通過繪製其周圍的線(或曲線)以封閉各三組的繪製三組數據的R中。在R中有沒有辦法做到這一點?

我發現以下功能(https://chitchatr.wordpress.com/2011/12/30/convex-hull-around-scatter-plot-in-r/),圍繞點繪製一條線。有沒有辦法讓它更平滑,更平滑?

Plot_ConvexHull<-function(xcoord, ycoord, lcolor){ 
    hpts <- chull(x = xcoord, y = ycoord) 
    hpts <- c(hpts, hpts[1]) 
    lines(xcoord[hpts], ycoord[hpts], col = lcolor) 
} 
+1

看看'chull' – rawr

+1

你見過這個? http://stackoverflow.com/questions/13577918/r-plotting-a-curve-around-a-set-of-points/13579969 – thelatemail

+1

我剛剛添加了一個答案的鏈接問題。考慮關閉這個作爲愚蠢的... –

回答

1

好的,這裏是一個不同的解決方案。它使用凸包,但只是稍微伸出一點(遠離質心)。

例子:

x = rnorm(100) 
y = rnorm(100) 
Dat = data.frame(x,y) 
plot(Dat, xlim=c(-3.5, 3), ylim=c(-3,3)) 

Mx = mean(x) 
My = mean(y) 
CH = chull(Dat) 
BumpX = x[CH] + 0.1*(x[CH]-Mx) 
BumpY = y[CH] + 0.1*(y[CH]-My) 
polygon(BumpX, BumpY) 

enter image description here

相關問題