Reproducible figures

Part 2: A project that lasts

Introduction to agentic AI

About this lecture

A figure made by clicking in a spreadsheet cannot be remade: not on new data, not in the journal’s style, not by the next student. A figure made by a script can. Writing the script used to need a programmer. Now you describe the figure, the agent writes the script, and you check the figure.

  • Script, not clicks. The script reads the data and writes the figure, the same way every time.
  • Describe the figure: what is plotted, n per group, units, size in mm, file formats.
  • Check it against the data: one mean by hand, n per group, labels and units.
  • Rerun on new data in one sentence, and check again: new data brings new surprises.
  • Pin the versions (renv, requirements.txt) and commit script, figure and versions together.

This is a hands-on unit. We use R and ggplot2, with a Python variant. Publishing the figure on a web page comes in A course or project page.

Figure 2 was made by clicking, and nobody can make it again

A script is a recipe: the same data gives the same figure, every time

The agent writes the script; you decide what the figure shows and check it

You The agent
say what the figure must show, and for whom reads the data and proposes a plot
give the details: units, n, size, formats, style writes the script and runs it
check the figure against the data installs R or packages when needed (asks first)
decide what is a correct and honest figure explains any line you ask about
commit script, figure and versions records the package versions

You do not need to read ggplot2 code. You need to read the figure.

Our example: a two-factor experiment in a tidy CSV file

Tidy data, one row per value, makes plotting simple

R or Python? Both are fine; choose what your colleagues use

R with ggplot2 Python with matplotlib
Common in biology, medicine, ecology, statistics, social science physics, engineering, image analysis, machine learning
Strength grouped data and statistics plots in few lines full control; same language as much analysis code
Version pinning renv (a lock file) requirements.txt (or similar)
In this unit worked example variant (lake temperatures)

The agent writes both well. The deciding question: who will run this script after you?

If R or a package is missing, the agent installs it with your approval

The first prompt describes the figure, not the code

plant-biomass.csv holds dry biomass (mg) of 48 seedlings: fertiliser
(none, low, high) x watering (well-watered, drought), 8 plants each.
Write an R script with ggplot2 that makes a figure for a journal:

- x axis: fertiliser, in the order none, low, high; colour: watering
- show every plant as a point, plus the mean and standard deviation
- show n per group on the figure
- y axis "Dry biomass (mg)", starting at zero
- colours that work for colour-blind readers
- 85 mm wide, text about 7-8 pt; save as PDF, SVG and PNG (300 dpi)
- leave out plants with no biomass, and print which ones and why
- also write a small table with n, mean and SD per group

Run it and tell me what you did and any choices you made.

A publication figure has a short list of must-haves

A bar with an error bar can hide very different data

The worked figure: every plant, mean ± SD, and n per group

Check one group by hand against the data

Check labels, units and what the error bars mean

  • Units. Is it milligrams, as in the data? An agent may “helpfully” convert to grams, or label mg data as g.
  • Error bars. SD, standard error or confidence interval? They differ by a factor of about 3 for n = 8. Ask which, and put it in the caption.
  • Order and colours. none, low, high, not alphabetical. The same colour means the same thing in every figure.
  • Axis range. Starting at zero for amounts; nothing cut off at the top.
  • Size. Open the PDF at 100 %: is the text readable at 85 mm?
  • Ask the agent to describe the figure in words. Compare its description with what you see.

Changing the style is one sentence, not an afternoon

Change the style: grey bars for the means with standard error bars, the
points on top in black, one panel per watering treatment, 180 mm wide.
Keep everything else.

A house style keeps every figure in a paper consistent

Journals ask for sizes in millimetres, not pixels

Vector formats stay sharp; raster formats are pixels

Format Use it for
PDF journal submission, LaTeX, printing
SVG editing in Inkscape or Illustrator, web pages
PNG (300 dpi) slides, Word documents, quick sharing

New data arrives: rerun the script instead of redoing the figure

A rerun can go quietly wrong: “High” is not “high”

Ask the script to refuse data it does not expect

Make the script stricter, so that a rerun on new data cannot go wrong
quietly:
- stop with a clear message if fertiliser or watering has a value other
  than the ones we know
- print the number of rows read, plotted and left out, and n per group
- do not change any values in the data file; if a fix is needed, tell me

Then fix the data on purpose and record it: “In batch 2, ‘High’ means ‘high’. Handle that in the script, say so in a comment, and rerun.”

Your figure also depends on the software under the script

renv and requirements.txt record the versions*

* With Rust, no environment or lock file of installed packages is needed to run it: a Rust program compiles to one self-contained file. See Translating code to Rust.

Commit the script, the figure and the versions together

The Python variant follows the same steps

lake-temperature.csv holds daily water temperature (°C) at 1, 5 and 15 m
in 2024. With Python and matplotlib: one line per depth, months on the x
axis, 180 mm wide; PDF, SVG, PNG. Leave gaps in the data as gaps.

Your session will look different from this lecture

  • The same prompt gives another script, other colours, another jitter. That is normal.
  • What must match: n per group, means, units, labels, what was left out.
  • If your figure and the reference differ in a number, find out which one is right: yours, the reference, or the data.
  • Scripts sometimes fail on the first run. The agent reads the error and fixes it. Check the result anyway.

Where this goes wrong

  • Silently dropped rows. Missing values, odd labels or a filter: fewer points than you think. Count.
  • The wrong summary. SE labelled as SD, a median called a mean, a log scale nobody mentions.
  • Unit mix-ups. mg labelled g, °F as °C, a conversion the agent added on its own.
  • Invented smoothness. Lines drawn across gaps; a trend line nobody asked for.
  • Raw data edited to make the script run. Never: fix it in the script and say so.
  • A figure that only works at screen size. Check at the final width.
  • Unpinned versions. Works today, fails or differs in two years.
  • Figure not committed with its script. Then nobody knows which script made it.

A checklist for every figure

Summary

Concept Key fact
Script, not clicks the script turns data into the figure, the same way every time
Describe the figure axes, order, points + summary, n, units, size in mm, formats
Check against the data rows in = plotted + left out; n per group; one mean by hand
Error bars say whether SD, SE or CI; they differ a lot
Sizes and formats single column ~85-90 mm; PDF/SVG (vector) and PNG at 300 dpi
House style one style file used by every figure script
Rerun on new data one sentence; then redo the checks; let the script refuse surprises
Pinned versions renv.lock for R, requirements.txt for Python
Commit script, figure, summary table and versions in one commit

Next (GitHub): the project’s history goes online, so colleagues can see, run and review your scripts and figures.