-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2D_point_visualization.qmd
More file actions
119 lines (101 loc) · 4.71 KB
/
2D_point_visualization.qmd
File metadata and controls
119 lines (101 loc) · 4.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
```{r}
library(ggplot2)
# Step 1: Read the 2D point data
points_df <- read.table("2D_poin100.lst", header = FALSE)
colnames(points_df) <- c("Point", "X", "Y")
# Step 2: Define clusters by point names
cluster1 <- paste0("Point", sprintf("%02d", c(4, 27, 37, 86, 53, 33, 28, 83, 1, 45, 2, 3, 46, 40, 34, 24, 66, 23, 9, 67)))
cluster2 <- paste0("Point", sprintf("%02d", c(5, 38, 44, 55, 78, 50, 54, 49, 20, 51, 65, 19, 81, 18, 26, 57, 98)))
cluster3 <- paste0("Point", sprintf("%02d", c(6, 61, 97, 68, 88, 22, 70, 32, 94, 63, 8, 69, 74, 80, 84, 76)))
cluster4 <- paste0("Point", sprintf("%02d", c(42, 52, 85, 10, 59, 72, 71, 41, 16, 56, 95, 82, 91, 96, 7)))
cluster5 <- paste0("Point", sprintf("%02d", c(58, 79, 73, 48, 15, 11, 39, 30, 75, 17, 89)))
cluster6 <- paste0("Point", sprintf("%02d", c(43, 64, 92, 77, 62, 25, 87)))
cluster7 <- paste0("Point", sprintf("%02d", c(12, 29, 31, 93, 60)))
cluster8 <- paste0("Point", sprintf("%02d", c(35, 90, 47, 99)))
cluster9 <- paste0("Point", sprintf("%02d", c(14, 36, 21)))
cluster10 <- "Point13"
# Step 3: Combine all clusters into one mapping
cluster_assignments <- c(
setNames(rep(1, length(cluster1)), cluster1),
setNames(rep(2, length(cluster2)), cluster2),
setNames(rep(3, length(cluster3)), cluster3),
setNames(rep(4, length(cluster4)), cluster4),
setNames(rep(5, length(cluster5)), cluster5),
setNames(rep(6, length(cluster6)), cluster6),
setNames(rep(7, length(cluster7)), cluster7),
setNames(rep(8, length(cluster8)), cluster8),
setNames(rep(9, length(cluster9)), cluster9),
setNames(rep(10, 1), cluster10)
)
# Step 4: Assign cluster labels to the data frame
points_df$Cluster <- as.factor(cluster_assignments[as.character(points_df$Point)])
# Step 5: Plot the clusters
ggplot(points_df, aes(x = X, y = Y, color = Cluster)) +
geom_point(size = 3) +
theme_minimal() +
labs(title = "2D QT Clustering Visualization", x = "X Coordinate", y = "Y Coordinate")
```
```{r}
library(ggplot2)
library(dplyr)
library(ggplot2)
library(dplyr)
library(tidyr)
# Step 1: Read data
points_df <- read.table("2D_poin100.lst", header = FALSE)
colnames(points_df) <- c("Point", "X", "Y")
# Step 2: Assign clusters manually
cluster1 <- paste0("Point", sprintf("%02d", c(4, 27, 37, 86, 53, 33, 28, 83, 1, 45, 2, 3, 46, 40, 34, 24, 66, 23, 9, 67)))
cluster2 <- paste0("Point", sprintf("%02d", c(5, 38, 44, 55, 78, 50, 54, 49, 20, 51, 65, 19, 81, 18, 26, 57, 98)))
cluster3 <- paste0("Point", sprintf("%02d", c(6, 61, 97, 68, 88, 22, 70, 32, 94, 63, 8, 69, 74, 80, 84, 76)))
cluster4 <- paste0("Point", sprintf("%02d", c(42, 52, 85, 10, 59, 72, 71, 41, 16, 56, 95, 82, 91, 96, 7)))
cluster5 <- paste0("Point", sprintf("%02d", c(58, 79, 73, 48, 15, 11, 39, 30, 75, 17, 89)))
cluster6 <- paste0("Point", sprintf("%02d", c(43, 64, 92, 77, 62, 25, 87)))
cluster7 <- paste0("Point", sprintf("%02d", c(12, 29, 31, 93, 60)))
cluster8 <- paste0("Point", sprintf("%02d", c(35, 90, 47, 99)))
cluster9 <- paste0("Point", sprintf("%02d", c(14, 36, 21)))
cluster10 <- "Point13"
cluster_assignments <- c(
setNames(rep(1, length(cluster1)), cluster1),
setNames(rep(2, length(cluster2)), cluster2),
setNames(rep(3, length(cluster3)), cluster3),
setNames(rep(4, length(cluster4)), cluster4),
setNames(rep(5, length(cluster5)), cluster5),
setNames(rep(6, length(cluster6)), cluster6),
setNames(rep(7, length(cluster7)), cluster7),
setNames(rep(8, length(cluster8)), cluster8),
setNames(rep(9, length(cluster9)), cluster9),
setNames(rep(10, 1), cluster10)
)
points_df$Cluster <- as.factor(cluster_assignments[as.character(points_df$Point)])
points_df <- na.omit(points_df)
# Step 3: Compute cluster centroids and max radius
cluster_centers <- points_df %>%
group_by(Cluster) %>%
summarise(
cx = mean(X),
cy = mean(Y)
)
cluster_radii <- points_df %>%
left_join(cluster_centers, by = "Cluster") %>%
rowwise() %>%
mutate(dist = sqrt((X - cx)^2 + (Y - cy)^2)) %>%
group_by(Cluster) %>%
summarise(cx = first(cx), cy = first(cy), r = max(dist))
# Step 4: Build circle data
circle_points <- function(cx, cy, r, n = 100) {
theta <- seq(0, 2 * pi, length.out = n)
data.frame(X = cx + r * cos(theta), Y = cy + r * sin(theta))
}
circle_df <- cluster_radii %>%
rowwise() %>%
mutate(data = list(circle_points(cx, cy, r))) %>%
mutate(Cluster = as.factor(Cluster)) %>%
unnest(cols = c(data))
# Step 5: Plot
ggplot(points_df, aes(x = X, y = Y, color = Cluster)) +
geom_point(size = 3) +
geom_path(data = circle_df, aes(x = X, y = Y, group = Cluster, color = Cluster), alpha = 0.4) +
theme_minimal() +
labs(title = "2D QT Clustering with Circles by Cluster Diameter", x = "X Coordinate", y = "Y Coordinate")
```