[英]Linear discriminant analysis plot using ggplot2
How can I add the sample ID (row number) as labels to each point in this LDA plot using ggplot2? 如何使用ggplot2将样本ID(行号)作为标签添加到此LDA图中的每个点?
Thanks 谢谢
Script: 脚本:
require(MASS)
require(ggplot2)
data(iris)
irisLda <- lda(iris[,-5],iris[,5])
irisLda <- lda(Species~.,data=iris)
plot(irisLda)
irisProjection <- cbind(scale(as.matrix(iris[,-5]),scale=FALSE) %*% irisLda$scaling,iris[,5,drop=FALSE])
p <- ggplot(data=irisProjection,aes(x=LD1,y=LD2,col=Species))
p + geom_point()
You simply need to use geom_text
: 你只需要使用
geom_text
:
irisProjection$row_num = 1:nrow(irisProjection)
p <- ggplot(data=irisProjection, aes(x=LD1,y=LD2,col=Species)) +
geom_point() + geom_text(aes(label = row_num))
print(p)
Maybe you need to play around a bit with hjust
and vjust
, which are part of geom_text
. 也许你需要在
hjust
和vjust
玩一下,这是geom_text
一部分。 You also might want to have a look at the directlabels
package for smart label placement. 您还可以查看
directlabels
标签包以进行智能标签放置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.