这是我在 Rstudio 的
.qmd
文档中的代码。我将在 Rstudio 中使用 Quarto 文档编写一份报告。
---
title: "Quarto test document"
format:
pdf:
pagesize: "a4"
number-sections: true
number-depth: 2
margin-left: 30mm
margin-right: 30mm
execute:
include: true
warning: false
error: false
freeze: false
fig-cap-location: bottom
fig-dpi: 300
fig-align: center
fig-format: pdf
---
# Creating picture
\`\`\`{r}
#| label: fig-myplot
#| fig-cap: "This is the label of picture."
#| fig-height: 3
#| fig-width: 4
#| layout-nrow: 1
#| column: page
plot(1:7, abs(-3 : 3))
hist(cars$speed)
\`\`\`
您可以看到,除了“图1”和“图2”之外,还有两个重复的相同标签。其实我只需要“图1:这是图片的标签”。
#| fig-subcap:
我将它们添加到 R 块中:
#| fig-cap: "This is the label of picture."
#| fig-subcap:
#| - ""
#| - ""
#| fig-height: 3
您可以看到重复的标签消失了。只有一个“图1:这是图片的标签”(见绿线)。这是我向往的目标。然而,这些
(a)
和(b)
(见红线)的标签位于每张图片的左下角。事实上,它们应该放在期刊论文中每张图片的左上角位置。这不是好戏。
patchwork
或类似方法我使用
mfrow=c(1,2)
,patchwork
来绘制 ggplot 图片,将两张图片合并为一张。
正如你所看到的,我使用了
patchwork
来达到预期的效果,但是是否可以直接修改四开文档中的 YAML
和 #|
部分来实现这些效果呢?毕竟,当我想将使用 plot(y~x)
的 (a) 图形和使用 ggplot()
的 (b) 图形合并为一个图形时,mfrow=c(x,x)
、patchwork
、ggside
和 ggarrange
变得无效。使用 grid & gridExtra
当然是可以的,但是代码看起来会非常冗余。我认为这并不优雅。我认为目前最好的方法是修改YAML和R chunk部分中的#|
内容,这是最优雅的方法。
1. 在
### Question
部分,如果我可以只保留“图1:这是图片的标签”。没有“图2:这是图片的标签”。通过修改 R chunk 或 YAML 组件中的 #|
部分,这将是一个很大的帮助。
2. 当 (a) 和 (b) 标签都存在时,如果我可以通过修改 R 块或 YAML 组件中的
#|
部分,将 (a) 和 (b) 标签移动到每个图的左上角并且只保留一个标签“图1:这是图片的标签。”,这也会有很大的帮助。
以上任何解决方案将不胜感激。
这是使用布局功能的一种方法:
---
title: "Quarto test document"
format:
pdf:
pagesize: "a4"
number-sections: true
number-depth: 2
margin-left: 30mm
margin-right: 30mm
execute:
include: true
warning: false
error: false
freeze: false
fig-cap-location: bottom
fig-dpi: 300
fig-align: center
fig-format: pdf
execute:
echo: false
---
# Creating picture
::: {layout="[ 0.4, -0.1, 0.4 ]" #fig-myplot}
:::: {#first-column}
```{r}
plot(1:7, abs(-3 : 3))
```
:::
::: {#second-column}
```{r}
hist(cars$speed)
```
:::
This is the label of picture.
::::