LSMA

LONDON SCHOOL OF MULTIOMICS & AI

Free course

Free course

0 of 25 lessons

Lesson 1 of 9

Module 3 · Figures that tell the truth

Choosing the right chart

Choosing the right chart

Choosing the right chart

A figure answers one question. Decide the question first, and the chart usually chooses itself.

⏱ 12 min readModule 3 · Figures that tell the truthFree
Lesson at a glance4 steps · about 12 min
  1. Start from the question
  2. How to read this table
  3. The tooth growth data
  4. What this does, line by line

Start from the question

Module 2, Statistics, gave you the tools to describe data, compare groups, analyse counts and proportions, fit regression lines, handle many tests at once and keep the whole analysis reproducible, ending with the low birth weight project. Every one of those results reaches a reader through a figure, and a figure can support an honest result or quietly undermine it. This module, Figures that tell the truth, is about choosing, drawing and checking figures so that they show what the analysis found: no more, and no less.

Every figure is an answer to a question, whether or not its maker wrote the question down. "Did the drug change tumour size?" needs a comparison of groups. "Does expression of this gene rise with age?" needs a relationship between two measurements. Choosing the chart before the question usually produces a figure that is attractive but answers nothing in particular. The table below pairs the common questions with charts that answer them well, and with the usual poor choices.

A useful habit is to write the question down as a comment above your plotting code, for example # Does tooth length differ between supplements at each dose?. It costs one line, and it gives you a test for the finished figure: can a reader answer that question from the figure alone, without your help? If the honest answer is "only after reading the methods", the chart type or its layout needs to change. Writing the question also shows when you have two questions at once, such as a comparison between groups and a trend over dose. Two questions often need two panels, or one panel designed carefully so that both answers can be read.

How to read this table

Find the row whose first column matches what you want the reader to learn. The middle column lists good options, roughly from simplest to most specialised; the last column names a common choice that hides or distorts the answer. A few terms: a density plot is a smoothed outline of a histogram; a confidence interval is the range of values compatible with the data, met in Module 2; PCA, principal component analysis, compresses thousands of genes into a few summary axes so that samples with similar profiles appear close together.

The same dataset can lead to different rows, depending on what you ask. In the birth weight data from Module 2, "do babies of mothers who smoke weigh less?" is a comparison of groups, so it calls for the individual babies as points in two groups, with each group's mean and its confidence interval. "Does a mother's weight relate to her baby's weight?" is a relationship between two measurements, so it calls for a scatter plot, which you drew in Correlation and linear regression. The data have not changed; only the question has, and the chart follows the question.

Treat the last column as a warning, not a ban. Each "avoid" entry hides something the reader needs for that particular question. A stacked bar of the parts of a whole is fine with three or four parts, but with twelve the middle segments float at different heights and are hard to compare, and a table may then be clearer than any chart.

The questionGood choicesAvoid
How are values spread?Histogram, density, box with points, violinA single bar of the mean
How do groups compare?Points with mean and confidence intervalBars with an error bar, for continuous data
How do two measures relate?Scatter plot, with a fitted line if usefulTwo lines on two different axes
How does something change over time?Line plot, with points for few timesPie charts at each time
What are the parts of a whole?Stacked bar, or simply a table3D pie charts
Patterns across many genes and samples?Clustered heatmap, PCADozens of separate bar charts

Why avoid the choices in the last column? A bar of the mean shows one number and hides how many values there were, how they spread and whether any are unusual. Two lines on two different y-axes can be made to look related or unrelated by stretching either axis. Pie charts ask readers to compare angles and areas, which people judge far less accurately than lengths or positions, and 3D effects distort them further by making nearer slices look bigger. The figure below shows the first problem directly.

These problems share a cause: each chart turns the numbers into a visual quantity, such as a length, an angle or an area, that the eye judges poorly or that the chart's maker can stretch at will. A good chart puts the comparison the reader must make onto a common scale, so that the answer is read off by position. That is why points along one shared axis appear in so many rows of the table.

Choosing the right chart · LSMA free course Left: two bars of equal height show that groups A and B have the same mean of 10. Right: the same eight values per group as points; group A clusters around 10, while group B splits into two clumps near 5 and 15, with no values near the mean. 1 · BARS OF THE MEAN 2 · EVERY POINT 01020 01020 Group AGroup B Group AGroup B Bars: mean ± standard error. Points: each value, with a line at the mean. mean
Both groups have a mean of exactly 10 (eight made-up values each), so the bars are the same height and only their error bars differ. Only the points reveal that group B is really two clusters, with nothing near its mean. Illustrative data.

The claim that people judge position and length more accurately than angle or area rests on experiments by Cleveland & McGill, "Graphical perception" (Journal of the American Statistical Association, 1984), which ranked these judgements and remains the standard reference for choosing chart types. The ToothGrowth data come from Crampton 1947, a study of odontoblast growth in guinea pigs as a measure of vitamin C intake, published in the Journal of Nutrition; R's help page, ?ToothGrowth, gives the details. Cleveland and McGill tested simple judgement tasks, so treat their ranking as a strong guide, not as proof that every pie chart misleads.

The tooth growth data

Sixty guinea pigs received vitamin C at 0.5, 1 or 2 mg a day, as orange juice or as pure ascorbic acid, and the length of their tooth-forming cells was measured. Ten animals per group: few enough to show every one.

This is R's classic ToothGrowth dataset, saved here as a CSV file. The measured cells are odontoblasts, the cells that build dentine, the main tissue of a tooth, so their length reflects how well teeth grow. Guinea pigs, like humans, cannot make their own vitamin C, which is why they suit this question. Two factors vary at once: the supplement (orange juice, OJ, or ascorbic acid, VC) and the dose. Six groups of 10 make 60 animals.

The design is balanced: every combination of supplement and dose has the same number of animals. This matters for the figure as well as for the statistics. With equal groups, no group looks more or less certain just because it has more points, and a reader can compare the clouds of points directly. With 10 animals per group, every value fits on the plot without crowding, so there is no reason to hide any of them behind a summary. In your own data, check the group sizes before you plot, as the code below does with n(); very unequal groups are worth stating in the legend.

Files for this lessonSave each file in your project folder under the name shown here. Your browser may give the download a different name; rename it before you run the code.
toothgrowth.csvTooth growth in 60 guinea pigs given vitamin C
R
library(ggplot2)
library(dplyr)

tg <- read.csv("toothgrowth.csv") |>
  mutate(supp = factor(supp, levels = c("VC", "OJ"), labels = c("Ascorbic acid", "Orange juice")),
         dose = factor(dose))
okabe <- c("Ascorbic acid" = "#0072B2", "Orange juice" = "#E69F00")

tg |> group_by(supp, dose) |> summarise(n = n(), mean = round(mean(len), 1), .groups = "drop")

ggplot(tg, aes(x = dose, y = len, colour = supp)) +
  geom_point(position = position_jitterdodge(jitter.width = 0.15, dodge.width = 0.6, seed = 1), alpha = 0.7) +
  stat_summary(fun = mean, geom = "crossbar", width = 0.4, linewidth = 0.4,
               position = position_dodge(width = 0.6)) +
  scale_colour_manual(values = okabe) +
  labs(x = "Vitamin C dose (mg/day)", y = "Tooth length", colour = NULL) +
  theme_classic(base_size = 12)

What this does, line by line

  1. read.csv("toothgrowth.csv") reads the data. In the mutate(), supp becomes a factor with ascorbic acid first and readable labels, and dose becomes a factor, so the three doses are treated as three categories spaced evenly along the x-axis rather than as numbers on a scale.
  2. okabe <- c("Ascorbic acid" = "#0072B2", "Orange juice" = "#E69F00") stores two colours, a blue and an orange from the Okabe–Ito palette, a set of colours chosen to stay distinguishable for readers with the common forms of colour blindness. Each colour is named after its group.
  3. The group_by(supp, dose) |> summarise(…) line counts the animals and calculates the mean tooth length, to one decimal place, for each of the six combinations.
  4. ggplot(tg, aes(x = dose, y = len, colour = supp)) puts dose on the x-axis, length on the y-axis and colours points by supplement.
  5. geom_point(position = position_jitterdodge(…), alpha = 0.7) draws every animal as a point. Dodging places the two supplements side by side at each dose (dodge.width = 0.6); jittering adds a small random sideways shift (jitter.width = 0.15) so animals with similar values do not hide behind each other. seed = 1 makes that random shift the same every time.
  6. stat_summary(fun = mean, geom = "crossbar", …) calculates each group's mean and draws it as a short horizontal bar. position_dodge(width = 0.6) uses the same dodge width as the points, so each bar sits on its own group.
  7. scale_colour_manual(values = okabe) applies the named colours, labs() sets the axis titles and removes the legend title, and theme_classic(base_size = 12) gives plain axes with no grid.
