svggwas-qq
QQ plot of p-values (with λ_GC)
Do the p-values of a scan follow the null except for a few real signals, or are they inflated across the board?
How to read it
- Read the dashed line as what p-values look like when no marker is associated; points on it are null.
- Points that follow the line and rise only at the top right are compatible with a few real signals on a calibrated test.
- Points that leave the line early and stay above it, with λ_GC well above 1, suggest inflation from structure, relatedness or a mis-specified test.
- Callout 1: λ_GC: median χ² relative to the null.
- Callout 2: Most markers on the null line.
- Callout 3: A few strong signals leave the line at the end.
Watch out
- λ_GC needs hundreds of mostly null markers; with a handful of markers or a candidate set it means little.
- A polygenic trait in a large sample raises λ without any structure; LD score regression separates the two, a QQ plot cannot.
Defaults
- λ_GC from the method
- 0
- The engine's λ_GC to show; 0 estimates it from the p-values drawn (median χ²₁ over 0.4549).
- Range 0 to 100, step 0.001
- 95% band
- On
- Pointwise 95% range of each ordered p under the null, from Beta(i, n − i + 1).
Pinned in the code
- Draws the genetics method's published numbers as given (codesplash ≡ codesplashr); only the labelled reference curves are computed for display.
- Expected p-values are R's ppoints(n): (i − a)/(n + 1 − 2a) with a = 3/8 for n ≤ 10 and ½ otherwise; p outside (0, 1] is left out.
- λ_GC = median of the χ²₁ upper quantiles of p, divided by qchisq(0.5, 1) = 0.4549 (Devlin & Roeder 1999); a λ given by the method is shown as given.
- The band holds the i-th smallest of n uniform p-values with 95% probability (Beta(i, n − i + 1)); it is pointwise, not simultaneous. Above 400 p-values it is evaluated at log-spaced ranks.
Use with care
Research use only: genotype charts describe a sample; they are not a clinical or diagnostic result.
# QQ plot of p-values (with λ_GC) — Codesplash Chart Lab (recipe "gwas-qq")
# controls: lambda=0, band=true
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("gwas-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"
LAMBDA, BAND = 0, True # λ_GC from the method (0 = estimate from these p-values), 95% band
p = pd.to_numeric(df[P], errors="coerce")
p = np.sort(p[(p > 0) & (p <= 1)].to_numpy())
n = len(p)
a = 3 / 8 if n <= 10 else 0.5
e = (np.arange(1, n + 1) - a) / (n + 1 - 2 * a) # R's ppoints(n)
lam = LAMBDA if LAMBDA > 0 else np.median(stats.chi2.isf(p, 1)) / stats.chi2.ppf(0.5, 1) # Devlin & Roeder (1999)
i = np.arange(1, n + 1)
band_lo, band_hi = -np.log10(stats.beta.ppf(0.975, i, n - i + 1)), -np.log10(stats.beta.ppf(0.025, i, n - i + 1)) # every rank (the app samples ranks above 400)
ex, ob = -np.log10(e), -np.log10(p)
fig, ax = plt.subplots(figsize=(6, 6))
if BAND and n:
ax.fill_between(ex, band_lo, band_hi, color=PALETTE[0], alpha=0.16, linewidth=0, label="95% band")
lim = ex.max() if n else 1
ax.plot([0, lim], [0, lim], color=REF, linestyle="--", linewidth=1, label="expected under the null")
ax.scatter(ex, ob, s=8, color=PALETTE[0], linewidths=0, label="markers")
ax.text(0.03, 0.97, f"λ_GC = {lam:.3f}", transform=ax.transAxes, va="top")
ax.set_xlabel("expected −log₁₀ p")
ax.set_ylabel("observed −log₁₀ p")
ax.set_xlim(0, None)
ax.set_ylim(0, None)
ax.legend(frameon=False, loc="lower right")
plt.show()Needs pandas, numpy, matplotlib, scipy · reads gwas-qq-demo.csv · app theme from the optional codesplash helper
# QQ plot of p-values (with λ_GC) — Codesplash Chart Lab (recipe "gwas-qq")
# controls: lambda=0, band=true
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("gwas-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"
LAMBDA, BAND = 0, True # λ_GC from the method (0 = estimate from these p-values), 95% band
p = pd.to_numeric(df[P], errors="coerce")
p = np.sort(p[(p > 0) & (p <= 1)].to_numpy())
n = len(p)
a = 3 / 8 if n <= 10 else 0.5
e = (np.arange(1, n + 1) - a) / (n + 1 - 2 * a) # R's ppoints(n)
lam = LAMBDA if LAMBDA > 0 else np.median(stats.chi2.isf(p, 1)) / stats.chi2.ppf(0.5, 1) # Devlin & Roeder (1999)
i = np.arange(1, n + 1)
band_lo, band_hi = -np.log10(stats.beta.ppf(0.975, i, n - i + 1)), -np.log10(stats.beta.ppf(0.025, i, n - i + 1)) # every rank (the app samples ranks above 400)
ex, ob = -np.log10(e), -np.log10(p)
fig = go.Figure()
if BAND and n:
fig.add_scatter(x=np.r_[ex, ex[::-1]], y=np.r_[band_hi, band_lo[::-1]], fill="toself", mode="none", fillcolor=PALETTE[0], opacity=0.2, name="95% band")
lim = ex.max() if n else 1
fig.add_scatter(x=[0, lim], y=[0, lim], mode="lines", line=dict(color=REF, dash="dash"), name="expected under the null")
fig.add_scattergl(x=ex, y=ob, mode="markers", marker=dict(size=4, color=PALETTE[0]), name="markers", customdata=p, hovertemplate="p = %{customdata:.3g}<extra></extra>")
fig.add_annotation(xref="paper", yref="paper", x=0.02, y=0.98, text=f"λ<sub>GC</sub> = {lam:.3f}", showarrow=False, xanchor="left")
fig.update_layout(xaxis_title="expected −log₁₀ p", yaxis_title="observed −log₁₀ p", xaxis_rangemode="tozero", yaxis_rangemode="tozero")
fig.show()Needs pandas, numpy, plotly, scipy · reads gwas-qq-demo.csv · app theme from the optional codesplash helper
# QQ plot of p-values (with λ_GC) — Codesplash Chart Lab (recipe "gwas-qq")
# controls: lambda=0, band=true
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("gwas-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"
lambda <- 0; band <- TRUE # lambda_GC from the method (0 = estimate from these p-values), 95% band
pv <- suppressWarnings(as.numeric(df[[p_col]]))
pv <- sort(pv[!is.na(pv) & pv > 0 & pv <= 1]); n <- length(pv)
e <- ppoints(n)
lam <- if (lambda > 0) lambda else median(qchisq(pv, 1, lower.tail = FALSE)) / qchisq(0.5, 1) # Devlin & Roeder (1999)
i <- seq_len(n)
d <- data.frame(x = -log10(e), y = -log10(pv), lo = -log10(qbeta(0.975, i, n - i + 1)), hi = -log10(qbeta(0.025, i, n - i + 1)))
p <- ggplot(d, aes(x, y))
if (band) p <- p + geom_ribbon(aes(ymin = lo, ymax = hi), fill = cs_cat[1], alpha = 0.16)
p <- p + geom_abline(slope = 1, intercept = 0, colour = ref_col, linetype = "dashed") +
geom_point(colour = cs_cat[1], size = 1) +
annotate("text", x = 0, y = max(c(d$y, d$hi), na.rm = TRUE), label = sprintf("lambda[GC] == %.3f", lam), parse = TRUE, hjust = 0, vjust = 1) +
labs(x = "expected -log10 p", y = "observed -log10 p") + theme_cs
print(p)Needs ggplot2 · reads gwas-qq-demo.csv · app theme from the optional codesplashr helper