I am trying to visualize PCA results, and I want to have a quadrant plot (fviz_pca_var) showing the groups visually and then a bar plot showing the actual values of the contribution (fviz_contrib)
Ideally, I would like the fviz_contrib bar graph to match the colors I used to identify clusters in the fviz_pca_var plot. When I try to do this via "fill" or "color" parameters, I get the error:
Caused by error in `if (fill %in% names(data)) ...
! the condition has length > 1`:
This is the code I used to get the cluster groups:
var <- get_pca_var(data_no_na.std.pc)
set.seed(123)
res.km <- kmeans(var$coord, centers = 3, nstart = 25)
grp <- as.factor(res.km$cluster)
Where the grp object is my groupings -- it looks like a factor list with three levels where all of my variables are assigned either 1,2, or 3.
The code to create the plot itself is;
fviz_contrib(data_no_na.std.pc, choice="var", axes = 1, top=50, fill=grp,
color="black") +
scale_fill_manual(value=c("#481567FF", "#E7B800", "#20A387FF"),
breaks=grp)
The result would be the regular fviz_contrib bar plot but with the bars showing different colors based on the group I've assigned that variable to (1,2,3). I tried the solution here, but when I try to fill based on the grp object it doesn't work, and I've tried to append grp as it's own element in the data_no_na.std.pc object and use that, but it also does not work (I get an "unknown color name" error):
data_no_na.std.pc$grp <- grp
fviz_contrib(data_no_na.std.pc, choice="var", axes = 1, top=50, fill="grp",
color="black")
Any ideas? This seems like it should be straightforward...
dput
?