« クローバー | トップページ | Greenacre's Symbiplot »

2026年2月 3日 (火)

FactoMineRのCAパッケージのデフォルトはsymmetric map

衆議院議員選挙2024年福岡11区のデータ

Screenshot-20260203-at-124842 Screenshot-20260203-at-124012 Screenshot-20260203-at-124023


```{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
```

[クラスタリングを付加]

Screenshot-20260203-at-130554 Screenshot-20260203-at-130614

[CA: Clustering & Full-Dim MST]

Screenshot-20260203-at-135420

# 全次元、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
```

|

« クローバー | トップページ | Greenacre's Symbiplot »

「分析メモ」カテゴリの記事