Search code examples
rplotcluster-analysisk-means

Function clusplot assign color to dots depending on a added column


I used k-means for clustering after that I plotted my clusters with the clusplot function. I know you can color code the dots depending on the cluster. But I want to color code them depending on a separate column, which just includes “yes” or “no”. I added this column to my data frame after the clustering.

Does anyone have an idea?

Thank you so much in advance.

Toni


Solution

  • Here is an example using the iris data set that comes with R:

    library(cluster)
    data(iris)
    iris.clara <- clara(iris[, -5], 3)
    set.seed(42)   # Set random seed for replicability
    code <- sample(c("Yes", "No"), 150, replace=TRUE)
    color <- ifelse(code=="Yes", "red", "blue")
    clusplot(iris.clara, col.p=color)