Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R Biplot with clusters as colors

I'm doing a clustering after a PCA transformation and I would like to visualize the results of the clustering in the first two or three dimensions of the PCA space as well as the contribution from the original axes to the projected PCA ones.

I use the factoextra library which uses ggplot, and it works fine, but I would like to take the legend off:

My code:

# Load iris dataset
data(iris)

# PCA
pca <- prcomp(iris[,-5], scale=TRUE)
df.pca <- pca$x

# Cluster over the three first PCA dimensions
kc <- kmeans(df.pca[,1:3], 5)

# 2-D biplot (how to get rid of legend?)
# install.packages("devtools")
# library("devtools")
# install_github("kassambara/factoextra")
library(factoextra)
fviz_pca_biplot(pca, label="var", habillage=as.factor(kc$cluster)) +
  labs(color=NULL) + ggtitle("") +
  theme(text = element_text(size = 15),
      panel.background = element_blank(), 
      panel.grid.major = element_blank(),
      panel.grid.minor = element_blank(),
      axis.line = element_line(colour = "black"),
      legend.key = element_rect(fill = "white"))

enter image description here

How can delete the legend columns at the right?

The equivalent biplot without using any library would be a welcomed solution as well.

PS:

Even in 3-D is quite straighforward to get an good biplot:

library(rgl)
text3d(pca$x[,1:3],texts=rep("*",dim(pca$x)[1]), col=kc$cluster) # points
text3d(1*pca$rotation[,1:3], texts=rownames(pca$rotation), col="red") # arrows title
coords <- NULL
for (i in 1:nrow(pca$rotation)) {
  coords <- rbind(coords, rbind(c(0,0,0),1*pca$rotation[i,1:3]))
}
lines3d(coords, col="blue", lwd=1)
like image 430
alberto Avatar asked Feb 09 '23 19:02

alberto


1 Answers

I think you should try theme(legend.position="none").

 library(factoextra)
 plot(fviz_pca_biplot(pca, label="var", 
  habillage=as.factor(kc$cluster)) + ggtitle("") +
  theme(text = element_text(size = 15), 
      panel.background = element_blank(), 
      panel.grid.major = element_blank(), 
      panel.grid.minor = element_blank(), 
      axis.line = element_line(colour = "black"),
      legend.position="none"))

This is what I get: enter image description here

like image 105
RHertel Avatar answered Feb 12 '23 10:02

RHertel