是否有一种方法可以通过R中的K均值内的变量来计数/着色?

问题描述 投票:0回答:2

我已经在数据帧上部署了K-Means,我想看看每个集群中每个组有多少个。

train2 <- structure(c(-1.23909311107667, 3.26644747582115, -0.322711974758471, 
-0.551807258838021, -0.322711974758471, -0.628172353531204, -0.780902542917571, 
-0.475442164144838, -0.628172353531204, 1.35732010849156, -0.246346880065287, 
-0.551807258838021, -0.169981785372104, -0.169981785372104, -0.857267637610754, 
0.059113498707446, -0.780902542917571, -0.704537448224387, -0.933632732303937, 
-0.0936166906789208, 1.96824086603703, 1.89187577134385, 0.059113498707446, 
3.26644747582115, -1.00999782699712, 0.82276444563928, -0.246346880065287, 
2.1209710554234, -0.169981785372104, -0.322711974758471, -1.33823469800757, 
-0.972497608976615, 0.795231654673008, -0.48484815693534, -0.301979612419861, 
-0.606760519945658, -0.850585245966296, -1.21632233499725, -0.667716701450818, 
-0.42389197543018, -0.972497608976615, 0.429494565642051, -1.21632233499725, 
0.00280129510593579, -0.911541427471455, -0.728672882955977, 
-0.789629064461137, -0.362935793925021, -0.0581548863992237, 
-0.972497608976615, 0.978100199188486, -1.21632233499725, -0.362935793925021, 
-0.972497608976615, 0.61236311015753, 1.64861819574524, -0.667716701450818, 
-0.850585245966296, -1.15536615349209, -0.48484815693534, 0.08750796055683, 
1.33068763722735, -1.63913047926333, -0.326885265000009, 1.19255656204173, 
-0.257819727407202, -0.050623114628783, 0.847228874077701, 0.08750796055683, 
0.225639035742443, 0.432835648520862, -0.534081877778428, 0.640032261299281, 
-1.50099940407772, 0.363770110928056, 1.19255656204173, 0.156573498149636, 
1.05442548685612, 0.432835648520862, 0.501901186113669, 0.156573498149636, 
1.8832119379698, 1.53788425000577, 1.33068763722735, 0.08750796055683, 
-0.119688652221589, -0.534081877778428, 0.501901186113669, -0.257819727407202, 
0.847228874077701, -0.540416283538675, -0.30341182152257, -0.69123730482165, 
1.61416973478955, -0.863604186287908, 1.3125276922236, -0.949787627021037, 
-0.820512465921344, -0.917468836746114, -0.10949907987303, 0.235234683059486, 
1.85117419680565, -0.971333487204319, -0.411141122438981, -0.605053864088521, 
0.860064628374672, 0.256780543242769, 1.24789011167375, 0.924702208924518, 
1.95890349772207, -0.669691444638368, -0.842058326104626, 1.61416973478955, 
-0.30341182152257, -0.906695906654473, -0.712783165004933, 1.89426591717222, 
-0.863604186287908, 2.00199521808863, 0.601514306175284, -0.347760555301561, 
1.38078511326583, 0.228421334220902, -0.347760555301561, 2.4755307033585, 
-0.174905988444822, -0.722278783491161, -0.405378744253807, -0.347760555301561, 
-0.80870606691953, -0.491806027682176, -0.232524177397068, -0.549424216634422, 
-0.347760555301561, -0.43418783872993, -0.635851500062792, -0.751087877967284, 
-0.462996933206053, -0.462996933206053, -0.146096893968699, 1.0926941685046, 
-0.203715082920945, -0.203715082920945, 1.38078511326583, -0.203715082920945, 
0.804603223743364, -0.0884787050164525, -0.174905988444822, -0.146096893968699, 
-0.290142366349314, -0.536526567324641, -1.50769420608874, -0.707909091812423, 
2.20559382447987, 1.23442618571577, 0.206131038789081, -0.479399059162047, 
0.948788644902803, -0.850727862218908, -0.193761518349077, -0.593654075487235, 
-0.365144042836859, -0.479399059162047, -0.765036599975017, -0.365144042836859, 
-0.0795065020238888, 0.320386055114269, 1.74857375917912, -0.0795065020238888, 
1.0059161530654, -1.16492915711317, -0.422271550999453, 1.80570126734171, 
-1.50769420608874, -1.05067414078799, -1.10780164895058, -0.650781583649829, 
-0.650781583649829, -0.0795065020238888, 1.23442618571577, -0.748288584824742, 
-1.6289468558649, -1.36474937455285, 1.75278090492932, 0.572698821735499, 
0.836896303047548, -0.748288584824742, 0.431793498369074, 1.79681381848132, 
-0.360798945567071, -0.0966014642550227, -0.0437619679926131, 
-0.360798945567071, -0.924420239032774, -0.290346283883858, 0.326114505844254, 
-0.431251607250284, 1.18915961146361, 0.149982851636222, 0.713604145101925, 
-1.24145721660723, 0.46701982921068, 0.828089720337146, -1.6289468558649, 
0.0266906936905998, -1.15339138950322, -0.378412110987874, 0.643151483418712, 
0.590311987156303, 1.18915961146361, -1.9518886906165, 1.66501741070482, 
0.281482839992629, 0.679206537224389, -0.340597814651915, 0.747193494016135, 
-1.47937934091381, -0.578552163423064, -0.918486947381832, -0.510565206631318, 
-0.136636944276677, 1.18910871316252, -1.12244781775711, -0.340597814651915, 
-1.07825629584244, -0.272610857860169, -1.12244781775711, 0.414057405736549, 
0.237291318077964, 1.62082588879016, 1.33868001810438, 0.373265231661494, 
1.25709566995427, 1.66501741070482, -1.14284390479462, 0.849173929203754, 
1.18910871316252, -0.238617379464296, 1.09392697365409, 0.386862623019821, 
-0.313028610723458, 0.376969468470301, 0.502423664687349, -0.313028610723458, 
-0.877572493700171, 0.565150762795874, 0.126061076036209, 0.627877860904396, 
0.690604959012921, -1.12848088613427, -0.752118297483123, 1.38060303820668, 
0.188788174144731, -1.31666218045984, -0.250301512614933, -1.44211637667688, 
0.627877860904396, -0.626664101266076, 1.56878433253225, 0.816059155229969, 
0.126061076036209, -1.50484347478541, -1.00302668991722, 0.376969468470301, 
1.50605723442373, 1.12969464577259, -0.313028610723458, -1.37938927856836, 
-1.63029767100246, -0.124847316397886, -1.42668607958027, 2.94414045489919, 
0.792348930232381, -0.687007742976053, -0.283546832101025, -0.552520772684377, 
-0.0818163766635117, -0.14905986180935, -0.619764257830215, -0.888738198413566, 
-0.754251228121891, -0.0145728915176737, -0.687007742976053, 
-0.955981683559404, -0.216303346955188, -1.42668607958027, -0.552520772684377, 
-0.14905986180935, -0.350790317246863, 1.66651423712827, 0.456131504503191, 
0.590618474794867, -0.552520772684377, 2.94414045489919, -0.619764257830215, 
0.523374989649029, -0.216303346955188, -0.754251228121891, -0.955981683559404, 
-0.14905986180935, 2.01455021886537, -0.330547545632037, -0.749315003578002, 
-0.41430103722123, -1.25183595311316, -0.833068495167194, 0.674494353438279, 
-0.246794054042844, 0.4232338786707, 0.674494353438279, 0.00446642072473504, 
-0.581808020399615, 0.339480387081507, -1.08432896993477, 0.339480387081507, 
0.4232338786707, 1.67953625250859, -0.833068495167194, 0.4232338786707, 
-1.16808246152397, -0.665561511988809, -0.833068495167194, -1.16808246152397, 
-0.330547545632037, 0.842001336616664, -0.246794054042844, -0.749315003578002, 
-0.0792870708644576, -1.25183595311316, -0.916821986756387, 2.50173351786805, 
0.2109895737961, 0.2109895737961, 0.2109895737961, 0.2109895737961, 
-0.934382398239873, 0.2109895737961, 0.2109895737961, 0.2109895737961, 
0.2109895737961, -0.934382398239873, 0.2109895737961, 0.2109895737961, 
-0.934382398239873, 0.2109895737961, 1.35636154583207, 1.35636154583207, 
0.2109895737961, 0.2109895737961, -0.934382398239873, 1.35636154583207, 
-0.934382398239873, 0.2109895737961, 0.2109895737961, -0.934382398239873, 
0.2109895737961, -0.934382398239873, 0.2109895737961, 1.35636154583207, 
-0.934382398239873), .Dim = c(30L, 12L), .Dimnames = list(NULL, 
    c("fixed acidity", "volatile acidity", "citric acid", "residual sugar", 
    "chlorides", "free sulfur dioxide", "total sulfur dioxide", 
    "density", "pH", "sulphates", "alcohol", "quality")))

