Biplot with clustering set to 4

50 Views Asked by At

Hello I am trying to answer this question:

cluster the data into 4 clusters using kmeans. Visualize your cluster solution in a biplot by setting the biplot argument xlabs to the vector of cluster membership of your cluster solution.

We are using data from

 library("flexclust")
data(milk)

this is what I have started with:

pca_milk <- prcomp(x = milk_matrix, center = TRUE, scale. = FALSE, retx = TRUE) 
summary(pca_milk)
pca_milk



km4<-kmeans(milk, centers = 4)
head(km4)


biplot(pca_milk, main = "Biplot", cex = c(0.7,1), scale = TRUE)

I am a bit lost as to how to represent my 4 clusters on my biplot and haven't found much info on r help pages or online. Any help would be greatly appreciated. Thank you :)

1

There are 1 best solutions below

0
A_yard_farmer On

I think we are in the same lecture. I solved this by just adding km4$cluster together with the names to the xlabs parameter of the biplot function. Best, Armin

biplot(pca_milk, main = "Biplot", cex = c(0.5,0.7), xlabs = paste0(km4$cluster," [",names(km4$cluster),"]"),  scale = TRUE)