Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linear discriminant analysis plot using ggplot2

Tags:

r

ggplot2

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()   
like image 768
lroca Avatar asked Sep 13 '25 04:09

lroca


1 Answers

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.

enter image description here

like image 78
Paul Hiemstra Avatar answered Sep 15 '25 19:09

Paul Hiemstra