4

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)
StupidWolf
  • 45,075
  • 17
  • 40
  • 72
alberto
  • 2,625
  • 4
  • 29
  • 48
  • I'm unable to install the `factoextra` package to check but can you just add `+ guides(shape=FALSE)` after `ggtitle("")`? – mts Jun 17 '15 at 12:48
  • Almost! I need to get rid of the two legends, not just one. – alberto Jun 17 '15 at 13:44
  • I updated my comment for that as an answer, can you please confirm that it works? – mts Jun 17 '15 at 14:05

2 Answers2

6

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

RHertel
  • 23,412
  • 5
  • 38
  • 64
2

This should also work: add + guides(shape=FALSE, color=FALSE) after ggtitle("")

library(factoextra)
fviz_pca_biplot(pca, label="var", habillage=as.factor(kc$cluster)) +
  labs(color=NULL) + 
  ggtitle("") + guides(shape=FALSE, color=FALSE)
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"))
alberto
  • 2,625
  • 4
  • 29
  • 48
mts
  • 2,160
  • 2
  • 24
  • 34