svgconsensus-heatmap
Consensus matrix
How often do two rows end up in the same cluster when the data is resampled?
How to read it
- Read a cell as how often two rows cluster together across resamples.
- Solid dark blocks: stable clusters.
- Mottled cells between blocks: rows that move between clusters.
- Callout 1: A solid block: a stable cluster.
- Callout 2: Another block.
- Callout 3: Pale: these rows rarely cluster together.
Watch out
- Stable doesn't mean real: an artefact of scaling can be perfectly stable.
Defaults
No settings: the chart draws one way.
Pinned in the code
- Draws the run's numbers as given (computed by the host's cs-ml evaluation or the Python/R engine); the chart computes nothing statistical.
- Each cell is (resamples with both rows clustered together) / (resamples containing both rows); rows are a fixed evenly spaced sample, grouped by their cluster on the full data.
- PAC (share of pairs strictly between 0.1 and 0.9) summarises ambiguity: lower is crisper.
# Consensus matrix — Codesplash Chart Lab (recipe "consensus-heatmap")
# controls: defaults
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
try:
import codesplash as cs # optional: the app's chart theme
except ImportError:
cs = None
if cs:
cs.use_codesplash(dark=False)
PALETTE = cs.palette(dark=False)["categorical"] if cs else plt.rcParams["axes.prop_cycle"].by_key()["color"]
df = pd.read_csv("consensus-heatmap-demo.csv", keep_default_na=False, na_values=[""]) # demo data (only empty cells are missing): Chart Lab → Code → Download demo CSV
R = "row (by cluster)"
C = "column (by cluster)"
V = "share of resamples together"
m = df.pivot_table(index=R, columns=C, values=V, aggfunc="mean").sort_index().sort_index(axis=1)
fig, ax = plt.subplots(figsize=(6, 5))
im = ax.imshow(m.to_numpy(), cmap="Blues", aspect="equal", interpolation="nearest", extent=[m.columns.min() - 0.5, m.columns.max() + 0.5, m.index.max() + 0.5, m.index.min() - 0.5])
fig.colorbar(im, label=V, shrink=0.8)
ax.set_xlabel("row (by cluster)")
ax.set_ylabel("row (by cluster)")
plt.show()Needs pandas, numpy, matplotlib · reads consensus-heatmap-demo.csv · app theme from the optional codesplash helper
# Consensus matrix — Codesplash Chart Lab (recipe "consensus-heatmap")
# controls: defaults
import numpy as np
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
try:
import codesplash as cs # optional: the app's chart theme
except ImportError:
cs = None
if cs:
cs.use_codesplash(dark=False)
PALETTE = cs.palette(dark=False)["categorical"] if cs else px.colors.qualitative.Plotly
df = pd.read_csv("consensus-heatmap-demo.csv", keep_default_na=False, na_values=[""]) # demo data (only empty cells are missing): Chart Lab → Code → Download demo CSV
R = "row (by cluster)"
C = "column (by cluster)"
V = "share of resamples together"
m = df.pivot_table(index=R, columns=C, values=V, aggfunc="mean").sort_index().sort_index(axis=1)
fig = go.Figure(go.Heatmap(z=m.to_numpy(), x=m.columns, y=m.index, colorscale="Blues", reversescale=False, colorbar=dict(title=V)))
fig.update_layout(xaxis_title="row (by cluster)", yaxis_title="row (by cluster)", yaxis_autorange="reversed", yaxis_scaleanchor="x")
fig.show()Needs pandas, numpy, plotly · reads consensus-heatmap-demo.csv · app theme from the optional codesplash helper
# Consensus matrix — Codesplash Chart Lab (recipe "consensus-heatmap")
# controls: defaults
library(ggplot2)
has_cs <- isTRUE(requireNamespace("codesplashr", quietly = TRUE)) # optional: the app's chart theme
theme_cs <- if (has_cs) codesplashr::theme_codesplash(dark = FALSE) else theme_minimal()
cs_cat <- if (has_cs) codesplashr::cs_palette(dark = FALSE)$categorical else grDevices::palette.colors(8, "Okabe-Ito")
if (has_cs) update_geom_defaults("text", list(colour = codesplashr::cs_palette(dark = FALSE)$foreground))
df <- read.csv("consensus-heatmap-demo.csv", check.names = FALSE, stringsAsFactors = FALSE, na.strings = "") # empty cells are missing; Chart Lab → Code → Download demo CSV
tok <- if (has_cs) codesplashr::cs_palette(dark = FALSE) else list()
seq_ramp <- if (is.null(tok$sequential)) c("#eff6fd", "#1a469d") else tok$sequential # low → high
div_ramp <- if (is.null(tok$diverging)) c("#c13e2e", "#f3f5f8", "#0859ac") else tok$diverging # −, 0, +
ref_col <- if (is.null(tok$reference)) "#4e5661" else tok$reference
grey_col <- if (is.null(tok$dim)) "#ced1d6" else tok$dim
R <- "row (by cluster)"
C <- "column (by cluster)"
V <- "share of resamples together"
d <- data.frame(r = df[[R]], c = df[[C]], v = df[[V]])
p <- ggplot(d, aes(c, r, fill = v)) +
geom_raster() +
scale_fill_gradient(low = seq_ramp[[1]], high = seq_ramp[[length(seq_ramp)]], name = V) +
scale_y_reverse() + coord_equal() +
labs(x = "row (by cluster)", y = "row (by cluster)") + theme_cs
print(p)Needs ggplot2 · reads consensus-heatmap-demo.csv · app theme from the optional codesplashr helper