4  Images

Links

4.1 Native Quarto figures

Here are some examples of adding figures.

Figure 4.1: Relative Path Directions

And I can cross reference the figure by typing @fig-directions. See Figure 4.2

Important

For cross-references to work, the image must have a caption and a label.

4.2 Adding figures with Knitr

In R4DS (link above), Hadley et al. are still using knitr::include_graphics("path") to insert images even though the book has been converted to Quarto documents. When using Bookdown, Yihui gives four arguments for using knitr::include_graphics("path") instead of native markdown image formatting. So, we will likely continue to use them too. Here is an example image code chunk from R4DS:

```{r}
#| label: fig-ds-diagram
#| echo: false
#| fig-cap: |
#|   In our model of the data science process, you start with data import
#|   and tidying. Next, you understand your data with an iterative cycle of
#|   transforming, visualizing, and modeling. You finish the process 
#|   by communicating your results to other humans.
#| fig-alt: |
#|   A diagram displaying the data science cycle: Import -> Tidy -> Understand 
#|   (which has the phases Transform -> Visualize -> Model in a cycle) -> 
#|   Communicate. Surrounding all of these is Communicate.
#| out.width: NULL

knitr::include_graphics("diagrams/data-science/base.png", dpi = 270)
```

Here, I’m adding my own image with knitr::include_graphics("path").

An image of a store with directions

Figure 4.2: Relative Path Directions.

And I can cross reference the figure by typing @fig-directions. See Figure 4.2

Important

For cross-references to work, the image must have a caption and a label.