library(readr)
library(dplyr)
library(ggplot2)
seedlings <- read_csv("data/seedlings.csv", show_col_types = FALSE) |>
mutate(growth_mm = final_mm - initial_mm)Day 13 — Summarising groups without hiding the plants
Lecture slides · Quiz · Weekly revision
Today’s question
A bar chart of two group means cannot tell a reader how many plants are behind each bar, how much they varied, or whether one unusual plant pulled a mean up. Today you make a small table with one row per group and a figure that shows every plant together with its group mean, so a reader gets both.
Start day13-groups.R in the project root:
Save today: day13-groups.R, output/growth-summary.csv and output/growth-summary.png.
One row per group
Use group_by() and summarise() to find out the number of plants, mean, median and SD of growth in each treatment group:
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_summarygroup_by(treatment) makes the next step work separately within each treatment, and summarise() returns one row per group. The names left of = become the column names. n() counts rows, that is plant records, while sum(!is.na(growth_mm)) counts the growth values that are actually there (! means “not”). .groups = "drop" returns an ordinary table.
| treatment | n_plants | n_observed | mean_mm | median_mm | sd_mm |
|---|---|---|---|---|---|
| control | 12 | 12 | 15.28 | 15.65 | 3.19 |
| treated | 12 | 12 | 21.49 | 20.85 | 4.87 |
group_summary has two rows that describe groups; seedlings still has the 24 plants. You need both: the plants for the figure, the summary for the means.
Plants and means in one figure
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)",
caption = "Small dots: individual plants. Red diamonds: group means.") +
theme_minimal()
group_plotThe first geom_point() draws one dot per plant from seedlings. position_jitter(width = 0.08, height = 0) moves dots a little sideways so they do not hide each other, but never up or down, so every dot stays at its measured growth. The second geom_point() takes its data from group_summary and draws the means as red diamonds; inherit.aes = FALSE tells it to use its own x and y instead of looking for growth_mm, which the summary table does not have.
The figure shows what the table cannot: the groups overlap, one treated plant grew only 12.2 mm, and two treated plants grew about 30 mm. If you add whiskers, say what they show. Mean ± SD describes how plants vary; a confidence interval, which comes in week 4, describes how uncertain a mean is. Points and means without whiskers are a good choice for today.
Histograms
A histogram cuts the growth axis into intervals, called bins, and counts the plants in each. Use one to find out the shape of each group’s growth values:
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(~ treatment, ncol = 1) draws one panel per group, stacked on the same growth axis so the panels can be compared. With 4 mm bins, most control plants fall between 12 and 20 mm, and most treated plants between 20 and 24 mm, with two treated plants in the 28–32 mm bin. With only 12 plants per group the shape changes noticeably when you change the bin width.
Saving the table and the figure
write_csv(group_summary, "output/growth-summary.csv")
ggsave("output/growth-summary.png", plot = group_plot, width = 6, height = 4)The CSV holds the numbers and the PNG holds the picture; until you save them, both exist only in R’s memory. Find the two files in your file manager and open them outside RStudio.
Group summaries for the seedlings
- Use
filter()andmean()to find out whether the control mean ingroup_summarymatches the plants it came from. You should get 15.28 mm. - Use the same summary code on
output/seedlings-cleaned.csvfrom day 11 (addgrowth_mmif it is not there) to find out how the missing P06 shows up. Control hasn_plants12 butn_observed11, and a mean of 15.21 mm instead of 15.28 mm, because a different set of plants contributed. This is why the table keeps both counts. - Change the histogram’s
binwidthto 2 and then to 6 and find out which features stay the same. At every width the treated values sit higher than the controls and the two treated plants near 30 mm stand apart; smaller peaks and gaps appear or vanish with the bin width, so do not describe them. - Write a caption for
output/growth-summary.pngthat a reader could check without the code: the unit (one plant), the outcome and its unit, what dots and diamonds mean, and n. For example: “Seven-day growth (mm) of 24 individually potted seedlings, 12 per group. Dots are plants; red diamonds are group means.”
write_csv(group_summary, "output/growth-summary.csv") saves the summary, while ggsave("output/growth-summary.png", plot = group_plot, width = 6, height = 4) saves the plot. If a layer complains about a column it cannot find, use names() on the table that layer uses.
Another lab: percentage yield
Your teacher may choose this instead of task 2 above. In data/organic-recovery.csv each row is one independent synthesis run. Calculate yield_percent = 100 * isolated_mass_g / theoretical_mass_g, as on day 12, then use group_by(protocol) and summarise() to find out the number of runs, mean yield and SD for each protocol. Both protocols have six runs; the standard mean is 68.67% (SD 4.32 percentage points) and the modified mean 77.00% (SD 3.74). Plot the runs with a mean marker as above, with the y-axis labelled “Isolated product yield (%)”, and save output/organic-yield-summary.csv and output/organic-yield-summary.png.
A second class’s data
Make the same table and figure for data/seedlings-transfer.csv (18 plants, nine per group) in a new script, day13-transfer.R, with its own library() lines, import and growth calculation.
- Use
group_by()andsummarise()to maketransfer_summarywith n, mean, median and SD, and save it asoutput/day13-transfer-summary.csv. Usenrow()to find out how many rowstransferandtransfer_summaryhave: 18 plants and 2 groups. You should get means of 13.66 mm (control) and 17.87 mm (treated). - Draw the plants with a mean marker from
transfer_summary, with a caption that says n = 9 per group, and save it asoutput/day13-transfer-points.png. Does any control plant grow more than a treated plant? Yes: three control plants (15.9, 16.3 and 17.4 mm) grew more than the smallest treated plant (15.0 mm). - Restart R and rerun the script to find out whether it rebuilds both files on its own.
Check your understanding
The dots show how many plants there are, how much they vary, where unusual values lie and how much the groups overlap. A higher mean does not mean every treated plant grew more than every control plant.
No. Each row describes a group of 12 plants. The 24 plants stay in seedlings, and that is the table any later analysis uses.
Keep: day13-groups.R, output/growth-summary.csv, output/growth-summary.png and your caption. Next: replicates and uncertainty.