mzkrc
mzkrc

Reputation: 419

How to remove figure captions in Quarto-R Markdown without breaking cross-references

I have another very basic question to reference a figure in Quarto using R, which I could not find an answer to it online.

So, here is very minimal reproducible example;

---
title: "Untitled"
format: pdf
editor: visual
---

```{r fig-plot, warning=FALSE, message=FALSE, echo=FALSE}
library(tidyverse)
mtcars <- mtcars

ggplot(mtcars, aes(x = mpg, y = cyl)) +
  geom_point()

As can be seen in @fig-plot

The resulting output is enter image description here

As can be seen from the output, there is a text under the figure, which I do not want to have. How can I remove it without breaking the plot's cross-reference? Thank you for your kind attention beforehand.

Upvotes: 1

Views: 1019

Answers (1)

ncraig
ncraig

Reputation: 1213

Option 1: remove fig from chunk label

Your reprex was missing closing fence on the code chunk. With that fixed, simply removing the fig portion will remove the caption text below the figure. You also don't need fig.cap: false in the YAML frontmatter.

The fig section in the chunk label flags for quarto that this is a figure. See the documentation here. By omitting fig, the caption is removed. This does not solve the broken cross reference, but it isn't clear from the question what you want to do with that.

Additionally, as per the documentation, one can move the chunk options down into the chunk itself.

---
title: "Untitled"
format: pdf
editor: visual
---



```{r}
#| label: plot
#| warning: false
#| message: false
#| echo: false
library(tidyverse)
mtcars <- mtcars

ggplot(mtcars, aes(x = mpg, y = cyl)) +
  geom_point()
```

Output:

enter image description here

Option 2 set fig-cap to blank

Another approach is to set the chunk options to have a blank figure caption. This will keep the figure label, and the cross reference will work properly. However, if the desire is to remove all figure label information (which will break the cross-reference) option #1 is probably better.


```{r}
#| label: fig-plot
#| fig-cap: ""
#| warning: false
#| message: false
#| echo: false

library(tidyverse)
mtcars <- mtcars

ggplot(mtcars, aes(x = mpg, y = cyl)) +
  geom_point()
```

Output:

enter image description here

Option 3 Put title in caption and place caption above figure.

Comments indicate the desire to reference a figure by its title. To my knowledge, quarto cross references require some type of figure label so that there is an entity that is being cross referenced. In option 3, the plot title can be placed in the figure caption and the caption moved to the top of the figure.

---
title: "Untitled"
format: pdf
editor: visual
fig-cap-location: top
---

```{r}
#| label: fig-plot
#| fig-cap: "Plot Title"
#| warning: false
#| message: false
#| echo: false

library(tidyverse)
mtcars <- mtcars

ggplot(mtcars, aes(x = mpg, y = cyl)) +
  geom_point()
```

Output:

enter image description here

Upvotes: 0

Related Questions