【问题标题】:Is there any way to draw a boundary around a group of points in R? [duplicate]有没有办法在 R 中的一组点周围绘制边界? [复制]
【发布时间】:2016-12-08 23:17:13
【问题描述】:

所以我使用以下命令在 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 中做到这一点?

我发现以下函数 (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)
}  

【问题讨论】:

标签: r scatter-plot


【解决方案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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-07-27
    • 1970-01-01
    • 2015-06-05
    • 1970-01-01
    • 2014-10-27
    • 1970-01-01
    • 2022-11-10
    • 2019-10-05
    相关资源
    最近更新 更多