Lesson 2 of 9
Module 3 · Figures that tell the truth
A distribution is the shape of the data: where values cluster, how spread they are, whether there are two peaks or outliers. Four common views show different things.
Before the code: what a distribution is
A distribution describes how often each value turns up in a set of measurements. Two groups can have the same average and still look very different: one may be tightly bunched, the other spread wide, and a third may have two separate clusters. Each of the four views in this lesson summarises that shape in a different way, so each shows some features well and hides others.
A few words describe the shape of any distribution. The centre is where values typically lie, summarised by the mean or the median. The spread is how far they range around that centre, summarised by the standard deviation or the interquartile range. A distribution is symmetric when both sides of the centre look alike, and skewed when one side has a long tail. A peak, or mode, is a value around which many observations cluster; a distribution with two peaks is called bimodal. Finally, outliers are single values far from the rest. A good distribution plot lets the reader see all five at once.
In Describing data you summarised these features with numbers, and saw how five read counts of 10, 12, 15, 18 and 945 have a mean of 200 but a median of 15. The numbers told you the data were skewed; a plot shows it at a glance, and also shows things that no single number captures, such as two peaks or a gap. That is why it is worth plotting a distribution before you summarise or test it. In Comparing two groups you met tests, such as the t-test, that assume roughly normal data, meaning a single, symmetric, bell-shaped peak; a quick look at each group's distribution is the simplest check of that assumption, and it often finds data-entry errors as well.
On real data the shapes are rarely textbook ones. Raw RNA-seq read counts are strongly right-skewed: most genes have modest counts and a few have enormous ones, so a histogram on the raw scale is one tall bar at the left with a long, almost empty tail. Measurements from a mixture of cell types, or of patients with and without a mutation, can be bimodal. Body measurements such as birth weight are often close to symmetric. Knowing which shape to expect helps you spot when a plot looks wrong.
The data are the tooth growth measurements from Choosing the right chart: 60 guinea pigs, 10 in each of six groups, with dose stored as a factor (a categorical variable with fixed levels, here 0.5, 1 and 2). Because dose is a factor, ggplot2 treats the x-axis as three separate groups rather than as a continuous number line.
Choosing the right chart read toothgrowth.csv, turned supp and dose into factors and loaded ggplot2 and dplyr. The code below assumes that tg already exists. If you start a fresh R session, run that lesson's first code block again before this one, or R will report that object tg is not found.
Keep the sample size in mind throughout. With 10 animals per group, each group's shape can only be judged roughly: one unusual animal changes a histogram bar or a bump in a density curve noticeably. The views that summarise shape, such as histograms, densities and violins, work best with dozens or hundreds of values per group. For 10, the individual points carry the most honest information, and the summaries act as a guide on top of them.
# the violin, box and points in one panel ggplot(tg, aes(x = dose, y = len)) + geom_violin(fill = "grey90", colour = NA) + geom_boxplot(width = 0.15, outlier.shape = NA) + geom_point(position = position_jitter(width = 0.08, seed = 1), alpha = 0.6) + labs(x = "Vitamin C dose (mg/day)", y = "Tooth length") + theme_classic(base_size = 12) # other views: geom_histogram(bins = 12), geom_density(), geom_boxplot()
What this does, line by line
ggplot(tg, aes(x = dose, y = len))starts a plot from the data frametg. The aesthetic mapping,aes(), says which column goes where: dose on the x-axis, tooth length on the y-axis. Every layer added with+inherits this mapping.geom_violin(fill = "grey90", colour = NA)draws a violin for each dose: a smoothed outline of where the values lie, mirrored left and right. It is wide where many values sit and narrow where few do. The fill is a pale grey andcolour = NAremoves the outline, so the violin stays in the background.geom_boxplot(width = 0.15, outlier.shape = NA)draws a narrow box plot inside each violin.outlier.shape = NAstops it drawing outliers as separate dots, because the next layer draws every point anyway.geom_point(position = position_jitter(width = 0.08, seed = 1), alpha = 0.6)adds one dot per animal. Jitter nudges each dot sideways by a small random amount, up to 0.08 of a group's width either way, so dots with similar values do not hide behind each other. It moves points only sideways here, never up or down, so the lengths stay exact.seed = 1fixes the random nudges, so the plot looks identical every time you run it.alpha = 0.6makes the dots partly transparent, so overlaps show up as darker spots.labs()sets the axis titles, with units on the x-axis.theme_classic(base_size = 12)gives a plain white background with axis lines and no grid, and sets the base text size to 12 points.- The last line is a comment, so R does not run it. It names the functions for the other three views:
geom_histogram(bins = 12)for a histogram with 12 bins,geom_density()for a smoothed density curve, andgeom_boxplot()for box plots alone.
The order of the layers matters. ggplot2 draws them in the order you add them, so the violin sits at the back, the box in the middle and the points on top, where nothing covers them.
Reading the four views
All four panels tell the same main story: tooth length rises with dose. They differ in what else you can see.
- A histogram cuts the range of values into equal intervals, called bins, and draws a bar for how many values fall in each. With three overlapping groups the bars hide each other, which is why it is the hardest view to read here.
- A density curve is a smoothed histogram: instead of counting values in bins, it spreads each value into a small bump and adds the bumps up. A violin is a density curve turned on its side and mirrored.
- The box plot reduces each group to five numbers, which the worked example below explains.
- The points alone hide nothing. With 10 animals per group you can count them.
Why bin width changes a histogram
The number of bins is a choice you make, and it changes what the reader sees. Take the same 60 tooth lengths. With 3 wide bins, almost everything falls into one or two bars and any gap between groups disappears. With 60 narrow bins, most bars hold one value or none, and the histogram becomes a jagged comb of random spikes. Wide bins merge real features; narrow bins invent noise. A sensible choice lies in between, and it is worth trying two or three settings before you settle on one. ggplot2 uses 30 bins by default and prints a message asking you to pick a better value, which is why the code comment sets bins = 12.
You can also set the width of each bin directly with binwidth, in the units of the data: geom_histogram(binwidth = 2) makes each bar cover 2 units of tooth length. That is often easier to explain in a legend than a number of bins. A second pitfall concerns groups. When you fill a histogram by group, for example ggplot(tg, aes(x = len, fill = dose)) + geom_histogram(bins = 12), ggplot2 stacks the groups on top of each other by default, so the bars of the upper groups start at the top of the lower ones and their heights are hard to read. Add position = "identity", alpha = 0.5 to overlap them instead, or give each group its own panel with facet_wrap(~ dose), which is often clearest.
| View | Shows well | Hides |
|---|---|---|
| Histogram | Shape, peaks and gaps for large samples | Depends on the bin width; overlapping groups are hard to read |
| Box plot | Median, middle half (IQR), outliers; compact | Two peaks, and how many points there are |
| Violin | The full shape, smoothed | Sample size; can invent smooth shapes from few points |
| Points (strip or beeswarm) | Everything, for small samples | Becomes cluttered above a few hundred points |
How to read this table
Read across each row: the middle column is what the view is good for, the right column what it can quietly conceal. Choose by sample size first. With a handful of values per group, show the points. With hundreds or thousands, a histogram, density or violin summarises the shape better than a crowd of dots.
Two terms are new. A strip plot lines the points up in a column for each group, usually with jitter. A beeswarm plot places them side by side without overlap, so the width of the column itself shows where values are dense. The median is the middle value when the data are sorted, and the IQR (interquartile range) is the spread of the middle half of the data.
The phrase "can invent smooth shapes" deserves a note. A violin drawn from 5 values still looks like a confident, smooth outline, even though 5 values cannot reveal a shape. A reader sees a curve, not the 5 points behind it. That is why a violin should not be used alone for small groups.
Sample size also explains the order of the rows. A box plot needs at least a handful of values to make its five numbers meaningful; a histogram or violin needs more to show a believable shape; points work at any size until they start to overlap. Above a few hundred points per group, you can still show them faintly, with a small alpha, or switch to a histogram or density and mention the sample size in the legend.
Worked example · reading a box plot
whiskers = the furthest points within 1.5 × IQR of the box
points beyond the whiskers = possible outliers, shown individually
Step by step. A percentile is the value below which a given share of the data falls: a quarter of the values lie below the 25th percentile, and three quarters below the 75th. Suppose a group's 25th percentile is 10 and its 75th percentile is 16.
- The IQR is 16 − 10 = 6, so the box runs from 10 to 16.
- 1.5 × IQR = 1.5 × 6 = 9. The whiskers may reach as far as 10 − 9 = 1 below the box and 16 + 9 = 25 above it.
- Each whisker stops at the furthest real data point inside that limit. If the largest value under 25 is 22, the upper whisker ends at 22.
- A value of 27 lies beyond 25, so it is drawn as a separate dot: a possible outlier.
What the answer means. "Possible outlier" means unusual for this group, not wrong. It may be a real animal that grew more, or a measuring error; the plot cannot tell you which. Check the original record before you decide anything about it.
Say in the legend what the box and whiskers mean: conventions differ between programs.
Go deeper: the bandwidth, a density plot's hidden bin width
A density curve has the same kind of hidden choice as a histogram. Each value is spread into a small bump, and the width of those bumps, called the bandwidth, decides how smooth the curve is. Narrow bumps give a wiggly curve that follows every point; wide bumps blur real peaks into one hill. By default ggplot2 picks the bandwidth with a rule of thumb (Silverman's rule, bw = "nrd0"), which works best for single-peaked data and tends to over-smooth two peaks.
You can change it with adjust: geom_density(adjust = 0.5) halves the default bandwidth and adjust = 2 doubles it. geom_violin() accepts the same argument, since a violin is a mirrored density. As with bins, try two or three settings; if a second peak appears at only one setting, be cautious about reporting it. Densities can also spill past real limits, so a curve may extend below a length of zero.
The box plot was introduced by John Tukey in Exploratory Data Analysis (Addison-Wesley, 1977), and its variants, including the 1.5 × IQR whisker rule that ggplot2 uses, are described in McGill, Tukey & Larsen 1978, "Variations of box plots" (The American Statistician). The violin plot comes from Hintze & Nelson 1998, also in The American Statistician. ggplot2 calculates the quartiles with R's default quantile() rule, so other programs may draw slightly different boxes. Remember that an outlier in a box plot is a display rule, not a statistical test: it flags a point for checking, never a reason on its own to remove it.
Key point
Layer the views: a box or violin for the summary, and the points on top for honesty. Hide box-plot outliers (outlier.shape = NA) when you also draw all points, so none appear twice.
A second example of the same idea: imagine a gene measured in 40 tumour samples, where half the tumours carry an amplification of that gene and half do not. The expression values form two clusters, one high and one low. A box plot alone shows one wide box with the median in the empty gap between the clusters, a value that almost no tumour actually has. Adding the points makes the two groups obvious at a glance.
The usual mistakes
- Showing a summary without the data. A box plot of 4 values draws a confident box from almost nothing. Below about 30 values per group, always add the points.
- Drawing outliers twice. If you add
geom_point()on top of a box plot withoutoutlier.shape = NA, each outlier appears as two dots, one from each layer, and a reader may count it twice. - Leaving the jitter unseeded. Without a fixed seed, the dots move a little each time you redraw the figure, which is confusing when a colleague compares two versions.
- Not saying what the box means. Some programs draw whiskers to the minimum and maximum, others to fixed percentiles. One sentence in the legend removes the doubt.
Further reading · Module 3Weissgerber et al. 2015: why bar charts hide the data, with examples. · All reading for this course
Check your understanding
1. What can a box plot hide?
2. Why does a histogram's look depend on the bins?
3. You have 5 values per group. Which view is best?
Up next
Lesson 3
Comparisons and uncertainty