FactoMineRのCAパッケージのデフォルトはsymmetric map
衆議院議員選挙2024年福岡11区のデータ
```{r,out.width="100%"}
library(readr)
library(ggplot2)
library(ca)
library(FactoMineR)
library(patchwork)
library(vegan)
# データの準備
data <- read_csv("data11ku_PC.csv")
voting_data <- as.data.frame(data[,-1])
rownames(voting_data) <- data[[1]]
# head(voting_data,3)
# tail(voting_data,3)
voting_numeric <- voting_data[, 2:ncol(voting_data)]
# 1. 対応分析 (CA)
CA(voting_numeric, graph = TRUE)
ca_res <- CA(voting_numeric, graph = FALSE)
ca_df <- data.frame(
Dim1 = ca_res$col$coord[,1],
Dim2 = ca_res$col$coord[,2],
Name = rownames(ca_res$col$coord)
)
# 2. MDS
voting_props <- prop.table(as.matrix(voting_numeric), margin = 1)
mds_dist <- dist(t(voting_props))
mds_res <- cmdscale(mds_dist, k = 2)
mds_df <- data.frame(
Dim1 = mds_res[,1],
Dim2 = mds_res[,2],
Name = rownames(mds_res)
)
# 3. MSTデータ作成関数
get_mst_segments <- function(df) {
d <- dist(df[, c("Dim1", "Dim2")])
mst <- spantree(d)
n <- nrow(df)
data.frame(
x = df$Dim1[2:n],
y = df$Dim2[2:n],
xend = df$Dim1[mst$kid],
yend = df$Dim2[mst$kid]
)
}
# 4. 可視化関数
plot_func_mst <- function(df, title) {
mst_segments <- get_mst_segments(df)
# 全体(ggplot)には label を指定せず、各レイヤーで必要な aes を指定します
ggplot(df, aes(x = Dim1, y = Dim2)) +
# MSTの線 (aesを独立させることで Name を参照しなくなります)
geom_segment(data = mst_segments,
aes(x = x, y = y, xend = xend, yend = yend),
color = "gray70", linetype = "solid") +
geom_point(color = "blue", size = 3) +
# ラベルはここで Name を指定
geom_text(aes(label = Name), vjust = -1.5, fontface = "bold", family =
"HiraKakuProN-W3") +
theme_minimal(base_family="HiraKakuProN-W3") +
expand_limits(x = range(df$Dim1)*1.5, y = range(df$Dim2)*1.5) +
coord_cartesian(clip = "off") +
labs(title = title, x = "Dimension 1", y = "Dimension 2")
}
# 実行
p1 <- plot_func_mst(ca_df, "CA with MST")
p2 <- plot_func_mst(mds_df, "MDS with MST")
# p1 + p2
p1
p2
```
[クラスタリングを付加]
[CA: Clustering & Full-Dim MST]
# 全次元、CA関連のみ
```{r}
library(readr)
library(ggplot2)
library(ca)
library(FactoMineR)
library(patchwork)
library(vegan)
# --- データの準備 ---
data <- read_csv("data11ku_PC.csv")
voting_data <- as.data.frame(data[,-1])
rownames(voting_data) <- data[[1]]
voting_numeric <- voting_data[, 2:ncol(voting_data)]
# --- 1. 対応分析 (CA) とクラスタリング ---
ca_res <- CA(voting_numeric, graph = FALSE)
# 全次元の座標(距離計算用)
ca_full_coord <- ca_res$col$coord
# 階層的クラスター分析 (Ward法、3グループ)
ca_hc <- hclust(dist(ca_full_coord), method = "ward.D2")
ca_cluster <- factor(cutree(ca_hc, k = 3))
ca_df <- data.frame(
Dim1 = ca_full_coord[,1],
Dim2 = ca_full_coord[,2],
Name = rownames(ca_full_coord),
Cluster = ca_cluster
)
# --- 3. MSTデータ作成関数 (全次元距離行列に対応) ---
get_mst_segments <- function(df, dist_obj) {
mst <- spantree(dist_obj)
n <- nrow(df)
data.frame(
x = df$Dim1[2:n],
y = df$Dim2[2:n],
xend = df$Dim1[mst$kid],
yend = df$Dim2[mst$kid]
)
}
# --- 4. 可視化関数 (Clustering + MST版) ---
plot_func_combined <- function(df, dist_obj, title) {
mst_segments <- get_mst_segments(df, dist_obj)
ggplot(df, aes(x = Dim1, y = Dim2)) +
geom_segment(data = mst_segments,
aes(x = x, y = y, xend = xend, yend = yend),
color = "gray80", linetype = "solid") +
geom_point(aes(color = Cluster), size = 3) +
geom_text(aes(label = Name, color = Cluster),
vjust = -1.5, fontface = "bold",
family = "HiraKakuProN-W3", show.legend = FALSE) +
theme_minimal(base_family="HiraKakuProN-W3") +
expand_limits(x = range(df$Dim1)*1.5, y = range(df$Dim2)*1.5) +
coord_cartesian(clip = "off") +
scale_color_brewer(palette = "Set1") +
labs(title = title, x = "Dimension 1", y = "Dimension 2", color = "Cluster")
}
# --- 5. 実行 ---
# CAのみを出力します
p1 <- plot_func_combined(ca_df, dist(ca_full_coord), "CA: Clustering & Full-Dim MST")
# 描画
p1
```
| 固定リンク
「分析メモ」カテゴリの記事
- 高市内閣の支持・不支持の理由の推移:NHK世論調査(2026.10.05)
- 「左右対立(という戦略)」の失敗だけで説明できるのか――2026年衆議院議員選挙について(2026.06.24)
- 「リベラル」、「伝統保守」というクラスターに注目して、そのネーミングの問題点、実体化して捉えることの妥当性について検討する(2026.06.19)
- 自民党支持者の多数派は「新自由主義右翼」(橋本健二氏)か?(2026.06.15)
- 格差に対する意識と流動性に対する意識が政党支持とどのように関係しているか(2026.06.14)