Currently, I am writing a Quarto Markdown document with an HTML output. In this document I want to show several plots. Each plot should be within an own section in order to facilitate navigation. As I want to reduce boilerplate I create the plots dynamically. Now, the section headers appear one after another while the plots all appear at the end of the section.
Here is a reproducible example:
---
title: "Untitled"
format: html
editor: visual
---
## Quarto
```{r}
#| label: fig-attr-in-time
#| fig-cap: "Attribute in time"
#| results: "asis"
library(dplyr)
library(ggplot2)
library(purrr)
data_files = c(
"test1",
"test2",
"test3"
)
set.seed(176)
test1 = data.frame(year = 1991:2010, ratio = runif(20, 0, 1))
test2 = data.frame(year = 1991:2010, ratio = runif(20, 0, 1))
test3 = data.frame(year = 1991:2010, ratio = runif(20, 0, 1))
print_attr_plots = function(data) {
cat('\n\n### ', data, '\n')
data_tmp = get(data)
p = ggplot(data_tmp, aes(x = year, y = ratio)) + geom_bar(stat = "identity", color = "black", fill = "white") + ylab("Ratio") + xlab("Year") + theme_grey(base_size = 10)
print(p)
}
pwalk(list(data_files), print_attr_plots)
```
Instead, I want the section headers and plots to alternate. This code actually works in R-Markdown but strangely it does not in Quarto. Please help.

The reason of Quarto putting the figures together is because of that special
fig-prefixed label and not because you are usingfig-cap. Your intended approach would work if you had used either a simple label (likelabel: test, i.e. notfig-prefixed) or no label at all andfig-cap.So the following works as intended (almost!!!),
Note that, I have used a list structure in
fig-capso that three plots have different captions.rendered output
But the above approach is not quite enough (in my opinion). Because if you look at the output, you would see there are no
Figureprefix in the captions and since you are not using a label, you can not refer to these figures.So to solve this completely, you may want to use `Figure Divs,
Here a separate Figure-Divb with id (which you can use in cross-referencing) and a caption is being created for each figure. Check out the linked documentation to know details about Figure-Divs.
rendered output