ETC5513: Collaborative and Reproducible Practices
Tutorial 4: Figures, Tables, and Cross-Referencing
🎯 Objectives
Building on our ability to sync repositories and manage branches, today we focus on the technical side of professional reporting:
- Create a Quarto report with a table, figures, and sections that are labelled and referenced
- Generate professional figures with captions and sub-figures
- Use cross-referencing to link text to your figures and tables dynamically
- (Extension) Implement academic referencing using BibTeX in Quarto
We will be using the repository we made in this week’s workshop. Open this project in your IDE of choice and create a new .qmd file for this tutorial. Make sure to add it to version control.
🖼️ Task 1: Working with Figures
In Quarto, figures are more than just images; they are numbered entities with metadata. To make a figure “referencable,” its label must start with the prefix fig-.
Exercise
- Create a new code chunk in your QMD.
- Use the following code to generate a plot:
```{r}
#| label: fig-penguins-scatter
#| fig-cap: "Body mass vs. Flipper length for three penguin species."
#| warning: false
library(ggplot2)
library(palmerpenguins)
ggplot(penguins, aes(x = flipper_length_mm, y = body_mass_g, color = species)) +
geom_point() +
theme_minimal()
```Pro-Tip: The
#| label: fig-xxxchunk option is vital. Without thefig-prefix, Quarto will not include this in the automatic “List of Figures” or allow for dynamic cross-references.
Commit these changes to your repository with a meaningful commit message, and push to GitHub.
📊 Task 2: Creating Professional Tables
Static Markdown tables are fine for simple notes, but for reproducible research, we generate tables directly from data frames. To cross-reference a table, the label must start with tbl-.
Exercise
Create a summary table of the penguin data using knitr::kable:
```{r}
#| label: tbl-penguin-summary
#| tbl-cap: "Mean body mass of penguins by species and sex."
library(dplyr)
penguins |>
filter(!is.na(sex)) |>
group_by(species, sex) |>
summarise(mean_mass = mean(body_mass_g)) |>
knitr::kable()
```Commit these changes to your repository with a meaningful commit message, and push to GitHub.
🔗 Task 3: Dynamic Cross-Referencing
One of the greatest strengths of Quarto is automation. If you add a new figure at the beginning of your document, Quarto renumbers every subsequent figure and update every mention in your text automatically.
Syntax
- To reference a figure: Use
@fig-label - To reference a table: Use
@tbl-label
Exercise
Write a short paragraph in your document referencing the items you created above:
As shown in ?@fig-penguins-scatter, there is a clear positive correlation between flipper length and body mass. The specific averages for each group are detailed in ?@tbl-penguin-summary.
Commit these changes to your repository with a meaningful commit message, and push to GitHub.
🎓 Extension: Academic Referencing
To use academic citations, you need a bibliography file (usually a .bib file).
1. Create a Bibliography
Create a new text file in your project folder named references.bib and paste this BibTeX entry inside:
@Manual{,
title = {palmerpenguins: Palmer Archipelago (Antarctica) penguin data},
author = {Allison Marie Horst and Alison Presmanes Hill and Kristen B Gorman},
year = {2020},
note = {R package version 0.1.0},
doi = {10.5281/zenodo.3960218},
url = {https://allisonhorst.github.io/palmerpenguins/},
}2. Update your YAML
Add the bibliography field to the top of your .qmd file:
---
title: "Workshop 4"
bibliography: references.bib
---3. Cite in Text
Use the key from your .bib file preceded by an @ symbol.
- Parenthetical: The data was originally collected by Gorman et al. [@penguins_data].
- Narrative: @penguins_data argues that…
Quarto will automatically generate a References section at the very end of your document.