クラスタリング付きコレログラム
12 の開発指標について、r に応じて大きさと色を決めた corrplot 風の円を、階層的クラスタリングの順に並べたものです。

corrplot(method = "circle", order = "hclust") の Python 版です。各円の 面積 は |r| とともに大きくなり、色は符号を表すため、強い相関は目立ち、弱い相関は薄れて見えます。変数は 階層的クラスタリング(距離 1 − r に対する完全連結法)によって並べ替えられ、正の相関をもつ変数どうしが対角線に沿ってブロックにまとまります。枠は addrect = 3 と同様に3つのクラスターを囲んでいます。
設定は Dedinec, Stojkov, Warin & Kocarev (2026, Journal of Sustainable Development of Energy, Water and Environment Systems) による、不均等な開発の軌跡に関する研究です。データは説明のための シミュレーション であり、論文のデータや結果では ありません。150 か国を、所得、エネルギー、排出、健康、教育、経済構造に関する 12 の指標で評価したもので、ブロックが現れるよう相関のある3つの潜在因子から生成しています。並び順は scipy.cluster.hierarchy.linkage と leaves_list、枠は fcluster で求めています。
コードおよび図中のテキストは英語です。
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from matplotlib.patches import Circle, Rectangle
from scipy.cluster.hierarchy import fcluster, leaves_list, linkage
from scipy.spatial.distance import squareform
# Simulated data, set in the context of Dedinec, Stojkov, Warin & Kocarev (2026) [dedinec2026]
# Simulated countries: 12 indicators driven by three correlated latent factors
rng = np.random.default_rng(2026)
n = 150
income = rng.normal(0, 1, n)
human = 0.35 * income + 0.94 * rng.normal(0, 1, n)
green = -0.5 * income + 0.85 * rng.normal(0, 1, n)
loads = {"GDP per head (log)": (income, .9), "Energy use per head": (income, .8),
"CO₂ per head": (income, .75), "Urban population": (income, .65),
"Internet users": (income, .7), "Life expectancy": (human, .85),
"Years of schooling": (human, .8), "Electricity access": (human, .7),
"Child survival": (human, .8), "Renewables share": (green, .8),
"Agriculture (% GDP)": (green, .7), "Forest cover": (green, .55)}
df = pd.DataFrame({k: w * f + np.sqrt(1 - w**2) * rng.normal(0, 1, n) for k, (f, w) in loads.items()})
corr = df.corr()
# Hierarchical clustering on the distance 1 - r, then reorder the matrix
Z = linkage(squareform(1 - corr.to_numpy(), checks=False), method="complete")
order = leaves_list(Z)
corr = corr.iloc[order, order]
clusters = fcluster(Z, t=3, criterion="maxclust")[order]
k = len(corr)
fig, ax = plt.subplots(figsize=(8.4, 7.2))
cmap, norm = plt.get_cmap("RdBu"), plt.Normalize(-1, 1)
for i in range(k):
for j in range(k):
r = corr.iat[i, j]
if i == j:
continue
ax.add_patch(Circle((j, i), radius=0.47 * np.sqrt(abs(r)), color=cmap(norm(r)), lw=0))
# Boxes around the clusters (consecutive runs along the diagonal)
start = 0
for idx in range(1, k + 1):
if idx == k or clusters[idx] != clusters[start]:
ax.add_patch(Rectangle((start - 0.5, start - 0.5), idx - start, idx - start,
fill=False, ec="#1D1D1F", lw=1.6))
start = idx
ax.set_xticks(range(k), corr.columns, rotation=45, ha="right", rotation_mode="anchor")
ax.set_yticks(range(k), corr.index)
ax.set_xticks(np.arange(-0.5, k), minor=True)
ax.set_yticks(np.arange(-0.5, k), minor=True)
ax.grid(which="minor", color="#E5E5EA", lw=0.8)
ax.grid(which="major", visible=False)
ax.tick_params(which="minor", length=0)
ax.set_xlim(-0.5, k - 0.5)
ax.set_ylim(k - 0.5, -0.5)
ax.set_aspect("equal")
for s in ax.spines.values():
s.set_visible(False)
cb = fig.colorbar(plt.cm.ScalarMappable(norm=norm, cmap=cmap), ax=ax, shrink=0.6, pad=0.02,
ticks=[-1, -0.5, 0, 0.5, 1])
cb.set_label("Pearson r", color="#3A3A3C")
cb.outline.set_visible(False)
r_ge = corr.loc["GDP per head (log)", "Energy use per head"]
ax.set_title(f"Income, energy and emissions form one block (r = {r_ge:.2f}, GDP–energy)", pad=26)
ax.text(0, 1.02, f"Pearson r · n = {len(df)} countries (simulated) · complete linkage clustering on 1 − r · "
"circle area grows with |r|", transform=ax.transAxes, fontsize=9.5, color="#6E6E73")
plt.show()
# Correlogram with clustering: the R equivalent of plot.py
# Simulated data, set in the context of Dedinec, Stojkov, Warin & Kocarev (2026) [dedinec2026]
# R and NumPy draw different random numbers, so the values differ slightly from the Python figure.
library(ggplot2)
set.seed(2026)
# Simulated countries: 12 indicators driven by three correlated latent factors
n <- 150
income <- rnorm(n)
human <- 0.35 * income + 0.94 * rnorm(n)
green <- -0.5 * income + 0.85 * rnorm(n)
loads <- list("GDP per head (log)" = list(income, .9), "Energy use per head" = list(income, .8),
"CO\u2082 per head" = list(income, .75), "Urban population" = list(income, .65),
"Internet users" = list(income, .7), "Life expectancy" = list(human, .85),
"Years of schooling" = list(human, .8), "Electricity access" = list(human, .7),
"Child survival" = list(human, .8), "Renewables share" = list(green, .8),
"Agriculture (% GDP)" = list(green, .7), "Forest cover" = list(green, .55))
df <- as.data.frame(lapply(loads, function(l) l[[2]] * l[[1]] + sqrt(1 - l[[2]]^2) * rnorm(n)),
check.names = FALSE)
corr <- cor(df)
# Hierarchical clustering on the distance 1 - r, then reorder the matrix
# (corrplot is not installed: hclust() + cutree() do what order = "hclust", addrect = 3 do)
hc <- hclust(as.dist(1 - corr), method = "complete")
corr <- corr[hc$order, hc$order]
clusters <- cutree(hc, k = 3)[hc$order]
k <- nrow(corr)
# One circle per off-diagonal cell, drawn as a polygon so its radius is in data units
theta <- seq(0, 2 * pi, length.out = 60)
cells <- expand.grid(i = seq_len(k), j = seq_len(k))
cells <- cells[cells$i != cells$j, ]
cells$r <- corr[cbind(cells$i, cells$j)]
circles <- do.call(rbind, lapply(seq_len(nrow(cells)), function(m) {
rad <- 0.47 * sqrt(abs(cells$r[m]))
data.frame(id = m, r = cells$r[m], x = cells$j[m] + rad * cos(theta), y = cells$i[m] + rad * sin(theta))
}))
# Boxes around the clusters (consecutive runs along the diagonal)
runs <- rle(clusters)
ends <- cumsum(runs$lengths)
boxes <- data.frame(lo = ends - runs$lengths + 0.5, hi = ends + 0.5)
r_ge <- corr["GDP per head (log)", "Energy use per head"]
p <- ggplot() +
geom_hline(yintercept = seq(0.5, k + 0.5), colour = "#E5E5EA", linewidth = 0.4) +
geom_vline(xintercept = seq(0.5, k + 0.5), colour = "#E5E5EA", linewidth = 0.4) +
geom_polygon(data = circles, aes(x, y, group = id, fill = r)) +
geom_rect(data = boxes, aes(xmin = lo, xmax = hi, ymin = lo, ymax = hi), fill = NA,
colour = "#1D1D1F", linewidth = 0.8) +
scale_fill_distiller("Pearson r", palette = "RdBu", direction = 1, limits = c(-1, 1),
breaks = seq(-1, 1, 0.5)) +
scale_x_continuous(breaks = seq_len(k), labels = colnames(corr), expand = c(0, 0)) +
scale_y_reverse(breaks = seq_len(k), labels = rownames(corr), expand = c(0, 0)) +
coord_equal(xlim = c(0.5, k + 0.5), ylim = c(k + 0.5, 0.5)) +
labs(title = sprintf("Income, energy and emissions form one block (r = %.2f, GDP–energy)", r_ge),
subtitle = sprintf("Pearson r · n = %d countries (simulated) · complete linkage clustering on 1 − r · circle area grows with |r|",
n),
x = NULL, y = NULL) +
theme_minimal(base_size = 11) +
theme(plot.title = element_text(face = "bold"),
plot.title.position = "plot",
plot.subtitle = element_text(size = 8.5, colour = "#6E6E73"),
axis.text = element_text(colour = "#1D1D1F", size = 9.5),
axis.text.x = element_text(angle = 45, hjust = 1),
panel.grid = element_blank(),
legend.title = element_text(size = 9.5, colour = "#3A3A3C"),
legend.key.height = unit(2.2, "lines"))
print(p)
図にはオプションの PyStatlas スタイル を使用しています。同じ見た目にするには plt.style.use("pystatlas.mplstyle") を追加してください。
データの出典
- Simulated data. Simulated data with a fixed random seed, created for this example.
No real respondents, documents or measurements: the numbers only illustrate how the chart and its statistics work. - Dedinec, Stojkov, Warin & Kocarev (2026), the development compression trap. Dedinec, A., Stojkov, A., Warin, T., & Kocarev, L. (2026). The development compression trap: A framework for understanding uneven development trajectories. Journal of Sustainable Development of Energy, Water and Environment Systems, 15(1).
The example is set in the context of this paper; unless its description says otherwise, the numbers are simulated and are not the paper's data or results.
これらの例は統計手法を説明するためのものです。項目に別の記載がない限り数値はシミュレーションであり、図のタイトルはこのシミュレーションデータで図が示す内容を述べたものです。引用した論文の知見でも、実質的な問いに対する著者の立場でもありません。結果、背景、データについては、ここに挙げた論文を参照してください。
引用方法
PyStatlas は Thierry Warin が作成・運営しています。研究や業務に役立った場合は、ぜひ引用してください。
Warin, T. (2026). Correlogram with clustering. In PyStatlas: A Python Gallery of Statistical Graphics for the Social Sciences (Version 1.1.0) [Computer software]. https://warin.ca/pystatlas/correlation/correlogram-bubble/
@misc{warin2026statlas,
author = {Warin, Thierry},
title = {PyStatlas: A Python Gallery of Statistical Graphics for the Social Sciences},
year = {2026},
version = {1.1.0},
howpublished = {\url{https://warin.ca/pystatlas/}},
note = {Source code: https://github.com/warint/pystatlas}
}