我编写了以下函数来执行此操作:
lda.arrows <- function(x, myscale = 1, tex = 0.75, choices = c(1,2), ...){
## adds `biplot` arrows to an lda using the discriminant function values
heads <- coef(x)
arrows(x0 = 0, y0 = 0,
x1 = myscale * heads[,choices[1]],
y1 = myscale * heads[,choices[2]], ...)
text(myscale * heads[,choices], labels = row.names(heads),
cex = tex)
}
对于你的例子:
dis2 <- lda(as.matrix(iris[, 1:4]), iris$Species)
plot(dis2, asp = 1)
lda.arrows(dis2, col = 2, myscale = 2)
箭头的长度相对于 lda 图是任意的(当然,箭头之间的长度不是任意的!)。如果您想要更长或更短的箭头,请更改myscale
因此。默认情况下,这会绘制第一轴和第二轴的箭头。如果要绘制其他轴,请更改choices
来反映这一点。