因此,使用此数据,我将部署K-Means

##K-Means
library(factoextra)
library(tidyverse)


set.seed(123)
km.res <- kmeans(train2, 2, nstart = 25)

fviz_cluster(km.res,train2) 

因此,我有两个不同的群集,给定下面的标签,我想确定群集1中带有标签“ red”和“ white”以及群集2的值的百分比。

理想情况下,如果我可以看到每个点的可视颜色为红色/白色,那将是理想的。

这里是带有所有适当标签的df。

type < -c("white", "red", "red", "white", "white", "white", "white", 
"white", "white", "white", "white", "white", "white", "white", 
"white", "white", "white", "white", "white", "white", "red", 
"white", "white", "red", "white", "red", "white", "white", "white", 
"white")

这将通过fviz_cluster(km.res,train2)生成以下聚类图。每个点都是数据集中每个观测值的投影,无论如何都是总数据的投影,但是原理仍然成立。因此,既然将其分为两个类,我想显示其中哪些投影的类型为“ red”,哪些投影的类型为“ white”。

enter image description here

r ggplot2 cluster-analysis tidyverse k-means
2个回答
1
投票

您在这里。

数据准备

d <- tibble(cluster = as.factor(km.res$cluster),
            color = as.factor(if_else(cluster == 1, "white", "red")))

解决方案>>

方法1

d %>% ggplot(aes(x = cluster, fill = color)) +
  geom_bar() +
  scale_fill_identity(guide = "legend") +
  labs(x = "Cluster", y = "Count")

Method2

d %>% ggplot(aes(x = cluster, fill = color)) +
  geom_bar() +
  scale_fill_manual(values = levels(d$color)) +
  labs(x = "Cluster", y = "Count")

0
投票

如果您的type与数据匹配,则可以在ggplot对象中添加另一层:

© www.soinside.com 2019 - 2024. All rights reserved.