英文:
Dynamically created plots do not alternate with section headers in Quarto
问题
以下是您要翻译的内容:
"目前,我正在编写一个Quarto Markdown文档,希望以HTML输出。在这个文档中,我想展示几个图表。每个图表应该位于自己的部分内,以便进行导航。为了减少样板代码,我动态创建这些图表。
现在,部分标题一个接一个地出现,而图表都出现在部分的末尾。
这是一个可重现的示例:
title: "无标题"
format: html
editor: visual
Quarto
#| label: fig-attr-in-time
#| fig-cap: "时间内的属性"
#| 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("比率") + xlab("年份") + theme_grey(base_size = 10)
print(p)
}
pwalk(list(data_files), print_attr_plots)
相反,我希望部分标题和图表交替出现。
这段代码在R-Markdown中实际上是有效的,但奇怪的是在Quarto中却不起作用。请帮助。"
英文:
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.
答案1
得分: 3
Quarto将这些图形组合在一起的原因是因为有一个特殊的以fig-
为前缀的标签,而不是因为你使用了fig-cap
。如果你使用了一个简单的标签(比如label: test
,而不是fig-
作为前缀)或者根本不使用标签和fig-cap
,你的预期方法就会起作用。
所以下面的代码几乎可以按预期工作:
#| fig-cap:
#| - "Attribute in time in test1"
#| - "Attribute in time in test2"
#| - "Attribute in time in test3"
#| results: "asis"
#| message: false
#| echo: false
但是上述方法还不够完美(依我之见)。因为如果你查看输出,你会看到标题中没有Figure
前缀,而且由于你没有使用标签,你无法引用这些图形。
因此,为了完全解决这个问题,你可以考虑使用Figure Divs:
#| results: "asis"
#| message: false
在这里,为每个图形创建了一个单独的Figure-Div,带有ID(你可以用于交叉引用)和标题。查看链接的文档以了解有关Figure-Divs的详细信息。
英文:
The reason of Quarto putting the figures together is because of that special fig-
prefixed label and not because you are using fig-cap
. Your intended approach would work if you had used either a simple label (like label: test
, i.e. not fig-
prefixed) or no label at all and fig-cap
.
So the following works as intended (almost!!!),
Note that, I have used a list structure in fig-cap
so that three plots have different captions.
---
title: "Untitled"
format: html
---
## Quarto
```{r}
#| fig-cap:
#| - "Attribute in time in test1"
#| - "Attribute in time in test2"
#| - "Attribute in time in test3"
#| results: "asis"
#| message: false
#| echo: false
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)
}
walk(data_files, print_attr_plots)
```
But the above approach is not quite enough (in my opinion). Because if you look at the output, you would see there are no Figure
prefix 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,
---
title: "Untitled"
format: html
keep-md: true
---
## Quarto
```{r}
#| results: "asis"
#| message: false
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)
div_label <- paste0("#fig-", data)
cat("\n::: {", div_label, "}\n", sep="")
print(p)
cat("\n\nAttribute in time in ", data, "\n", sep="")
cat("\n:::\n")
}
walk(data_files, print_attr_plots)
```
check the plots @fig-test1, @fig-test2, @fig-test3
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论