我有一些資料,我使用歐幾里得距離執行了手動 K 均值聚類。我有最終的質心和聚集點。我已將其繪制成圖表,但是我不確定如何根據它們所屬的集群對不同的點進行顏色編碼。
這就是資料的聚類方式,我想根據它們所在的顏色為這些點著色。
Cluster1 centroid location: 2, 3.5
points under Cluster1: (1,1), (1,6), (2,1), (4,6)
Centroid2 centroid location: 6.2, 8.8
points under Cluster2: (3,9), (3,10), (5,6), (8,9), (9,9), (9,10)
Centroid3 centroid location: 8.8, 2.4
points under Cluster3: (7,2), (8,1), (9,1), (10,3), (10,5)
資料
structure(list(Subject = 1:15, X = c(1L, 1L, 2L, 3L, 3L, 4L,
5L, 7L, 8L, 8L, 9L, 9L, 9L, 10L, 10L), Y = c(1L, 6L, 1L, 9L,
10L, 6L, 6L, 2L, 1L, 9L, 1L, 9L, 10L, 3L, 5L), E1 = c(1L, 1L,
NA, NA, NA, 1L, 1L, 1L, 1L, 1L, NA, 1L, 1L, 1L, NA), E2 = c(NA,
NA, 2L, 2L, 2L, NA, NA, NA, NA, NA, 2L, NA, NA, NA, 2L)), class = "data.frame", row.names = c(NA,
-15L))
圖的當前實作
library(tidyverse)
library(ggplot2)
data <- read.csv("data.csv")
data2 <- data[, -c(1)]
data2 <- data %>% filter(!if_all(c(E1, E2), is.na)) %>% mutate(E = ifelse(is.na(E1), E2, E1))
# Format centroid positions to be a 2D point with coordinates
x <- c(2, 6.2, 8.8)
y <- c(3.5, 8.8, 2.4)
coords = paste(x,y, sep=",")
df = data.frame(x,y)
ggplot(data2, aes(X, Y, shape = factor(E)))
geom_point(size = 4)
scale_shape_manual(values = c(8, 3), name = "E")
theme_bw()
geom_point(df, mapping = aes(x, y), col = "blue", size = 3, inherit.aes = FALSE)
geom_label(df, mapping = aes(x .5, y 0.5, label = coords), inherit.aes = FALSE)
當前生成的圖表

uj5u.com熱心網友回復:
您需要為每個點確定它們所屬的集群,并將此資訊添加到繪圖資料框中。以下是一種方法:
df %>%
# add group
mutate(group = factor(row_number())) %>%
# create all combinations with data2 (rows for each point in data2 with each centroid)
crossing(data2) %>%
# compute euclidean distance
mutate(dist = (X-x)^2 (Y-y)^2) %>%
# for each subject, filter for the closest centroid
group_by(Subject) %>%
slice_min(dist) %>%
ungroup() %>%
# plot
ggplot(aes(colour = group, shape = group))
geom_point(aes(X, Y), size = 3)
geom_point(aes(x, y), size = 5)

uj5u.com熱心網友回復:
您需要添加集群資訊:
data2$Cluster <- factor(c(1, 1, 1, 2, 2, 1, 2, 3, 3, 2, 3, 2, 2, 3, 3))
df$Cluster <- factor(1:3)
現在你的情節可以完成:
ggplot(data2, aes(X, Y, shape = factor(is.na(E1))))
geom_point(size = 4, aes(color = Cluster))
scale_shape_manual(values = c(8, 3), name = "E", labels = 1:2)
theme_bw()
geom_point(data = df, aes(x, y, color = Cluster), size = 4,
inherit.aes = FALSE)
geom_label(data = df, aes(x .5, y 0.5, label = coords),
inherit.aes = FALSE)

轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/457867.html
