0

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)
}  
d.b
  • 32,245
  • 6
  • 36
  • 77

1 Answers1

2

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)

enter image description here

G5W
  • 36,531
  • 10
  • 47
  • 80