Colored ind PCA

I would like to colored my sites/ind in my PCA but it doesn't work
I saw I have to use "habillage = " but I can't colored my ind depending on "eco" and have at the same time my var colored with the cos2

This firts code work well

eco <- as.character(Env$Clust

fviz_pca_biplot(res.pca, repel = TRUE,
                col.var = "cos2",
                gradient.cols = c("#00AFBB", "#E7B800", "#FC4E07") 

This one work well also and with this one I have my ind colored but I don't have my colored var with cos2

fviz_pca_biplot(res.pca, label="var", habillage=iris$Species,
               addEllipses=TRUE, ellipse.level=0.95)

How can I merge these two code ?
I tried to do that but it say "Error: Discrete value supplied to continuous scale" :

fviz_pca_biplot(res.pca, repel = TRUE,
                col.var = "cos2",
                gradient.cols = c("#00AFBB", "#E7B800", "#FC4E07"),
                habillage = eco
1 Like

Hi @Hersh , remember put a reporducible example of data for better help you, like this dput() or reprex
And script for replicate the situation.

Check this documentation for know more about PCA

1 Like

Thanks for your response and sorry for the late response, I was looking for a way to send a reproductible example of a df without using mine !

So here a example :

data <- iris
habillage <- as.character(data$Species)
res.pca <- prcomp(data[,1:4],  scale = TRUE)

fviz_pca_biplot (res.pca, 
                 # Individuals
                 geom.ind = c("point","text"),
                 fill.ind = habillage,
                 pointshape = 21, pointsize=5,
                 palette = "set2",labelsize = 5, 
                 addEllipses=TRUE, ellipse.level=0.90, mean.point =FALSE, label="all",
                 repel = TRUE, 
                 col.var="contrib",gradient.cols = c("#00AFBB", "#E7B800", "#FC4E07")) +
  labs(fill = "Cluster", color = "Contrib", alpha = "contrib") +
#> Warning: ggrepel: 106 unlabeled data points (too many overlaps). Consider
#> increasing max.overlaps

I succeed to have both my Cluster color and "contrib" color but I just want to modify the Cluster color
I want to have "Setosa" in green, "Versicolor" in blue and "Virginica" in red

I tried many way but also the same problem about "discrete value supplied to continus scale" and so one...

1 Like

Ok, so apparently it was as simple as doing this :

palette = c("green","blue","red")

I'm not used to R being simple from time to time!
Sorry for the inconvenience, hope this post helps someone!

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.