svgmetareg-bubble
Meta-regression bubble plot
Does the study effect change with a study-level moderator?
How to read it
- Read x as the moderator, y as the effect.
- Bubble size shows weight.
- Read the line's slope and band for how the effect changes.
- Callout 1: A study at low latitude.
- Callout 2: Effects grow with latitude.
- Callout 3: Heavier studies count more.
Watch out
- It's ecological: a study-level trend need not hold for individuals inside studies.
Defaults
No settings: the chart draws one way.
Pinned in the code
- Draws the meta-analysis engine's numbers as given (codesplash.stats.meta in Python ≡ codesplashr in R); the chart computes nothing statistical.
- The line is b₀ + b₁·x from the engine (controls intercept, slope); the band is ± crit·√(v₀₀ + 2x·v₀₁ + x²·v₁₁) from the engine's coefficient covariance and critical value.
- Weights are 1/(vᵢ + τ²) — larger bubbles pull the line harder.
# Meta-regression bubble plot — Codesplash Chart Lab (recipe "metareg-bubble")
# controls: defaults
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from scipy import stats
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("metareg-bubble-demo.csv", keep_default_na=False, na_values=[""]) # demo data (only empty cells are missing): Chart Lab → Code → Download demo CSV
_tokens = cs.palette(dark=False) if cs else {}
SEQ = _tokens.get("sequential", ["#eff6fd", "#1a469d"]) # sequential ramp (low → high)
DIV = _tokens.get("diverging", ["#c13e2e", "#f3f5f8", "#0859ac"]) # negative, midpoint, positive
REF = _tokens.get("reference", "#4e5661")
GREY = _tokens.get("dim", "#ced1d6")
X = "absolute latitude"
Y = "log risk ratio"
WEIGHT = "weight"
B0, B1 = None, None # the fit the engine published
V00, V01, V11, CRIT = None, None, None, None
d = df.dropna(subset=[X, Y])
w = d[WEIGHT] if WEIGHT else pd.Series(1.0, index=d.index)
fig, ax = plt.subplots(figsize=(7.5, 5))
if B0 is not None and B1 is not None:
pad = (d[X].max() - d[X].min()) * 0.05
xs = np.linspace(d[X].min() - pad, d[X].max() + pad, 65)
fit = B0 + B1 * xs
if V00 is not None and V01 is not None and V11 is not None:
se = np.sqrt(np.maximum(0, V00 + 2 * xs * V01 + xs ** 2 * V11)) # Var(b0 + b1·x) from the coefficient covariance
crit = CRIT if CRIT is not None else stats.norm.ppf(0.975)
ax.fill_between(xs, fit - crit * se, fit + crit * se, color=PALETTE[0], alpha=0.15, lw=0)
ax.plot(xs, fit, color=PALETTE[0], linewidth=1.5)
ax.scatter(d[X], d[Y], s=20 + 300 * w / w.max(), facecolors="none", edgecolors=REF)
ax.set_xlabel(X)
ax.set_ylabel(Y)
plt.show()Needs pandas, numpy, matplotlib, scipy · reads metareg-bubble-demo.csv · app theme from the optional codesplash helper
# Meta-regression bubble plot — Codesplash Chart Lab (recipe "metareg-bubble")
# controls: defaults
import numpy as np
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
from scipy import stats
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("metareg-bubble-demo.csv", keep_default_na=False, na_values=[""]) # demo data (only empty cells are missing): Chart Lab → Code → Download demo CSV
_tokens = cs.palette(dark=False) if cs else {}
SEQ = _tokens.get("sequential", ["#eff6fd", "#1a469d"]) # sequential ramp (low → high)
DIV = _tokens.get("diverging", ["#c13e2e", "#f3f5f8", "#0859ac"]) # negative, midpoint, positive
REF = _tokens.get("reference", "#4e5661")
GREY = _tokens.get("dim", "#ced1d6")
X = "absolute latitude"
Y = "log risk ratio"
WEIGHT = "weight"
LABEL = None
B0, B1 = None, None # the fit the engine published
V00, V01, V11, CRIT = None, None, None, None
d = df.dropna(subset=[X, Y])
w = d[WEIGHT] if WEIGHT else pd.Series(1.0, index=d.index)
fig = go.Figure()
if B0 is not None and B1 is not None:
pad = (d[X].max() - d[X].min()) * 0.05
xs = np.linspace(d[X].min() - pad, d[X].max() + pad, 65)
fit = B0 + B1 * xs
if V00 is not None and V01 is not None and V11 is not None:
se = np.sqrt(np.maximum(0, V00 + 2 * xs * V01 + xs ** 2 * V11)) # Var(b0 + b1·x) from the coefficient covariance
crit = CRIT if CRIT is not None else stats.norm.ppf(0.975)
fig.add_scatter(x=np.r_[xs, xs[::-1]], y=np.r_[fit + crit * se, (fit - crit * se)[::-1]], fill="toself", mode="lines", line=dict(width=0), fillcolor=PALETTE[0], opacity=0.15, name="interval")
fig.add_scatter(x=xs, y=fit, mode="lines", line=dict(color=PALETTE[0], width=2), name="fit")
fig.add_scatter(x=d[X], y=d[Y], mode="markers", marker=dict(size=6 + 24 * np.sqrt(w / w.max()), color="rgba(0,0,0,0)", line=dict(color=REF, width=1)), name="study", text=d[LABEL].astype(str) if LABEL else None)
fig.update_layout(xaxis_title=X, yaxis_title=Y)
fig.show()Needs pandas, numpy, plotly, scipy · reads metareg-bubble-demo.csv · app theme from the optional codesplash helper
# Meta-regression bubble plot — Codesplash Chart Lab (recipe "metareg-bubble")
# 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("metareg-bubble-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
x_col <- "absolute latitude"
y_col <- "log risk ratio"
weight_col <- "weight"
b0 <- NULL; b1 <- NULL # the fit the engine published
v00 <- NULL; v01 <- NULL; v11 <- NULL; crit <- NULL
d <- df[!is.na(df[[x_col]]) & !is.na(df[[y_col]]), ]
d$x <- d[[x_col]]; d$y <- d[[y_col]]
d$w <- if (is.null(weight_col)) rep(1, nrow(d)) else d[[weight_col]]
p <- ggplot()
if (!is.null(b0) && !is.null(b1)) {
pad <- diff(range(d$x)) * 0.05
line <- data.frame(x = seq(min(d$x) - pad, max(d$x) + pad, length.out = 65))
line$fit <- b0 + b1 * line$x
if (!is.null(v00) && !is.null(v01) && !is.null(v11)) {
se <- sqrt(pmax(0, v00 + 2 * line$x * v01 + line$x^2 * v11)) # Var(b0 + b1·x) from the coefficient covariance
k <- if (is.null(crit)) qnorm(0.975) else crit
line$low <- line$fit - k * se; line$high <- line$fit + k * se
p <- p + geom_ribbon(data = line, aes(x, ymin = low, ymax = high), fill = cs_cat[1], alpha = 0.15)
}
p <- p + geom_line(data = line, aes(x, fit), colour = cs_cat[1], linewidth = 0.8)
}
p <- p + geom_point(data = d, aes(x, y, size = w), shape = 1, colour = ref_col) +
scale_size_area(max_size = 9, guide = "none") +
labs(x = x_col, y = y_col) + theme_cs
print(p)Needs ggplot2 · reads metareg-bubble-demo.csv · app theme from the optional codesplashr helper