我正在尝试使用ggplot2创建一个包含6个数据变量的成对图,并根据它们所属的k-means聚类对点进行着色。我阅读了高度出色的“GGally”软件包的文档以及Adam Laiacano的非正式修复[http://adamlaiacano.tumblr.com/post/13501402316/colored-plotmatrix-in-ggplot2]。不幸的是,我在两者中都没有找到任何方法来获得所需的输出。
以下是示例代码:-
以下是示例代码:-
#The Swiss fertility dataset has been used here
data_ <- read.csv("/home/tejaskale/Ubuntu\ One/IUCAA/Datasets/swiss.csv", header=TRUE)
data_ <- na.omit(data_)
u <- c(2, 3, 4, 5, 6, 7)
x <- data_[,u]
k <- 3
maxIterations <- 100
noOfStarts <- 100
filename <- 'swiss.csv'
library(ggplot2)
library(gridExtra)
library(GGally)
kmeansOutput <- kmeans(x, k, maxIterations, noOfStarts)
xNew <- cbind(x[,1:6], as.factor(kmeansOutput$cluster))
names(xNew)[7] <- 'cluster'
kmeansPlot <- ggpairs(xNew[,1:6], color=xNew$cluster)
OR
kmeansPlot <- plotmatrix(xNew[,1:6], mapping=aes(colour=xNew$cluster))
两个图形都已经创建,但没有按簇着色。
希望我没有在论坛上错过这个问题的答案,如果确实如此,我深表歉意。任何帮助都将不胜感激。
谢谢!
col
参数中传递clusterIDs来完成这个操作。 - Thomas Jungblut其中
cl$cluster`是分配给簇的值。 - Thomas Jungblut