1
votes

I performing k mean clustering analysis on the Iris dataset in R. I am trying to produce a pairwise plot of all the different combinations of attributes (Sepal.Length, Sepal.Width, Petal.Length and Petal.Width) clustered using kmeans with a center of 3. I was able to produce the plot for the first combination (Sepal Length v Sepal Width) which is as follows:

attach(iris)
iris.scaled <- scale(iris[, -5])
k <- kmeans(iris.scaled,centers=3)
plot(iris.scaled[,1],iris.scaled[,2],col=KM$cluster,)

However, I am unsure how to do this for all 6 possible combinations of attributes and have a 4 by 4 pairwise plot. I thought maybe the pairs function but have no such luck

1
k <- kmeans(iris.scaled,centers=3) Error in as.matrix(x) : object 'iris.scaled' not foundWill
Updated now, apologiesdaisybeats
no worries i figured it out, just it s easier start with the same data as you, instead of guessing what kind of processing you did. answered below, hopefully it's ok.Will

1 Answers

1
votes

I propose:

library(GGally)
library(data.table)
attach(iris)
iris$Species <- NULL
iris.scaled <- data.table(scale(iris))
k <- kmeans(iris.scaled, centers=3)
iris.scaled[, cluster := as.factor(k$cluster)]
colnames(iris.scaled)
# ggplot(iris.scaled, aes(x = Sepal.Length, y = Sepal.Width)) +
#   geom_point(aes(color = factor(cluster)))
ggpairs(iris.scaled, aes(colour = cluster, alpha = 0.4), columns = c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width"))

EDIT:
you can also remove upper graphs:

ggpairs(iris.scaled, aes(colour = cluster, alpha = 0.4), 
columns = c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width"),
upper = "blank",
diag = NULL)

pair graphs from iris