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()
Figures are essential for visualizing data. Quarto supports figures generated from R code as well as external images, with chunk options and Markdown attributes controlling captions, size, and placement.
Do: Run the code below in your own Quarto document. It sets four chunk options:
label: fig-scores — a label enabling this figure to be cross-referenced elsewhere as @fig-scoresfig-cap — the figure’s captionfig-width / fig-height — the figure’s dimensions, in incheslibrary(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()
See: the resulting figure, with the caption and dimensions from the chunk options applied.
Predict: if you add fig-align: "center" to this same chunk, what do you expect to change: the figure’s content and size, or something else?
Explain: fig-align only changes the figure’s horizontal placement on the page (left/center/right); the plot itself, its caption, and its dimensions stay exactly the same. Chunk options that control appearance are independent of one another, much like the hist()/plot() styling options covered in the visualization module.
When possible, export figures as vector graphics (PDF or SVG) rather than raster formats (PNG, JPEG): vector graphics stay crisp at any zoom level or print size, while raster images can look blurry or pixelated when scaled up.
Local and web images can also be included, using Markdown’s image syntax.
Local images, referenced with a project-relative path (leading /):
Web images, linked directly by URL:

Both accept the same {#fig-label width=...} attributes as R-generated figures, so they can be sized and cross-referenced the same way.
A local image path that works on one page can break on another if it’s a plain relative path (e.g., images/photo.jpg), since Quarto resolves it relative to that page’s own folder — the same class of path problem covered for bibliography files earlier in this module. For images, a leading / reliably resolves the path from the project root regardless of which page includes it, so prefer /assets/images/photo.jpg over a bare relative path.
Once a figure has a label, reference it anywhere in the text with @:
As shown in Figure 1, group B has the highest score.
Create a labeled, captioned bar plot with ggplot2 showing the Score for each Group in the data frame from the example above, sized 5 by 3 inches.
The plot itself can be built and tested here with ggplot2. In an actual .qmd file, you would also add these chunk options above the code to control the label, caption, and size:
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()
In a real .qmd file, this chunk would also carry #| label: fig-mine, #| fig-cap: "Your caption here", #| fig-width: 5, and #| fig-height: 3 as chunk options above the code.