Summarise without hiding the observations

Day 13 — Groups, plots and interpretation

Data Analysis in Life Sciences

About this lecture

A thesis draft shows a bar chart with two bars: mean growth of control and treated seedlings. The examiner asks: how many plants are behind each bar? How much did they vary? Is one bar pulled up by a single unusual plant? The chart cannot answer, because the plants have disappeared into two numbers. How do we summarise groups and still let the reader see the plants?

Today you will:

  • make a summary table with one row per group, including how many plants contributed;
  • plot individual plants with a mean marker, and define any whiskers you draw;
  • use a histogram to see a distribution, and notice how much the bin width shapes it;
  • save the table and the figure as files, with a caption a reader can check.

This builds on Day 12’s mean and SD; the table and figure go straight into the descriptive report on Day 15.

The problem: means alone hide the plants

  • Left: only the two group means, 15.28 and 21.49 mm.
  • Right: the same means with the 24 plants behind them.
  • Now you can see that the groups overlap, that one treated plant grew only 12.2 mm, and that two treated plants grew about 30 mm.
  • You can also count the plants: 12 per group.

Left panel shows two group means alone; right panel shows the same means together with the individual plant growth values, which overlap between groups.

A summary is a new, smaller table

  • seedlings: 24 rows, one per plant.
  • group_summary: 2 rows, one per treatment.
  • The two summary rows are not two new plants; they describe groups.
  • Keep both tables. The plant table feeds the individual points; the summary feeds the mean markers.

Twenty-four plant measurements on the left are compressed into two group summary values on the right.

One row per group with group_by() and summarise()

> group_summary <- seedlings |>
+   group_by(treatment) |>
+   summarise(n_plants = n(),
+             n_observed = sum(!is.na(growth_mm)),
+             mean_mm = mean(growth_mm, na.rm = TRUE),
+             median_mm = median(growth_mm, na.rm = TRUE),
+             sd_mm = sd(growth_mm, na.rm = TRUE),
+             .groups = "drop")
> group_summary
# A tibble: 2 × 6
  treatment n_plants n_observed mean_mm median_mm sd_mm
  <chr>        <int>      <int>   <dbl>     <dbl> <dbl>
1 control         12         12    15.3      15.7  3.19
2 treated         12         12    21.5      20.8  4.87
  • group_by(treatment): do the next step separately for each treatment; summarise() returns one row per group. Names left of = become columns.
  • n() counts rows (plant records); sum(!is.na(growth_mm)) counts usable values (! means “not”). .groups = "drop" gives an ordinary table.
  • The tibble rounds what it prints (15.3); the table keeps full precision (15.275).

Always report how many plants contributed

The same summary on Day 11’s repaired file:

> cleaned <- read_csv("output/seedlings-cleaned.csv",
+                     show_col_types = FALSE)
> cleaned |>
+   group_by(treatment) |>
+   summarise(n_plants = n(),
+             n_observed = sum(!is.na(growth_mm)),
+             mean_mm = mean(growth_mm, na.rm = TRUE),
+             .groups = "drop")
# A tibble: 2 × 4
  treatment n_plants n_observed mean_mm
  <chr>        <int>      <int>   <dbl>
1 control         12         11    15.2
2 treated         12         12    21.5
  • Control: 12 plants, 11 growths (P06 unknown), so its mean is 15.21 mm, not 15.28 mm.
  • A reader needs the n next to every mean.

In the repaired file both groups keep twelve plant records, but one control plant has no usable growth value, leaving eleven.

Plot the plants, then add the means

group_plot <- ggplot(seedlings,
    aes(x = treatment, y = growth_mm)) +
  geom_point(position = position_jitter(
    width = 0.08, height = 0, seed = 13),
    alpha = 0.75) +
  geom_point(data = group_summary,
    aes(x = treatment, y = mean_mm),
    inherit.aes = FALSE, shape = 18,
    size = 4, colour = "darkred") +
  labs(x = "Treatment",
       y = "Change in seedling length (mm)") +
  theme_minimal()
group_plot
  • First layer: plants from seedlings; second layer: means from group_summary.
  • height = 0: dots move sideways only, so each stays at its measured growth.

