Day 13 — Groups, plots and interpretation
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:
This builds on Day 12’s mean and SD; the table and figure go straight into the descriptive report on Day 15.
seedlings: 24 rows, one per plant.group_summary: 2 rows, one per treatment.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 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
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_plotseedlings; second layer: means from group_summary.height = 0: dots move sideways only, so each stays at its measured growth.
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()facet_wrap() draws one panel per group on a shared axis.
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:
> 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.| 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.