Search code examples
rggplot23dcluster-analysisk-means

How can I make a 3D plot in R of the clusters obtained with kmeans?


My contains observations with 3 attributes, I have used to cluster them into four different groups. My goal is to plot the clusters I have obtained in a plot in order to have a quick and easy way to look at the clustered data.

However I do not know how to plot in 3D, I have code that works with 2D but I don't know how to adapt it to add a dimension. The code I have is the following:

    library(ggplot2)
set.seed(137)
km = kmeans(bella,4, nstart=25)

df = as.data.frame(bella)
df$cluster = factor(km$cluster)
centers=as.data.frame(km$centers)
df

 ggplot(data=df, aes(x=Annual.Income..k.., z = Age, y=Spending.Score..1.100.)) +
 geom_point() + theme(legend.position="right") +
 geom_point(data=centers,
 aes(x=Annual.Income..k.., y=Spending.Score..1.100., z=Age,color=as.factor(c(1:4))), aes(x=Age, y=Spending.Score..1.100., color=as.factor(c(1:4))),
 size=10, alpha=.3, show.legend=FALSE)

How can I create a 3D plot? Thanks in advance!


Solution

  • You can also use plotly:

    df = iris[,1:3]
    df$cluster = factor(kmeans(df,3)$cluster)
    
    library(plotly)
    library(dplyr)
    p <- plot_ly(df, x=~Sepal.Length, y=~Sepal.Width, 
    z=~Petal.Length, color=~cluster) %>%
         add_markers(size=1.5)
    print(p)
    

    enter image description here

    Another option with htmlwidget is using threejs (which is based on scatterplot3d as shown in @G5W's answer):

    library(threejs)
    COLS = RColorBrewer::brewer.pal(3,"Set2")
    scatterplot3js(as.matrix(df[,1:3]),col=COLS[df$cluster],size=0.3)
    

    enter image description here