svgtopic-sizes
Topic sizes
How large is each topic, and how many documents fit no topic at all?
How to read it
- Read each bar as one topic: its length is how many documents fell into it, and its label lists the words that set it apart.
- Topics are ordered from largest to smallest; the grey bar at the end holds the documents that fit no topic.
- Check the grey bar first: a long one means many documents were left unassigned by the settings.
- Callout 1: The largest topic and its distinctive words.
- Callout 2: Outliers: documents in no dense group.
- Callout 3: Class-based TF-IDF words: frequent here, rare in other topics.
Watch out
- Topic sizes depend on the smallest topic size and the seed; rerun with other settings before trusting a small topic.
- Distinctive words describe the topic, not every document in it.
Defaults
No settings: the chart draws one way.
Pinned in the code
- Draws what the Python or R engine computed; the chart computes nothing statistical.
- Topics are ordered by size; the outlier group (HDBSCAN noise, topic −1) is drawn grey and last whatever its size.
- Bar labels are the topic's highest class-based TF-IDF words — words frequent in this topic and rare in the others.
- A large outlier bar means the density clustering left many documents unassigned; it is a property of the settings (smallest topic size), not of the documents alone.
# Topic sizes — Codesplash Chart Lab (recipe "topic-sizes")
# 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("topic-sizes-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")
TOPIC = "topic"
DOCS = "documents"
TERMS = "top terms"
OUT = "outliers"
d = df.dropna(subset=[DOCS]).copy()
d["_out"] = d[OUT].astype(bool) if OUT else d[TOPIC].astype(str).str.fullmatch(r"(?i)outliers?|-1|noise")
d = d.sort_values(["_out", DOCS], ascending=[True, False], kind="stable").reset_index(drop=True) # outliers last
fig, ax = plt.subplots(figsize=(8, 0.45 * len(d) + 1.2))
y = np.arange(len(d))[::-1]
ax.barh(y, d[DOCS], color=[GREY if o else PALETTE[0] for o in d["_out"]])
if TERMS:
for yi, n, t in zip(y, d[DOCS], d[TERMS].fillna("")):
ax.annotate(str(t), (n, yi), xytext=(4, 0), textcoords="offset points", va="center", fontsize=8)
ax.set_yticks(y, d[TOPIC].astype(str))
ax.set_xlabel(DOCS)
ax.set_title("Documents per topic (outliers last)")
plt.show()Needs pandas, numpy, matplotlib · reads topic-sizes-demo.csv · app theme from the optional codesplash helper
# Topic sizes — Codesplash Chart Lab (recipe "topic-sizes")
# 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("topic-sizes-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")
TOPIC = "topic"
DOCS = "documents"
TERMS = "top terms"
OUT = "outliers"
d = df.dropna(subset=[DOCS]).copy()
d["_out"] = d[OUT].astype(bool) if OUT else d[TOPIC].astype(str).str.fullmatch(r"(?i)outliers?|-1|noise")
d = d.sort_values(["_out", DOCS], ascending=[True, False], kind="stable").reset_index(drop=True) # outliers last
fig = go.Figure(go.Bar(
x=d[DOCS], y=d[TOPIC].astype(str), orientation="h", text=d[TERMS] if TERMS else None, textposition="outside",
marker_color=[GREY if o else PALETTE[0] for o in d["_out"]],
))
fig.update_yaxes(autorange="reversed")
fig.update_layout(xaxis_title=DOCS, title="Documents per topic (outliers last)")
fig.show()Needs pandas, numpy, plotly · reads topic-sizes-demo.csv · app theme from the optional codesplash helper
# Topic sizes — Codesplash Chart Lab (recipe "topic-sizes")
# 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("topic-sizes-demo.csv", check.names = FALSE, stringsAsFactors = FALSE, na.strings = "") # empty cells are missing; Chart Lab → Code → Download demo CSV
df[["outliers"]] <- as.logical(df[["outliers"]])
df[["topic"]] <- factor(df[["topic"]])
df[["top terms"]] <- factor(df[["top terms"]])
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
topic_col <- "topic"
docs_col <- "documents"
terms_col <- "top terms"
out_col <- "outliers"
d <- df[!is.na(df[[docs_col]]), ]
d$out <- if (is.null(out_col)) grepl("^(outliers?|-1|noise)$", as.character(d[[topic_col]]), ignore.case = TRUE) else as.logical(d[[out_col]])
d <- d[order(d$out, -d[[docs_col]]), ] # largest first, outliers last
d$topic <- factor(as.character(d[[topic_col]]), levels = rev(unique(as.character(d[[topic_col]]))))
d$terms <- if (is.null(terms_col)) "" else as.character(d[[terms_col]])
p <- ggplot(d, aes(x = .data[[docs_col]], y = topic, fill = out)) +
geom_col(width = 0.75) +
geom_text(aes(label = terms), hjust = -0.05, size = 3) +
scale_fill_manual(values = c(`FALSE` = cs_cat[1], `TRUE` = grey_col), guide = "none") +
scale_x_continuous(expand = expansion(mult = c(0, 0.35))) +
labs(x = docs_col, y = NULL, title = "Documents per topic (outliers last)") + theme_cs
print(p)Needs ggplot2 · reads topic-sizes-demo.csv · app theme from the optional codesplashr helper