您可以使用 ellipse 包中的 ellipse 函数来计算椭圆上的点,对于每组数据,您可以为椭圆函数提供均值、标准差和相关性,然后将结果传递给 @987654322 @ 函数添加到散点图中。如果组相当正常,这将很有效,但如果组中存在强烈偏斜,则椭圆将无法很好地拟合。
另一种选择是使用chull 函数来计算包含组中所有点的复杂外壳。您可以使用它来绘制一个包含所有点的多边形(有些会接触多边形)。如果你想要比多边形更弯曲的东西,那么使用xspline 函数来绘制而不是lines 或polygon。下面是一些示例代码:
with(iris, plot( Petal.Width, Petal.Length, col=c('red','green','blue')[Species]))
tmp <- chull(iris[ iris$Species=='setosa', c('Petal.Width','Petal.Length')])
xspline( iris[ iris$Species=='setosa', c('Petal.Width','Petal.Length')][tmp,],
border='red',open=FALSE, shape= -0.75)
tmp <- chull(iris[ iris$Species=='versicolor', c('Petal.Width','Petal.Length')])
xspline( iris[ iris$Species=='versicolor', c('Petal.Width','Petal.Length')][tmp,],
border='green',open=FALSE, shape= -0.75)
tmp <- chull(iris[ iris$Species=='virginica', c('Petal.Width','Petal.Length')])
xspline( iris[ iris$Species=='virginica', c('Petal.Width','Petal.Length')][tmp,],
border='blue',open=FALSE, shape= -0.75)
library(ellipse)
with(iris, plot( Petal.Width, Petal.Length, col=c('red','green','blue')[Species]))
polygon( ellipse(
var( iris[ iris$Species=='setosa', c('Petal.Width','Petal.Length') ] ),
centre=colMeans(iris[ iris$Species=='setosa', c('Petal.Width','Petal.Length') ]),
t=3),
border='red')
polygon( ellipse(
var( iris[ iris$Species=='versicolor', c('Petal.Width','Petal.Length') ] ),
centre=colMeans(iris[ iris$Species=='versicolor', c('Petal.Width','Petal.Length') ]),
t=3),
border='green')
polygon( ellipse(
var( iris[ iris$Species=='virginica', c('Petal.Width','Petal.Length') ] ),
centre=colMeans(iris[ iris$Species=='virginica', c('Petal.Width','Petal.Length') ]),
t=3),
border='blue')