Figures

Figures are essential for visualizing data, illustrating concepts, or enhancing your reports. Quarto supports figures generated from R code as well as external images, allowing you to control captions, size, and placement for a polished presentation.

Figures Generated from R

When creating plots in R, you can add captions and adjust figure size using chunk options in Quarto.

The options within your R chunk would be set like this:

#| label: fig-scores
#| fig-cap: "Boxplot of scores by group"
#| fig-width: 6
#| fig-height: 4

And with the following R code:

library(ggplot2)

data <- data.frame(
  Group = c("A", "B", "C"),
  Score = c(5.2, 6.8, 4.9)
)

ggplot(data, aes(x = Group, y = Score, fill = Group)) +
  geom_bar(stat = "identity") +
  theme_minimal()
Figure 1: Boxplot of scores by group

This would then result in the figure as directly outputted after the code. Here, you see:

  • fig-cap: Adds a caption to the figure.
  • fig-width and fig-height: Control the size of the rendered figure (in inches for HTML/PDF).
  • label: Allows you to cross-reference the figure in text with @fig-scores: Ref to Figure 1.

You can also combine fig-align: "center" in the chunk to center the figure in the output.

2. External Images

Quarto can include images stored locally or from the web using Markdown syntax:

Local images:

Local images can be presented as follows:

![This is a local image caption](/assets/images/ecrins_pano.jpg){#fig-local width=50%}
Figure 2: This is a local image caption
  • {#fig-local}: Assigns a label for cross-referencing using @fig-local: Figure 2.
  • width=50%: Adjusts the display size relative to the output width.

Web images:

Web images can be presented by linking to them directly:

![Quarto logo](https://quarto.org/quarto.png){width=200px}

Quarto logo
  • Directly embeds an image from a URL.
  • Can adjust size using width or height.
  • Captions can be added, but for HTML output, you may need to style with CSS for advanced formatting.

Best Practices for Figures

  1. Always add captions: Makes figures self-contained and referenceable.
  2. Label your figures: Use #| label: for R plots or {#fig-label} for Markdown images to enable cross-referencing.
  3. Adjust sizes thoughtfully: Use fig-width/fig-height or CSS/Markdown size attributes to make sure figures are readable.
  4. Use consistent styling: Keep themes, fonts, and color schemes consistent across all figures for a professional look.
  5. Prefer vector graphics for clarity: When possible, export PDF/SVG plots for crisp rendering in reports.

Combining Inline References with Figures

Once a figure is labeled, you can refer to it in the text dynamically:

As shown in @fig-scores, group B has the highest score.

Which will look as follows in text: As shown in Figure 1, group B has the highest score.

Quarto will automatically number the figure in the rendered document.

This approach allows you to seamlessly integrate data-driven plots, static images, and online resources while keeping your figures clean, readable, and fully reproducible.

External Resources

For more detail, see the Quarto documentation

Back to top