使用ggplot2的线性判别分析图 [英] Linear discriminant analysis plot using ggplot2

查看:144
本文介绍了使用ggplot2的线性判别分析图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用ggplot2将样本ID(行号)作为标签添加到此LDA图中的每个点?



谢谢

脚本:

  require(MASS)
require(ggplot2)
data (虹膜)

irisLda <-lda(iris [, - 5],iris [,5])


irisLda < - lda数据=虹膜)
plot(irisLda)
irisProjection < - cbind(scale(as.matrix(iris [, - 5]),scale = FALSE)%*%irisLda $ scaling,iris (数据= irisProjection,aes(x = LD1,y = LD2,col =物种))
p + geom_point()
<= ggplot / code>


解决方案

您只需使用 geom_text
$ p $ 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)

也许你需要用 hjust vjust ,它们是 geom_text 的一部分。您还可以查看直接标签智能标签放置的包装。


How can I add the sample ID (row number) as labels to each point in this LDA plot using ggplot2?

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:

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. You also might want to have a look at the directlabels package for smart label placement.

这篇关于使用ggplot2的线性判别分析图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