根据簇

时间:2018-10-23 07:44:52

标签: r ggplot2

此代码将虹膜数据的4个维度聚类:

library(SOMbrero)
library(dplyr)
library(ggplot2)
library(datasets)

set.seed(42)
rm(list = ls(all = TRUE))
setwd('C:/Data')

#str(iris)

x_dim = 6
y_dim = 4
Train <- iris

som <- trainSOM(x.data = Train[, 1:4], dimension = c(y_dim, x_dim), maxit = 10000, scaling = "unitvar", radius.type = "letremy")

superClass <- superClass(som, k = 3)
summary(superClass)

Train$Cluster = superClass$cluster[superClass$som$clustering]
Train$Node = superClass$som$clustering
Train$X = som$parameters$the.grid$coord[superClass$som$clustering, 1]
Train$Y = som$parameters$the.grid$coord[superClass$som$clustering, 2]

bp <- ggplot(Train, aes(x = Species, y = Sepal.Length)) +
    geom_boxplot() +
    facet_grid(Y ~ X)
bp

最后一位描述网格及其侧面:Sepal.Length。我想根据“群集”列定义的群集成员资格来更改每个网格单元的背景颜色,例如:

Cluster = 1 => red  
Cluster = 2 => blue
Cluster = 3 => black

任何帮助将不胜感激。非常感谢!

PS:

bp <- ggplot(Train, aes(x = Species, y = Sepal.Length, fill = factor(Cluster))) +
    geom_rect(xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = Inf, alpha = 0.01) +
    geom_boxplot() +
    facet_grid(Y ~ X)
bp  

0 个答案:

没有答案