Lecture 9: R Graphics & Transcriptomic Statistics — statistics that turn numbers into claims, plots that show them.
1. Review from Lecture 8
1. Review from Lecture 8
2. Descriptive Statistics
3. Shape, Distribution & Normality
4. p-value & Statistical Tests
5. Correlation & Regression
6. Statistics Practice
7. From Numbers to Figures
8. The Common Plots
9. Reading a ggplot2 Call
10. Export for Publication
11. Graphics Practice
1. Review from Lecture 8
⚡ Q1: Find the counts for gene X — which line gets the row?
A.df[2, ]
B.df[3, ]
C.df[, 2]
D.df$X
1. Review from Lecture 8
✅ B — df[3, ] is the whole row for gene X
A is the 2nd row — BRCA1, not X. The comma is right; the number is the wrong gene.
C is the 2nd column as a vector: 1200 450 89. The comma comes before the 2.
D is the quiet trap: $ takes a column by name, and there is no column called X, so it returns NULL with no error.
Tip
The way you do it in real code: df[df$gene == "X", ]. Row names are not row numbers, so index by a condition.
A data frame gives you a row only when the comma is there: [row, column]
1. Review from Lecture 8
⚡ Q2: Which line gets the counts, not the wrapper?
A.qc_report["umi_counts"]
B.qc_report[["umi_counts"]]
C.qc_report$sample_name
D.qc_report[1]
1. Review from Lecture 8
✅ B — [[ ]] unwraps, [ ] keeps the wrapper
A single brackets keep the wrapper: the printout starts with $umi_counts, then the numbers.
C$ picks by name, and sample_name is a string: chr "Visium_HD_01", not the counts.
D[1] picks by position and keeps the wrapper: List of 1 with $sample_name.
Note
Use [[ ]] when you want the value, [ ] when you want a smaller list.
str() shows the difference immediately.
1. Review from Lecture 8
⚡ Q3: Which line averages only the measured spots?
A.mean(umi, rm.na = TRUE)
B.mean(umi, na.rm = TRUE)
C.mean(!is.na(umi))
D.mean(umi, na.rm = NA)
1. Review from Lecture 8
✅ B — the flag is spelled na.rm, and it must be TRUE
Amean() reads na.rm. rm.na lands in ... and is ignored, so the NA stays and the result is NA.
C!is.na(umi) is a logical vector: TRUE TRUE FALSE TRUE TRUE. Averaging it gives the share of measured spots, 0.8, not the counts.
D the flag has to be TRUE. isTRUE(NA) is FALSE, so nothing is dropped and the answer stays NA.
Count the holes first, then decide. na.rm = TRUE is a choice you report, not a default.
1. Review from Lecture 8
A number means nothing on its own
A p-value, not a p-list.
— the statistician’s rule of thumb, on this deck’s own theme
One count answers one question. Statistics is what lets us say:
what the data looks like (descriptive)
what the data is likely to be (distribution)
what we can claim from it (inference)
Today: 8 tools, in the order you use them.
1. Review from Lecture 8
🧰 The statistician’s toolbox
Describe: mean, median, sd, IQR
Observe the shape: is it a normal distribution?
Check normality: Shapiro-Wilk, Q-Q plot
Choose a test: parametric or non-parametric
Read the p-value
Correct for multiple testing
Correlate and regress
Plot it — devices, ggplot2, publication figures
2. Descriptive Statistics
1. Review from Lecture 8
2. Descriptive Statistics
3. Shape, Distribution & Normality
4. p-value & Statistical Tests
5. Correlation & Regression
6. Statistics Practice
7. From Numbers to Figures
8. The Common Plots
9. Reading a ggplot2 Call
10. Export for Publication
11. Graphics Practice
2. Descriptive Statistics
🎯 The problem: seven spots — how do you describe them?
what’s the range of our data? — the span
what is a typical spot? — the average
how spread out are the counts? — the spread
2. Descriptive Statistics
The Range — min to max
2. Descriptive Statistics
The Center — where the middle is
mean: add them, divide by how many
median: the middle value, after sorting
2. Descriptive Statistics
Quartiles — the median of each half
Q1: the median of the lower half
Q3: the median of the upper half
Two ways to choose the halves. Keeping the median in both halves gives 3 and 14 — what R’s quantile(), fivenum() and boxplot() report. Excluding the median is the calculator rule: lower half 1, 2, 4 → 2, upper half 8, 20, 40 → 20.
the typical range — sd sets the band mean − sd to mean + sd; inside = typical, outside = stands out → p-value
the summary table — every group reports mean ± sd, and Shapiro-Wilk tests the normal distribution sd defines
2. Descriptive Statistics
🔍 variance vs sd — same information, two units
⚖️ sd is what decides a claim
2. Descriptive Statistics
⚡ Q1: Seven tumor spots, mean 11.57. You want one number saying how typical a spot is, in UMI counts. Which?
A.var(counts)
B.sd(counts)
2. Descriptive Statistics
✅ B — sd is the spread in the same units as the counts
B sd is in counts, so mean ± sd is a band the reader can check against the dots.
A variance is squared counts — for tests and models, not for reporting.
Report the sd, keep the variance for the model.
2. Descriptive Statistics
⚡ Q2: mean(tight) and mean(wide) are both 11. Which line shows the groups are different?
A.median(tight) vs median(wide)
B.sd(tight) vs sd(wide)
C.length(tight) vs length(wide)
D.sum(tight) vs sum(wide)
2. Descriptive Statistics
✅ B — same centre, five times the spread
A both medians are 11, so the middle does not tell the groups apart.
C both groups have 5 spots; the same n says nothing about spread.
D both sums are 55, so the total is identical too.
B sd is the only number that separates them: a tight group is repeatable, a wide group is noisy
Note
The mean is the same number; the data is not the same data.
2. Descriptive Statistics
⚡ Q3: One 500 joins nine small counts. Which number moves most?
A. the median
B. the mean
C. both move by the same amount
D. neither: the centre is fixed
2. Descriptive Statistics
✅ B — the mean carries 500, the median ignores its size
A the median sees only the middle two positions: 6.5.
C the mean absorbs 500, the median gains nothing — not symmetric.
D the centre moves by 48.9. That gap is the finding.
B the mean adds every value, so 500 enters as 500 / 10 = 50 per spot.
Tip
Report both. If mean and median sit far apart, that gap is the finding.
2. Descriptive Statistics
Too lazy? R got you covered!
2. Descriptive Statistics
✅ Summary
three questions: the span, the typical spot, the spread
range = min to max · mean = add and divide · median = middle
quartiles = median of each half · IQR = the middle 50%
variance = squared units, for tests · sd = the data’s units, for reporting
mean + median together show the shape — one outlier moves the mean
summary() prints the whole set
3. Shape, Distribution & Normality
1. Review from Lecture 8
2. Descriptive Statistics
3. Shape, Distribution & Normality
4. p-value & Statistical Tests
5. Correlation & Regression
6. Statistics Practice
7. From Numbers to Figures
8. The Common Plots
9. Reading a ggplot2 Call
10. Export for Publication
11. Graphics Practice
3. Shape, Distribution & Normality
🎯 The problem: same mean, three different tumors
Three tumors can have the same average UMI count and be completely different data.
3. Shape, Distribution & Normality
🧐 sd ranks the spread, not the shape
sd sorts the three by spread: tumor2 widest, tumor3 tightest.
sd is one number — how far the counts sit from the mean, not where they sit.
tumor3 has a tail to the left, and sd only reports that it is tight.
So what should we do?
3. Shape, Distribution & Normality
The shape is the information
It matters because the most common tests assume this shape. If the data is normally distributed, those tests work. If it is not, those tests can lie.
Note
UMI counts are usually not normally distributed: many low-count spots, a few very high ones. Long tail on the right.
3. Shape, Distribution & Normality
Symmetric — tumor1
Many measurements pile up in the middle and thin out evenly on both sides.
That shape is called normal — the bell curve.
the counts sit evenly on both sides of the middle
mean ≈ median — the two lines land on the same spot
3. Shape, Distribution & Normality
Right-skewed — tumor2
twelve counts climb from 1 to 9, one count sits far above
the high count drags the mean up — the median stays with the pile
mean > median
3. Shape, Distribution & Normality
Left-skewed — tumor3
ten counts pile at the high end, a low tail runs to the left
the low tail drags the mean down — the median stays with the pile
mean < median
3. Shape, Distribution & Normality
Shapiro-Wilk test
One number, one p-value:
p > 0.05 → no evidence against normal. Treat it as a normal distribution.
p ≤ 0.05 → the data is not normal. Do not use normal-distribution tests.
Note
A Q-Q plot is the visual version: points curving away at the end mean a real tail.
3. Shape, Distribution & Normality
A Q-Q plot
Read the shape from the plot before choosing a test.
3. Shape, Distribution & Normality
✅ Summary
sd ranks the spread, not the shape
shape = mean vs median: mean ≈ median symmetric · mean > median right · mean < median left
the common tests assume the normal shape — UMI counts are usually right-skewed
Shapiro-Wilk gives the number, the Q-Q plot the picture — same check
p > 0.05 → treat as normal · p ≤ 0.05 → not normal, use the rank test
4. p-value & Statistical Tests
1. Review from Lecture 8
2. Descriptive Statistics
3. Shape, Distribution & Normality
4. p-value & Statistical Tests
5. Correlation & Regression
6. Statistics Practice
7. From Numbers to Figures
8. The Common Plots
9. Reading a ggplot2 Call
10. Export for Publication
11. Graphics Practice
4. p-value & Statistical Tests
What a p-value is
…the p-value is the probability of obtaining test results at least as extreme as the result actually observed, under the assumption that the null hypothesis is correct.
FB: @Statisticss · IG: @StatisticsForYou — image via Google Images (thumbnail source)
Important
p is not the probability the hypothesis is true.
It answers “what are the chances of getting a result this extreme by pure randomness?”.
4. p-value & Statistical Tests
p from 0.5 down to 0.001
p = 0.5 → this result is common under no effect. Not evidence.
p = 0.1 → 1 in 10 under no effect. Not evidence.
p = 0.05 → the decision line. 5 in 100 under no effect.
p = 0.03 → if the groups were truly the same, this happens about 3 times in 100
p = 0.01 → 1 in 100 under no effect.
p = 0.001 → 1 in 1,000 under no effect.
4. p-value & Statistical Tests
⚠️ The problem: 20,000 genes
A gene with no difference still gets a p-value. p is defined under no effect, so a result this extreme happens 5 times in 100.
Test 20,000 genes at p < 0.05, and 1,000 will pass by chance alone. Not good.
Important
Every transcriptomics analysis tests thousands of features. Without a correction, you are not discovering — you are sorting noise.
4. p-value & Statistical Tests
The new threshold — Why divide by 20,000?
0.05 = 1 in 20 fluke chance for one test
20,000 = number of tests
1 in 20 ÷ 20,000 = 1 in 400,000: the whole analysis keeps 1 in 20
4. p-value & Statistical Tests
Bonferroni — strict
Tip
Simple BUT very hard to pass.
It kills real findings too.
4. p-value & Statistical Tests
Benjamini-Hochberg — the usual choice
Control the false discovery rate (FDR): the share of your “significant” list that is noise.
Important
BH is the standard in transcriptomics.
4. p-value & Statistical Tests
Where do p-values come from?
p is not a number you choose. It is the output of a test.
Two decisions come before the test:
The unit — what varies independently
The test — the question, and the shape of the data
4. p-value & Statistical Tests
⚠️ The problem: 80 spots, 4 patients
The question: is this gene higher in tumor?
The data we have is spots. The claim we want is about patients.
4. p-value & Statistical Tests
Every test in the book
Student t
Welch t
paired t
Wilcoxon
Wilcoxon signed-rank
sign test
bootstrap
permutation
ANOVA
Welch ANOVA
Kruskal-Wallis
Friedman
MANOVA
chi-square
Fisher
G-test
Poisson
negative binomial
Pearson
Spearman
Kendall
linear regression
logistic regression
Levene
Bartlett
Fligner
Kolmogorov-Smirnov
DESeq2
edgeR
limma-voom
…
Note
Every name is a valid test. Most of them do not answer this question, and the question itself does not say which one to run.
4. p-value & Statistical Tests
An example
p-value = 1.772e-07 — far below 0.05, so the result is significant. Means 26.200 vs 20.125, a difference of 6.075.
You reported this gene expressed MORE in tumor cohorts.
4. p-value & Statistical Tests
In reality, the pooled p is a pitfall
Patient 1 and patient 3 sit at the same level: 28.00 and 28.07. Run on the 4 patients, p = 0.831.
Important
Spots are not independent observations. Pooling them makes the sample size huge and the p-value tiny — the number got smaller, the claim did not get stronger.
The test has to be run on the unit the experiment was built on.
4. p-value & Statistical Tests
Normal distribution tests, and their backup
Question
Normal distribution
Any data
2 groups
t-test
Wilcoxon
2 groups, same subject twice
paired t
Wilcoxon signed-rank
3+ groups
ANOVA
Kruskal-Wallis
Parametric = assumes the normal distribution.
Non-parametric = assumes nothing. Uses ranks, not values.
4. p-value & Statistical Tests
The data — check for distribution
p1
p2
p3
p4
condition
control
control
tumor
tumor
distribution
normal
skewed right
normal
skewed right
mean
28.00
17.50
28.07
20.60
median
28
17
28
17
p2 and p4 are right-skewed: their hot spots drag the mean right of the median.
4. p-value & Statistical Tests
2 groups, different conditions — treatment vs control
Normal data → t-test
Any data → Wilcoxon
Both say no difference.
4. p-value & Statistical Tests
A gap is not enough — spread decides
pair
gap
sd
t
p
p4 vs p2
3.1
6.45 · 1.28
1.51
0.164
p2 vs p3
10.6
1.28 · 1.55
−28.77
3.205881e-35
Important
A statistic is a signal-to-noise judgement. More spots shrink the noise, so a smaller gap can become convincing.
4. p-value & Statistical Tests
2 groups, same subject — pre vs post
Same patient, biopsied before and after treatment: 10 spots.
The same data answers two questions. Paired: p = 0.0002025. Unpaired: p = 0.5095. Pairing removes the spot-to-spot spread, so the difference carries more weight.
4. p-value & Statistical Tests
3+ groups — ANOVA
All three are normal, so ANOVA is fair: p < 2e-16.
4. p-value & Statistical Tests
3+ groups — ANOVA: result and pairwise
Patients 1 and 3 express the gene at the same level (28.0 and 28.07); patient 5 expresses it lower (20.0).
Patient 5 is below patients 1 and 3 (p < 2e-16 each); patients 1 and 3 do not differ (p = 0.9).
4. p-value & Statistical Tests
3+ groups — Kruskal-Wallis
Patient 2 and patient 4 are skewed, so Kruskal-Wallis takes all four: p = 7.477e-12.
4. p-value & Statistical Tests
3+ groups — Kruskal-Wallis: Which pair differs
Insignificant pairs:
p1 vs p3
p2 vs p4
Significant pairs:
p2 vs p1
p2 vs p3
p4 vs p1
p4 vs p3
4. p-value & Statistical Tests
A category cannot be averaged
Every test so far takes one number per spot.
Some questions ask how many spots sit in a category.
4. p-value & Statistical Tests
Counting data — the question
The answer is a table of counts, not a mean.
Expected count = row total × column total ÷ total spots.
4. p-value & Statistical Tests
Chi-square — big counts
Expected counts stay above 5, so chi-square is valid: p = 0.0078.
4. p-value & Statistical Tests
Fisher — small counts
Null — immune share equal in control and tumor. Expected counts — the table that null predicts. p — chance of a table this far off by randomness: 3 in 10,000.
Any expected count below 5, so Fisher: p = 2.91263e-04.
4. p-value & Statistical Tests
The decision flow
Tip
Write the test name in the figure legend. A reader can then check whether the claim is supported.
4. p-value & Statistical Tests
⚡ Q1: p2 is right-skewed, p3 is normal. You want to compare them. Which test?
A. t-test
B. Wilcoxon
4. p-value & Statistical Tests
✅ B — p2 fails the normal check, so the rank test
A the t-test assumes the normal distribution — p2’s p = 1.924505e-09 says it is not normal
B Wilcoxon uses ranks and assumes nothing: p = 4.502014e-12 — the gap 17.5 vs 28.07 is real
Check the shape before choosing the test.
4. p-value & Statistical Tests
⚡ Q2: Both 5-spot groups pass Shapiro-Wilk (p = 0.97 and 0.81). Which test?
A. t-test
B. Wilcoxon
4. p-value & Statistical Tests
✅ A — p > 0.05 is no evidence against normal, so the t-test is fair
A both groups pass: p = 2.275325e-06 — the gap 28 vs 16.4 is real
B the rank test is not needed here, and 5 vs 5 cannot go below p = 0.0079
Five spots cannot prove normal — p > 0.05 means “not rejected”. Say n = 5 in the figure.
4. p-value & Statistical Tests
⚡ Q3: 30 spots — immune 2 / other 18 in control, immune 8 / other 2 in tumor. Which test?
A. chi-square
B. Fisher
4. p-value & Statistical Tests
✅ B — the smallest expected count is 3.33, below 5
A R warns Chi-squared approximation may be incorrect — the expected counts 6.67, 13.33, 3.33, 6.67 include a value below 5
B Fisher counts the tables exactly: p = 2.91263e-04
Read the expected counts, not the observed counts, to choose.
4. p-value & Statistical Tests
⚡ Q4: t.test(p4, p2) gives p = 0.164. What does that say?
A.p4 and p2 are the same
B. no evidence of a difference
4. p-value & Statistical Tests
✅ B — p is the chance of a gap this big by randomness, not proof of equality
gap 3.1 counts
sd 6.45 in p4, 1.28 in p2
n = 10 vs 30
p = 0.164
A p > 0.05 does not show the groups are equal — it shows the data is not evidence
B a gap of 3.1 against a spread of 6.45 carries little weight; report the gap, the sd, and the test name
Say no evidence, never “no difference”.
4. p-value & Statistical Tests
✅ Summary
p = how surprising the data is, assuming no effect — p > 0.05 = not evidence
20,000 genes → always correct — Bonferroni is strict, BH controls the share of noise
the unit is what varies independently — count patients, not spots
2 groups → t / Wilcoxon · paired → paired t / signed-rank · 3+ groups → ANOVA / Kruskal · counting → chi-square (expected ≥ 5) / Fisher (expected < 5)
spread decides as much as the gap — name the test in the legend
5. Correlation & Regression
1. Review from Lecture 8
2. Descriptive Statistics
3. Shape, Distribution & Normality
4. p-value & Statistical Tests
5. Correlation & Regression
6. Statistics Practice
7. From Numbers to Figures
8. The Common Plots
9. Reading a ggplot2 Call
10. Export for Publication
11. Graphics Practice
5. Correlation & Regression
⚠️ The problem: two numbers per spot
Every test so far compares groups — control vs tumor, pre vs post, category vs category.
Some questions have no groups: two measurements on the same spot, and the question is whether they move together.
5. Correlation & Regression
Pearson — a straight line
r = 0.9994, p = 4.799893e-10 — gene_b tracks gene_a almost perfectly.
Pearson: straight-line relationship
5. Correlation & Regression
Pearson visualization
5. Correlation & Regression
Spearman — the rank order
rho = 1, p = 4.960317e-05 — every spot keeps its order.
Spearman: monotone — “as one goes up, so does the other”
5. Correlation & Regression
Spearman visualization
5. Correlation & Regression
The caveat: One hot spot breaks Pearson
Pearson: r = 0.658, p = 0.076 — not significant. Spearman: rho = 1, p = 4.960317e-05 — unchanged.
Pearson uses the values — one extreme count pulls the line
Spearman uses the ranks — the spot is still the highest, so the ordering is untouched
5. Correlation & Regression
Correlation is not causation
tumor spots and normal spots can correlate in gene expression without one causing the other.
Both may be driven by a third thing: hypoxia, immune infiltration, tumor content.
Open lecture-09_statistics_practice.ipynb, set Runtime → Change runtime type → R
Run the cells in order: Runtime → Run All
The notebook takes the deck’s tools to the real matrix: descriptive stats, shape check, per-gene tests, BH correction, correlation and regression.
6. Statistics Practice
counts → QC → statistics → figure
The goal is not the p-value. It is explaining your data to another person — and a figure does that faster than any table.
Important
The figure is the deliverable — that is what sections 7-12 are for.
7. From Numbers to Figures
1. Review from Lecture 8
2. Descriptive Statistics
3. Shape, Distribution & Normality
4. p-value & Statistical Tests
5. Correlation & Regression
6. Statistics Practice
7. From Numbers to Figures
8. The Common Plots
9. Reading a ggplot2 Call
10. Export for Publication
11. Graphics Practice
7. From Numbers to Figures
🎬 Previously on statistics…
Center & spread: median resists outliers, mean absorbs them; report mean ± sd, keep var for models.
Check shape first: shapiro.test() & Q-Q plot decide the test — normal \(\rightarrow\) parametric, skewed \(\rightarrow\) rank test.
Test biological units: compare patients, not pooled spots (no pseudoreplication) — \(t\) / ANOVA vs Wilcoxon / Kruskal.
Multiple testing: 20,000 genes give \(\sim 1,000\) fluke hits at \(p < 0.05 \rightarrow\) control FDR with Benjamini-Hochberg (BH).
Associate & model: Pearson \(r\) (linear) vs Spearman \(\rho\) (ranks); lm() gives the slope and fit (\(R^2\)).
Tip
Statistics tells us something moved — figures show what moved and in which direction.
7. From Numbers to Figures
🧾 We’ve done the statistics… What now?
gene
Tumor
Control
log2FC
sd
p
MKI67
482.5
3.0
7.33
25.1
7.8e-5
CDKN2A
190.0
2.0
6.57
10.7
9.4e-5
EPCAM
323.8
6.0
5.75
15.5
6.1e-5
KRT19
209.0
4.0
5.71
9.6
5.0e-5
CDH1
151.0
5.0
4.92
8.3
8.7e-5
VIM
262.0
8.0
5.03
11.1
4.2e-5
PTPRC
6.0
214.5
−5.16
0.8
3.6e-5
CD3E
2.6
96.8
−5.22
0.5
2.7e-5
CD8A
1.9
70.0
−5.24
0.3
6.0e-5
GAPDH
997.8
1010.8
−0.02
27.6
1.0
Important
Raw data is meaningless, and statistics on raw data is still raw data.
7. From Numbers to Figures
📊 Draw it — 10 genes, 2 groups, one glance
up in tumor — MKI67, CDKN2A, EPCAM, KRT19, CDH1, VIM: 30–54×
down in tumor — PTPRC, CD3E, CD8A: 36–38× lower — immune cells leave
flat — GAPDH, the housekeeping gene (p = 1.0)
Important
Statistics says something moved. The figure says what moved, and in which direction — a pattern no p-value column can hold. That is why we draw. The how starts here.
7. From Numbers to Figures
🖼️ Every figure is code-generated
The figure you just saw is a file on disk.
7. From Numbers to Figures
⚙️ Five perks of code-generated figures
repeatable — rerun the script, the same figure.
editable — change the data or one aes(), the figure redraws
exact — size, dpi, colours are numbers in code, not a mouse drag
batchable — one loop over 20 genes, one style for all 20
trackable — code is text, so git sees every change
7. From Numbers to Figures
🧰 Tools you’ll need
ggplot2: draws figures from a data frame — dots, lines, bars, tiles
dplyr: picks, filters, and summarises columns
tidyr: reshapes between wide and long
ggridges: ridgeline plots
RColorBrewer: colour palettes
… depending on what you plan to do…
7. From Numbers to Figures
📖 Ask yourself 3 questions before you draw.
what data? — the first argument
what is drawn? — the geometry: dots, lines, bars, boxes, tiles
what maps where? — which column becomes x, y, colour, size
Tip
You do not have to remember the syntax to read it. Every call states its own data, geometry, and mapping, in that order.
8. The Common Plots
1. Review from Lecture 8
2. Descriptive Statistics
3. Shape, Distribution & Normality
4. p-value & Statistical Tests
5. Correlation & Regression
6. Statistics Practice
7. From Numbers to Figures
8. The Common Plots
9. Reading a ggplot2 Call
10. Export for Publication
11. Graphics Practice
8. The Common Plots
A. General plots
scatter, line — relationship and trend
bar, box, histogram, density — one column of numbers, and groups
dot plot, stacked bars, Venn, UMAP/tSNE, correlation matrix — composition, overlap, and pairs of measures
Note
One slide per plot type: the figure, the question it answers, and the numbers it is built from.
8. The Common Plots
🧾 Base graphics: the four shapes you meet first
scatter — does y move with x?
line — how does y change across x?
boxplot — centre, spread, outliers, per group
histogram — how one column is spread
8. The Common Plots
⚪ Scatter and line: relationship and trend
plot(1:5, c(2, 4, 6, 8, 10), type ="b")
scatter → relationship: one dot per pair, does y move with x
line → trend: the same dots joined in order, the shape is the trend
join only what is ordered: spot 1 → 5, time 1 → 12
8. The Common Plots
📊 Bar plots: one value per category
bar → one value per category: the height is the value — say which (mean, median, count, sum)
The bar hides the spread behind it
Note
Bar plots show one number per group. Ask what the number is: mean, median, count, sum. The caption has to say, and the axis label usually does not.
8. The Common Plots
📊 Bars with error bars: the bar, plus the spread
bar + error bar → one value per category, with its spread: bar is the centre, arms are the spread (sd, SE, or CI — say which)
Overlapping arms mean the gap is not clean
Note
An error bar is the smallest honest fix for a hidden spread. What it is — sd, standard error, confidence interval — has to be in the caption.
8. The Common Plots
📊 Box plots: centre, spread, and outliers per group
box → centre, spread, outliers: one box per group, all on one axis
The box keeps the spread visible, per group
Note
Read the axis before the shape. A boxplot of UMI and a boxplot of p-values look the same and mean nothing alike.
8. The Common Plots
📊 Histograms: how one column is spread
histogram → spread of one column: binned — the bars are counts, not values
The shape shows where the mass sits
Note
A histogram has one axis that is not the data: the y-axis is the bin count. Change breaks and the shape changes — the data did not.
8. The Common Plots
📈 Density and ridgeline: the shape, without bins
density / ridgeline → shape without bins: smoothed curve of one column; stacked when per group
The curve shows where the mass sits, not just the centre
Note
A histogram depends on breaks, a density on the bandwidth. Both are choices about the picture, not properties of the data.
8. The Common Plots
⚪ Dot plot: every spot, one dot
dot plot → every value, one dot: one dot per observation, no bins
Works for tens of points; at thousands the dots blur into a curve
Note
A dot plot is a histogram that refuses to bin. It works for tens of points; at thousands the pile becomes a curve.
8. The Common Plots
📊 Stacked bars: composition inside one bar
stacked bar → composition inside one bar: one bar split by group; 100% stacked shows shares
Read both how tall the bar is and how the inside is split
Note
Stacked bars show composition. Read two things: how tall the bar is, and how the inside is split. A tall bar and a large share are different claims.
8. The Common Plots
🧇 Venn diagram: how much two sets share
Venn → how much two or three sets share: each region is a count of genes
Two sets are readable; three are the limit
Note
A Venn answers “how much is shared”. Two sets are readable; three are the limit, and four sets stop being a picture of the counts.
8. The Common Plots
🧬 UMAP / tSNE: cells flattened to a plane
UMAP / tSNE → cells flattened to a plane: one dot per cell; position is learned similarity, colour is the label
Far apart means different expression profiles — not far apart in the tissue
Note
UMAP and tSNE are dimension reduction: neighbourhood, not geography. A spot map keeps the real x and y; a UMAP does not.
8. The Common Plots
🧮 Correlation matrix: r for every pair
correlation matrix → r for every pair: one cell per pair of genes, value is r
Read sign and size together; the diagonal is 1.00 by definition
Note
Read sign and size together. The colours are a palette choice; the numbers are the claim, so read the matrix with the values in it.
8. The Common Plots
B. Spatial & DE plots
spot map — real x, y in tissue, colour = expression
violin — the whole distribution, per sample
volcano — one dot per gene: effect size against significance
heatmap — gene × sample as coloured cells
Note
The same shapes, on data that carries a gene name, a sample name, or a position in the tissue.
Wickham, H. ggplot2 4.0.3 — ggplot, aes, geom_*, scale_*, facet_wrap, coord_equal, ggsave; with dplyr, tidyr (pivot_longer), ggridges, RColorBrewer
every figure and number in this deck is drawn in the course’s R_tutorial env (R 4.5.3, ggplot2 4.0.3, ggridges 0.5.7) on png() / pdf() devices, fixed seed
Course data: GSE129221 — Song et al. (2019) Cancer Cell Int 19:117
p-value meme (section 4) — FB: @Statisticss · IG: @StatisticsForYou; image via Google Images: encrypted-tbn0.gstatic.com