r - adding labels to 2D scatter plot (kmeans clustering) -
i calculated pca on samples of dataset , retained first 2 components vectors .i calculated k-means clustering on these first 2 components k=3. need plot 2d scatter plot first 2 eigenfunctions (from pca) , color based on cluster group. accomplished scatter plot when @ plot cannot differentiate samples clustered want add sample labels points in scatter plot. can suggest me how go this?
tdata<-t(subdata) pca <- prcomp((tdata),cor=f) dat.loadings <-pca$x[,1:2] cl <- kmeans(dat.loadings, centers=3) pca1 <-pca$x[,1] pca2 <-pca$x[,2] plot(pca1, pca2,xlab="pca-1",ylab="pca-2",col=cl$cluster)
thank you
this can done using ggplot. use mtcars data since don't have access dataset using. idea should pretty clear anyway.
library(ggplot2) pca <- prcomp((mtcars),cor=f) dat.loadings <-pca$x[,1:2] cl <- kmeans(dat.loadings, centers=3) pca1 <-pca$x[,1] pca2 <-pca$x[,2] #plot(pca1, pca2,xlab="pca-1",ylab="pca-2",col=cl$cluster) mydf<-data.frame(id=names(pca1),pca1=pca1, pca2=pca2, cluster=factor(cl$cluster)) ggplot(mydf, aes(x=pca1, y=pca2, label=id, color=cluster)) + geom_point() + geom_text(size = 4, colour = "black", vjust = -1)
this gives names output per data point.
Comments
Post a Comment