svgpvalue-qq
p-value Q–Q plot
Are there more small p-values than chance alone would give?
How to read it
- Read x as the expected p-value on a log scale, y as the observed one.
- On the diagonal: as expected under the null.
- Above the band: more small p-values than chance would give.
- Callout 1: These tests stand out above the band.
- Callout 2: Most p-values are on the diagonal.
- Callout 3: The band under the null.
Watch out
- Correlated tests can look inflated; check for shared samples.
Defaults
No settings: the chart draws one way.
Pinned in the code
- Draws the p-values as given; the band is the Beta(i, k + 1 − i) 2.5% and 97.5% quantiles of the i-th smallest of k null uniforms (pointwise, not simultaneous).
- Points above the band are coloured: more small p-values than the null predicts.
# p-value Q–Q plot — Codesplash Chart Lab (recipe "pvalue-qq")
# 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("pvalue-qq-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")
P = "p"
p = pd.to_numeric(df[P], errors="coerce").dropna()
p = np.sort(p[(p > 0) & (p <= 1)].to_numpy())
k = len(p)
i = np.arange(1, k + 1)
expected = -np.log10(i / (k + 1))
observed = -np.log10(p)
band_low = -np.log10(stats.beta.ppf(0.975, i, k + 1 - i)) # order statistics of uniform p-values
band_high = -np.log10(stats.beta.ppf(0.025, i, k + 1 - i))
hi = max(1, expected.max(), observed.max(), band_high.max()) * 1.06
fig, ax = plt.subplots(figsize=(6, 6))
ax.fill_between(expected, band_low, band_high, color=GREY, alpha=0.5, lw=0, label="95% band under the null")
ax.plot([0, hi], [0, hi], color=REF, linestyle="--", linewidth=1)
ax.plot(expected, observed, "o", color=PALETTE[0], markersize=4)
ax.set_xlim(0, hi)
ax.set_ylim(0, hi)
ax.set_xlabel("Expected −log10 p")
ax.set_ylabel("Observed −log10 p")
ax.legend(fontsize=8, loc="upper left")
plt.show()Needs pandas, numpy, matplotlib, scipy · reads pvalue-qq-demo.csv · app theme from the optional codesplash helper
# p-value Q–Q plot — Codesplash Chart Lab (recipe "pvalue-qq")
# 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("pvalue-qq-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")
P = "p"
p = pd.to_numeric(df[P], errors="coerce").dropna()
p = np.sort(p[(p > 0) & (p <= 1)].to_numpy())
k = len(p)
i = np.arange(1, k + 1)
expected = -np.log10(i / (k + 1))
observed = -np.log10(p)
band_low = -np.log10(stats.beta.ppf(0.975, i, k + 1 - i)) # order statistics of uniform p-values
band_high = -np.log10(stats.beta.ppf(0.025, i, k + 1 - i))
hi = max(1, expected.max(), observed.max(), band_high.max()) * 1.06
fig = go.Figure()
fig.add_scatter(x=np.r_[expected, expected[::-1]], y=np.r_[band_high, band_low[::-1]], fill="toself", mode="lines", line=dict(width=0), fillcolor=GREY, name="95% band under the null")
fig.add_scatter(x=[0, hi], y=[0, hi], mode="lines", line=dict(color=REF, dash="dash", width=1), showlegend=False)
fig.add_scatter(x=expected, y=observed, mode="markers", marker=dict(size=6, color=PALETTE[0]), name="p-values")
fig.update_xaxes(range=[0, hi], title="Expected −log10 p")
fig.update_yaxes(range=[0, hi], title="Observed −log10 p")
fig.show()Needs pandas, numpy, plotly, scipy · reads pvalue-qq-demo.csv · app theme from the optional codesplash helper
# p-value Q–Q plot — Codesplash Chart Lab (recipe "pvalue-qq")
# 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("pvalue-qq-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
p_col <- "p"
p_values <- suppressWarnings(as.numeric(df[[p_col]]))
p_values <- sort(p_values[!is.na(p_values) & p_values > 0 & p_values <= 1])
k <- length(p_values); i <- seq_len(k)
d <- data.frame(expected = -log10(i / (k + 1)), observed = -log10(p_values),
low = -log10(qbeta(0.975, i, k + 1 - i)), high = -log10(qbeta(0.025, i, k + 1 - i))) # order statistics of uniform p-values
hi <- max(1, d$expected, d$observed, d$high) * 1.06
p <- ggplot(d, aes(expected, observed)) +
geom_ribbon(aes(ymin = low, ymax = high), fill = grey_col, alpha = 0.5) +
geom_abline(slope = 1, intercept = 0, colour = ref_col, linetype = "dashed") +
geom_point(colour = cs_cat[1], size = 1.6) +
coord_cartesian(xlim = c(0, hi), ylim = c(0, hi)) +
labs(x = "Expected −log10 p", y = "Observed −log10 p") + theme_cs
print(p)Needs ggplot2 · reads pvalue-qq-demo.csv · app theme from the optional codesplashr helper