10  Exercises and a figure checklist

The code cells in this chapter are not run automatically. Fill in the blanks (___) and press “Run Code”. Work in the tab of whichever language you prefer, and if you have time, make the same figure in the other language too. Sample answers are collapsed below each exercise.

10.1 Figure checklist

# What to check Chapter
1 Do the axis labels have a quantity and unit (“Body mass (g)”)? 1
2 Is colour a meaningful mapping? Is it distinguishable for diverse colour vision and in black-and-white print? Is the same group the same colour in every figure? 2
3 Are points overplotted (transparency, cells, contours)? 2
4 Did you deliberately decide the order and names of categories? 3
5 Are you hiding the distribution (box plot only, bar chart + error bars only → show the points)? 4
6 Did you state whether the error bars are SD, SE or CI? 4
7 For significance symbols, did you state the test, the correction and what the symbols mean? Were the pairs to compare decided in advance? 5
8 For regression lines, did you check whether the direction changes when groups are separated? Did you state whether the band is a confidence interval or a prediction interval? 6
9 Did you make it at the actual size (mm) and text size (pt)? Did you save in a vector format? 7
10 Do the multiple panels have A, B, C labels, with aligned axes and legends? 8
11 When narrowing the axis range, are you discarding data (coord_cartesian())? 9
12 Did you state the excluded data (missing, out of range) and the sample sizes in the figure’s description? 1, 9

10.2 Exercise 1: diabetes and systolic blood pressure (by sex)

Using NHANES, make a figure that compares systolic blood pressure (BPSysAve) by diabetes status, separately for each sex.

  • Overlay points on box plots (show only 150 points per group)
  • Within each sex, compare with vs without diabetes using Welch’s t-test, and draw brackets and asterisks
  • R: off <- 0.75 / 4 (the dodge width of 0.75 is split between 2 groups, so ±0.1875 from the centre)
  • Python: off = 0.2 (seaborn’s default width of 0.8 is split between 2 groups, so ±0.2)

For both sexes, people with diabetes have higher systolic blood pressure, with very small p-values (****). However, people with diabetes are older, so this difference includes confounding by age (confounding hands-on). In the figure’s description, also state the size of each group and that the comparison is not adjusted for age.

10.3 Exercise 2: age and cholesterol, regression equations by sex

Using NHANES, draw regression lines and equations by sex on a scatter plot of age and total cholesterol (TotChol, mmol/L). Place the equations at the top left of the panel, one line per sex, in the same colour as the lines.

  • R: x = -Inf, y = Inf (the top-left corner of the panel)
  • Python: transform=ax.transAxes (panel coordinates)

R² is small for both, so a straight line explains little. Looking at the scatter plot, the relationship is curved, especially in women: cholesterol rises until about age 60 and then falls. Try setting geom_smooth()’s method to "loess" or using formula = y ~ splines::ns(x, 3) to check the straight-line assumption.

10.4 Exercise 3: a two-panel figure for a paper

Using the penguin data, make the following two-panel figure at 170 mm wide, 70 mm high, with 8 pt text.

  • A: scatter plot of flipper length and body mass (coloured by species)
  • B: box plot + points of body mass by species
  • Merge the legends into one. Add A and B labels
  • R: guides = "collect", tag_levels = "A", base_size = 8 (fig-width 6.7 inches ≈ 170 mm, fig-height 2.76 inches ≈ 70 mm)
  • Python: "font.size": 8, figsize=(170 * mm, 70 * mm)

To save, use ggsave("fig.pdf", width = 170, height = 70, units = "mm", device = cairo_pdf) in R and fig.savefig("fig.pdf") (with pdf.fonttype = 42) in Python. patchwork’s & applies the same settings (colours, theme) to all panels (+ applies only to the last panel).

10.5 Closing remarks

ggplot2 and matplotlib / seaborn are written differently, but both decide the same things:

  • which visual properties to map the data to (position, colour, shape, size)
  • what to compute before drawing (counting, summarising, fitting a line)
  • which coordinates determine positions (data, panel, whole figure)
  • at what size and in what format to output

If you keep these four in mind, you will be able to make “the figure you want” and “a figure that can’t be misread” in either language.