【发布时间】:2017-02-28 12:15:39
【问题描述】:
我正在尝试为线性判别分析 (LDA) 创建双图。我正在使用从这里获得的代码的修改版本https://stats.stackexchange.com/questions/82497/can-the-scaling-values-in-a-linear-discriminant-analysis-lda-be-used-to-plot-e
但是,我有 80 个变量,这使得双标图非常难以阅读。高度贡献的变量会加剧这种情况,因为它们的箭头长度很长,并且剩余的标签在中间被挤压。
所以我想要实现的是一个双标图,其中所有可变箭头的长度相同,并且它们的相对贡献(比例)通过分级颜色来区分。
到目前为止,我已经设法获得分级颜色,但我找不到使箭头长度相同的方法。据我了解,geom_text 和 geom_segment 使用 LD1 和 LD2 值来确定箭头的 length 和 direction。如何覆盖长度?
代码:
library(ggplot2)
library(grid)
library(MASS)
data(iris)
iris.lda <- lda(as.factor(Species)~.,
data=iris)
#Project data on linear discriminants
iris.lda.values <- predict(iris.lda, iris[,-5])
#Extract scaling for each predictor and
data.lda <- data.frame(varnames=rownames(coef(iris.lda)), coef(iris.lda))
#coef(iris.lda) is equivalent to iris.lda$scaling
data.lda$length <- with(data.lda, sqrt(LD1^2+LD2^2))
#Plot the results
p <- qplot(data=data.frame(iris.lda.values$x),
main="LDA",
x=LD1,
y=LD2,
colour=iris$Species)+stat_ellipse(geom="polygon", alpha=.3, aes(fill=iris$Species))
p <- p + geom_hline(aes(yintercept=0), size=.2) + geom_vline(aes(xintercept=0), size=.2)
p <- p + theme(legend.position="right")
p <- p + geom_text(data=data.lda,
aes(x=LD1, y=LD2,
label=varnames,
shape=NULL, linetype=NULL,
alpha=length, position="identity"),
size = 4, vjust=.5,
hjust=0, color="red")
p <- p + geom_segment(data=data.lda,
aes(x=0, y=0,
xend=LD1, yend=LD2,
shape=NULL, linetype=NULL,
alpha=length),
arrow=arrow(length=unit(0.1,"mm")),
color="red")
p <- p + coord_flip()
print(p)
【问题讨论】: