Lesson 3 of 9
Module 3 · Figures that tell the truth
An error bar can show three different things, and readers cannot tell which unless you say so. This lesson computes all three, then replaces the bar chart with something more honest.
Three summaries of spread
Before any plotting, this lesson computes the three numbers that error bars most often show. They answer different questions, so it helps to define them first.
- The standard deviation (SD) measures how far individual values typically sit from their group's mean. It describes the animals.
- The standard error (SE) of the mean measures how much the mean itself would move if you repeated the experiment with new animals. It is the SD divided by the square root of the sample size, so it shrinks as you add animals.
- A 95% confidence interval (CI) is a range built around the mean so that, if the experiment were repeated many times, about 95% of the ranges built this way would contain the true mean.
You met the SD in Describing data and confidence intervals in Comparing two groups; this lesson puts them side by side as error bars. The key distinction is between describing and inferring. The SD describes the spread of the individuals you measured, and it stays roughly the same however many animals you add, because the animals themselves do not become more alike. The SE and the CI describe how well you know the group's mean, and they shrink as the sample grows. So choose the bar by the question: "how variable are these animals?" calls for the SD, while "how well do we know the average, and could two averages differ?" calls for the CI.
Choosing the right chart read toothgrowth.csv into tg, set up the okabe colours and loaded ggplot2 and dplyr. The code below assumes they already exist. If you start a fresh R session, run that lesson's first code block again before this one.
tg |> filter(dose == "0.5") |> group_by(supp) |> summarise(n = n(), mean = mean(len), sd = sd(len), .groups = "drop") |> mutate(se = sd / sqrt(n), ci95 = se * qt(0.975, n - 1)) |> mutate(across(mean:ci95, \(x) round(x, 2)))
What this does, line by line
tg |>passes the tooth growth data into the next function. The pipe,|>, means "take this, then do the following to it".filter(dose == "0.5")keeps only the 20 animals on the lowest dose. The 0.5 is in quotes becausedoseis a factor, whose levels are labels.group_by(supp)splits those 20 rows by vitamin C source, so the next step works on each source separately.summarise(n = n(), mean = mean(len), sd = sd(len), .groups = "drop")reduces each group to one row: the number of animals, the mean length and the SD..groups = "drop"removes the grouping afterwards, so later steps work on the whole table.mutate(se = sd / sqrt(n), ...)adds the SE.qt(0.975, n - 1)looks up the value from the t distribution that cuts off the top 2.5%, with n − 1 = 9 degrees of freedom. Multiplying the SE by it gives the half-width of the 95% CI,ci95.- The last line rounds every column from
meantoci95to two decimal places.\(x) round(x, 2)is a short way to write a small function ofx, andacross()applies it to each of those columns.
Reading the output
The first line says the result is a tibble, the tidyverse version of a data frame, with 2 rows and 6 columns. The second row of the header gives each column's type: <fct> is a factor, <int> a whole number and <dbl> a decimal number. Each group has 10 animals. The orange juice mean prints as 13.2 rather than 13.23 because tibbles show about three significant digits by default; the stored value is 13.23, which the worked example uses.
Notice that the orange juice group has both a higher mean and a larger SD: its animals vary more. Every other column follows from that SD and from n = 10.
The SE depends on n through its square root. With 40 animals instead of 10, and the same SD, the SE would be half as large, because √40 is twice √10. Quadrupling the sample therefore halves the SE, while the SD would be expected to stay about the same. This is why a small SE bar can come from a large but very variable sample, and why an error bar means little unless the legend gives n.
Worked example · three error bars from the same data
± SD 4.46 → how much animals vary
± SE 1.41 = 4.46 ÷ √10 → how precisely the mean is known
± 95% CI 3.19 = 1.41 × 2.262 (t value for 9 degrees of freedom) → a range likely to contain the true mean
Step by step. The square root of 10 is 3.162. Dividing the SD by it: 4.46 ÷ 3.162 = 1.41, the SE. The t value for 9 degrees of freedom is 2.262, so the CI half-width is 1.41 × 2.262 = 3.19. The interval runs from 13.23 − 3.19 = 10.04 to 13.23 + 3.19 = 16.42.
What the answer means. The same group gives a bar of ± 4.46, ± 1.41 or ± 3.19, depending on which measure you pick. A reader who is not told which one cannot judge the result. The t value is larger than the familiar 1.96 because a sample of 10 estimates the SD imperfectly; as samples grow, the t value approaches 1.96.
SE bars are the shortest, which is why they are popular, and the least useful on their own. Show the points and a 95% confidence interval, and say so in the legend.
Cumming, Fidler & Vaux 2007, "Error bars in experimental biology" (Journal of Cell Biology), is the standard short guide to reading SD, SE and confidence-interval bars, and asks that every figure state which is shown and the value of n. Weissgerber et al. 2015 (PLOS Biology), this module's further reading, documents how often bar charts hide the data in published papers. Note that their rules of thumb assume independent groups. For paired or repeated measurements, such as the same animals measured twice, error bars on each group say nothing about the within-subject differences that the test actually uses.
Replacing the bar chart
The next block draws the same 20 animals twice, side by side: once as the common bar chart with SE bars, and once as every point with the mean and its 95% CI. It uses okabe, the two-colour vector defined in Choosing the right chart, which gives ascorbic acid blue and orange juice orange.
library(patchwork) d05 <- filter(tg, dose == "0.5") summ <- d05 |> group_by(supp) |> summarise(n = n(), mean = mean(len), se = sd(len) / sqrt(n), ci95 = se * qt(0.975, n - 1), .groups = "drop") p1 <- ggplot(summ, aes(supp, mean, fill = supp)) + geom_col(width = 0.6) + geom_errorbar(aes(ymin = mean - se, ymax = mean + se), width = 0.2) + scale_fill_manual(values = okabe, guide = "none") + labs(title = "Bar of means with SE bars", x = NULL, y = "Tooth length (dose 0.5)") + theme_classic(base_size = 11) p2 <- ggplot(d05, aes(supp, len, colour = supp)) + geom_point(position = position_jitter(width = 0.08, seed = 1), alpha = 0.7) + geom_pointrange(data = summ, aes(y = mean, ymin = mean - ci95, ymax = mean + ci95), colour = "black") + scale_colour_manual(values = okabe, guide = "none") + labs(title = "Every point, mean and 95% CI", x = NULL, y = NULL) + theme_classic(base_size = 11) p1 + p2
What this does
library(patchwork)loads the package that lets you place ggplot objects next to each other with+.d05holds the 20 animals at dose 0.5, andsummholds one row per source with n, mean, SE and CI half-width, computed exactly as in the first block (without rounding).p1is the bar chart.geom_col()draws a bar whose height is the value in the data, here the mean.geom_errorbar()adds a bar from mean − SE to mean + SE.scale_fill_manual(values = okabe, guide = "none")colours the bars and hides the legend, since the x-axis already names the groups.p2uses the raw data,d05, so each animal is a dot.geom_pointrange()takes its data fromsumminstead, through thedata =argument, and draws a black point at the mean with a vertical line from mean − CI to mean + CI.x = NULLremoves an axis title that would only repeat the group names, andy = NULLon the right panel avoids printing the same y title twice.p1 + p2places the two plots side by side as one figure.
Compare the two panels. The bar chart suggests two tidy groups, each a solid block from zero to its mean. The right panel shows what the bars concealed: 10 animals per group, a much wider spread for orange juice, and where each individual lies. A reader of the right panel can judge the overlap between the groups for themselves.
The bar chart also invites a wrong reading of its scale. A bar drawn from zero asks the eye to compare lengths, as if a mean of 13 were "about one and a half times" a mean of 8. For a count that comparison is the point; for a measured length, the question is usually how far apart the groups are and how much they overlap, which bars from zero do not show. The points-and-interval panel puts the two groups on the same scale without that suggestion, and its y-axis can start near the data rather than at zero. This style of figure, a bar of the mean topped with a short error bar, is sometimes called a dynamite plot because of its shape, and many journals now discourage it for continuous data.
Whichever version you draw, the legend should say three things: what the points or bars show, what the error bars show (SD, SE or 95% CI), and how many values are in each group. For the right panel that could read "Points: individual animals (n = 10 per group). Black point and line: mean and 95% confidence interval."
A common mistake is to judge significance by whether error bars overlap. Overlap of SE bars, CI bars or SD bars each means something different, and none of them replaces a formal test. Show the data and the interval, and report the test separately.
The interval also gives the reader something a p-value cannot: a sense of size. An interval from 10.04 to 16.42 says the true mean for orange juice at this dose is plausibly anywhere in that range, which is a span of more than 6 units. If the next study needed to know the mean to within 1 unit, this experiment would clearly be too small. A figure that shows the interval lets readers make that kind of judgement for themselves.
Go deeper: two intervals that just miss, and p = 0.006
At 0.5 mg the two 95% intervals are 7.98 ± 1.96 (6.02 to 9.94) and 13.23 ± 3.19 (10.04 to 16.42). They miss each other by a hair, which might suggest a borderline result. Yet t.test(len ~ supp, data = d05) gives t = 3.17 and p = 0.006, quite convincing.
The reason is how uncertainties combine. The standard error of a difference is not the sum of the two SEs but the square root of the sum of their squares: √(0.87² + 1.41²) = 1.66, smaller than 0.87 + 1.41 = 2.28. So the interval for the difference, about 5.25 ± 3.5, is narrower than the two half-widths added together (1.96 + 3.19 = 5.15). Two 95% intervals can therefore overlap a little and the difference still be significant. For independent groups like these, intervals that do not overlap do mean p < 0.05; overlap on its own tells you little.
Two measures: a scatter plot with a fitted line
When both variables are measurements, such as a mother's weight and her baby's birth weight in the birth weight data, each person becomes one point on a scatter plot. A fitted line from linear regression summarises the trend: how much the y value changes, on average, for each unit of x.
You fitted exactly this kind of line in Correlation and linear regression, where lwt is the mother's weight in pounds and bwt the baby's weight in grams. In a figure, the line and the points do different jobs. The line answers "what is the average trend?", while the points answer "how well does that trend describe individual babies?". A fitted line drawn without its points hides the second answer, and it is the more common mistake of the two.
In ggplot2 this is geom_point() plus geom_smooth(method = "lm"); you will build it in Multi-panel figures. The shaded band is the 95% confidence interval for the fitted line, not for individual babies.
This is the same distinction as SE versus SD, in a new setting. The band describes uncertainty about the average trend, so it narrows as the sample grows. The scatter of individual babies around the line does not narrow with more data, because real babies differ. A band that looks tight does not mean a mother's weight predicts her own baby's weight closely. The band is also wider at the ends, because there are fewer points there to pin the line down.
Three more pitfalls are worth knowing. First, do not extend the line beyond the range of the data: a straight-line trend that holds across the mothers in the study says nothing reliable about weights lighter or heavier than any of them, and geom_smooth() rightly stops the line at the last point. Second, with hundreds of points, many overlap, so dense regions look no darker than sparse ones; a smaller point size or some transparency, such as geom_point(alpha = 0.5), makes the density visible. Third, a clear trend is not evidence of cause. Heavier mothers may differ from lighter ones in other ways, such as diet or smoking, which is why you added such factors to the model in Module 2.
Finally, give both axes their units. "Mother's weight (lb)" and "Birth weight (g)" let a reader turn the slope into a sentence, such as "each extra pound of maternal weight goes with a few grams more birth weight, on average", without looking anything up.
Key point
Bar charts are for counts and proportions, where the bar's length from zero means something. For continuous measurements, show the points.
A second example: the number of patients in each of three trial arms who had a side effect is a count, so a bar from zero is honest, and the bar's length is the answer. Blood pressure in those arms is a continuous measurement; a bar from zero to the mean blood pressure would mostly show the empty range from zero up to the lowest reading, where no patient lies, and would hide the spread that matters.
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 error bar describes how much individuals vary?
2. SD is 6 and n is 9. What is the SE?
3. When is a bar chart the right choice?
Up next
Lesson 4
Colour that works for everyone