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

Viewed 4613

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!

2 Answers

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

You do not provide your data so I will illustrate with the built-in iris data.

Here are two ways:

library(scatterplot3d)
scatterplot3d(iris[,2:4], pch=20, color=rainbow(3)[km$cluster])

Result of scatterplot3d

OR

library(rgl)
plot3d(iris[,2:4], col=rainbow(3)[km$cluster])

3D plot from rgl

When you run this version, you can click on the image and rotate it around to see different angles.

Related