# A tibble: 6 x 4 supp dose n mean <fct> <fct> <int> <dbl> 1 Ascorbic acid 0.5 10 8 2 Ascorbic acid 1 10 16.8 3 Ascorbic acid 2 10 26.1 4 Orange juice 0.5 10 13.2 5 Orange juice 1 10 22.7 6 Orange juice 2 10 26.1

Reading the output. The table has one row per supplement and dose. Every n is 10, confirming the balanced design. The means rise with dose for both supplements: 8.0, 16.8 and 26.1 for ascorbic acid, and 13.2, 22.7 and 26.1 for orange juice. The printed 8 is 8.0; R drops trailing zeros when it prints.

This table is worth printing even though the figure will show the same means. It is a quick check that the data were read correctly and the groups are what you expect, and it gives you exact numbers to quote in the text. A figure is for patterns, and a table is for precise values; the two work best together.

Strip plot of tooth length by vitamin C dose, every animal shown, with a short line at each group's mean. Length rises with dose; orange juice is higher than ascorbic acid at 0.5 and 1 mg, and equal at 2 mg.
The chart the code draws. This image was made with the Python version of the same code; ggplot2 gives the same layout in its own style.

Look at what the points add to the table of means. Within each group the animals spread over several units, so the groups overlap: some animals on 0.5 mg of orange juice grew longer teeth than some on 1 mg of ascorbic acid. At 2 mg the two clouds of points overlap almost completely, although the ascorbic acid group spreads more widely. None of this is visible in six bars of the mean, yet it is exactly what a reader needs to judge how convincing the differences are.

Three design choices in the code support that reading. The mean is drawn as a short crossbar rather than a tall bar from zero, so it marks a position without covering the points. The colours separate the supplements, but the dodge also puts them side by side, so the groups can be told apart even in a greyscale printout. The y-axis title names the measurement, and the x-axis title gives the unit of dose, so the figure makes sense on its own. A common pitfall is to add jitter without fixing the seed, so the points move slightly every time the figure is redrawn; seed = 1 prevents that.

Worked example · what the figure says in one sentence

tooth length rises with dose for both sources
at 0.5 and 1 mg orange juice is ahead by about 5–6 units (13.2 vs 8.0; 22.7 vs 16.8)
at 2 mg the two are the same (26.1 and 26.1)

If you cannot say what a figure shows in one sentence, it is probably trying to answer more than one question.

Step by step. At 0.5 mg, orange juice minus ascorbic acid is 13.2 − 8.0 = 5.2. At 1 mg it is 22.7 − 16.8 = 5.9. At 2 mg it is 26.1 − 26.1 = 0. So the gap is about 5 to 6 units at the two lower doses and vanishes at the highest.

What the answer means. The effect of the supplement depends on the dose. When the effect of one factor changes with the level of another, the two factors are said to interact. One plausible reading is that at 2 mg the animals receive enough vitamin C either way, so the source no longer matters. A single average "orange juice effect" across all doses would hide that pattern, which is one more reason to show every group.

Go deeper: dose as a category or a number

Turning dose into a factor spaces 0.5, 1 and 2 mg evenly along the x-axis. That happens to be honest here, because each dose is double the one before, and on a log scale doubling steps are equal steps. With doses of 1, 2 and 10 mg, a factor axis would still space them evenly and hide that the second step is a fivefold increase while the first is only a doubling.

To keep the true spacing, map a numeric copy of dose and choose the scale on purpose: ggplot(tg, aes(x = as.numeric(as.character(dose)), y = len)) + geom_point() + scale_x_log10(). The as.character() step matters: as.numeric() applied directly to a factor returns the level positions 1, 2 and 3, not the doses. A linear axis would put 0.5 and 1 close together and 2 further away; either choice is fine if the axis title says which you used.

Key point

With fewer than about 30 points per group, show every point. Readers can then judge the spread, the overlap and any outliers for themselves.

With many more points, for example thousands of cells per group in a single-cell experiment, individual points merge into a solid blob. Then a violin, box plot or histogram, as in Figures with ggplot2, shows the distribution better, and a light layer of points can still be added if the reader needs to see outliers.

Further reading · Module 3Weissgerber et al. 2015: why bar charts hide the data, with examples. · All reading for this course

Check your understanding

1. Which chart shows how two continuous measures relate?

2. Ten mice per group. What should the figure show?

3. Why does okabe name each colour by group?

Up next

Lesson 2

Showing distributions

Ask BioTutor

Create a free website with Framer, the website builder loved by startups, designers and agencies.