Hersh
June 29, 2022, 5:48pm
1
Hi,
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.
Why reprex?
Getting unstuck is hard. Your first step here is usually to create a reprex, or reproducible example. The goal of a reprex is to package your code, and information about your problem so that others can run it and feel your pain. Then, hopefully, folks can more easily provide a solution.
What's in a Reproducible Example?
Parts of a reproducible example:
background information - Describe what you are trying to do. What have you already done?
complete set up - include any library() calls and data to reproduce your issue.
data for a reprex: Here's a discussion on setting up data for a reprex
make it run - include the minimal code required to reproduce your error on the data…
Check this documentation for know more about PCA
1 Like
Hersh
June 30, 2022, 7:01pm
3
Hello
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 :
library(factoextra)
library(ggplot2)
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,
#variables
col.var="contrib",gradient.cols = c("#00AFBB", "#E7B800", "#FC4E07")) +
labs(fill = "Cluster", color = "Contrib", alpha = "contrib") +
theme_minimal()
#> 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
Hersh
June 30, 2022, 7:22pm
4
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
system
Closed
July 7, 2022, 7:23pm
5
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.