The plot made by this code: one grey dot per plant, slightly spread sideways, and a red diamond at each group mean, 15.28 millimetres for control and 21.49 for treated.

If you draw whiskers, say what they are

  • These whiskers show mean ± SD: how much plants vary around the mean.
  • Another figure’s whiskers may show a confidence interval (Day 17): how uncertain the mean is. They can look identical.
  • A caption that says only “error bars” leaves the reader guessing.
  • Fine choice for today: show the points and the means, no whiskers.

Diamonds with whiskers show mean plus or minus sample standard deviation beside the individual plant measurements.

A histogram shows the shape of a distribution

ggplot(seedlings, aes(x = growth_mm)) +
  geom_histogram(binwidth = 4,
    boundary = 0, colour = "white") +
  facet_wrap(~ treatment, ncol = 1) +
  labs(x = "Change in seedling length (mm)",
       y = "Number of plants") +
  theme_minimal()
  • Growth is cut into 4 mm bins (intervals); bar height = number of plants in each.
  • facet_wrap() draws one panel per group on a shared axis.
  • Most controls grew 12–20 mm, most treated 20–24 mm; two treated plants form their own bar near 30 mm.

The histogram made by this code: control and treated panels on one growth axis with 4-millimetre bins; the treated bars sit further right.

The bin width shapes the picture

  • The same 12 control values with bins of 2, 4 and 6 mm.
  • With 2 mm bins there appear to be gaps and peaks; with 6 mm the shape looks smooth.
  • Nothing about the plants changed: only a display choice.
  • With small samples, describe only features that survive several bin widths, and state the bin width you used.

The same twelve control growth values drawn as histograms with bin widths of 2, 4 and 6 millimetres look noticeably different.

Write a caption the reader can check

Weak: “Treatment makes everything larger; error is shown.”

Better: “Seven-day growth (mm) of individually potted seedlings. Each dot is one plant (n = 9 per group); diamonds are group means. No error bars are shown.”

A caption names:

  1. the unit (one plant) and outcome with units;
  2. what each symbol means;
  3. the n, and what any whiskers represent.

Point plot of the transfer study: dots are individual plants and diamonds are group means, nine plants per group.

Save the table and the figure as files

> write_csv(group_summary, "output/growth-summary.csv")
> ggsave("output/growth-summary.png", plot = group_plot,
+        width = 6, height = 4)
> list.files("output")
[1] "growth-summary.csv"    "growth-summary.png"
[3] "seedlings-cleaned.csv"
> read_csv("output/growth-summary.csv",
+          show_col_types = FALSE)
# A tibble: 2 × 6
  treatment n_plants n_observed mean_mm median_mm sd_mm
  <chr>        <dbl>      <dbl>   <dbl>     <dbl> <dbl>
1 control         12         12    15.3      15.7  3.19
2 treated         12         12    21.5      20.8  4.87
  • write_csv() and ggsave() print nothing; list.files() shows the two new files.
  • Reading the CSV back shows the numbers are in the file. The PNG holds the picture.
  • Reader test: close RStudio, open both files from the file manager. Can you find one plant, a mean, the units and the n?

Summary

Concept Key fact
Summary table group_by() + summarise(): one row per group; 2 rows are not 2 new plants
Counts n() counts records, sum(!is.na()) counts usable values; report n with every mean
Seedlings Control 15.28 mm (SD 3.19), treated 21.49 mm (SD 4.87), 12 each
Points + means Plant table for dots, summary table for diamonds; jitter sideways only
Whiskers Mean ± SD and a confidence interval look alike: always say which
Histogram Counts per bin; with small n the bin width changes the apparent shape
Caption Unit, outcome and units, symbols, n, meaning of any whiskers
Outputs CSV for numbers, PNG for figures, both regenerated by the script

Next (Day 14, replicates and uncertainty): what counts as an independent plant, and how much a mean would change if we repeated the sampling.

Now: open the Day 13 exercises. Leave with day13-groups.R, output/growth-summary.csv, output/growth-summary.png and a caption.