r - 如何在 R markdown (bookdown) 中添加子图

标签 r r-markdown knitr bookdown

我正在尝试在 R bookdown 项目中添加一个带有子标题的图形,如下所示

---
output:
  pdf_document:
    extra_dependencies: "subfig"
---
     
```{r  echo=F, out.width = "50%",fig.showtext=TRUE,fig.show='hold',fig.cap="TITULO"}
par(mfrow=c(1,2))
knitr::include_graphics("ts_mult.png") 
knitr::include_graphics("ts_ad.png") 
```
我得到以下结果
enter image description here
然后我尝试添加子标题(“imagen a”,“imagen b”)如下
```{r  echo=F, out.width = "50%",fig.showtext=TRUE,fig.show='hold',fig.cap="TITULO",fig.subcap=c("imagen a", "imagen b")}
par(mfrow=c(1,2))
knitr::include_graphics("ts_mult.png") 
knitr::include_graphics("ts_ad.png") 
```
但这不起作用,并引发以下错误
! Undefined control sequence.
<recently read> \subfloat 

Error: LaTeX failed to compile Tesis_AE.tex. See https://yihui.org/tinytex/r/#debugging for debugging tips.
如何正确添加子标题?

最佳答案

您只需要添加 fig.subcap=c("A subtitle","Another subtitle")在块中。
至少在我的环境中,fig.showtext=TRUE不是必需的,而是会导致错误:Error in loadNamespace(name) : there is no package called 'showtext' Calls: <Anonymous> ... loadNamespace -> withRestarts -> withOneRestart -> doWithOneRestart Execution halted .这就是为什么我在答案中排除了该设置。

---
output:
  pdf_document:
    extra_dependencies: "subfig"
    keep_tex: yes
---
     
```{r echo=F, out.width = "50%",fig.show='hold',fig.cap="TITULO", fig.subcap=c("Subtitulo1","Subtitulo2")}
par(mfrow=c(1,2))
knitr::include_graphics("your-path-to/image1.png") 
knitr::include_graphics("your-path-to/image2.png") 
```
session 信息
> sessionInfo()
R version 4.0.2 (2020-06-22)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18362)

Matrix products: default

locale:
[1] LC_COLLATE=Japanese_Japan.932  LC_CTYPE=Japanese_Japan.932    LC_MONETARY=Japanese_Japan.932
[4] LC_NUMERIC=C                   LC_TIME=Japanese_Japan.932    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
[1] compiler_4.0.2 tools_4.0.2   

> rmarkdown::pandoc_version()
[1] ‘2.7.3’
enter image description here

关于r - 如何在 R markdown (bookdown) 中添加子图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63665513/

相关文章:

r - R 中带有 group_by 的多个条件语句?

r - RStudio 中 R markdown 文档中的自动编号方程

r - 使用 knitr 和 RStudio 生成的 html 文件的默认位置变化很奇怪

r - 为什么 as.integer64 ("") 返回 0 而不是 NA_integer64_?

r - #include <Rcpp11> 文件未找到错误

string - 在 R 中读取空格分隔的数字

r - bookdown文档中的直方图出现两次

r - 循环遍历 ggplots 列表并使用 Knitr 为每个图提供一个图形标题

r - knitr 在文档中生成错误,但无论如何都能正确生成数字

r - 在 R Markdown 中将 str(...) 显示为表格