问题
So I have plotted three groups of data in R using the following command
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)
Now I want to enclose each of these three groups by drawing a line (or a curve) around them. Is there a way to do that in R?
I found the following function (https://chitchatr.wordpress.com/2011/12/30/convex-hull-around-scatter-plot-in-r/) that draws a line around the points. Is there a way to offset it even more and make it more smooth?
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:
OK, here is a different solution. It uses the convex hull but just stretches it out a little further (away from the centroid).
Example:
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)
来源:https://stackoverflow.com/questions/41050532/is-there-any-way-to-draw-a-boundary-around-a-group-of-points-in-r