Bookdown中的图像脚注

问题描述 投票:1回答:2

如何在bookdown::pdf_document2的图像正下方添加图形注释?对ggplot2创建的情节来说,相对容易。 grid.arrange可以与grid.arrange(<plot>, bottom = <figure_notes>)合作。但是,如果我手动插入图像,例如knitr::include_graphics(rep("images/knit-logo.png", 3)),有没有办法为它添加一个图形注释?我看到了knitr选项,fig.subcap。但这无法编译:

```{r fig.show="hold", fig.cap = "a", fig.subcap = "b"}

knitr::include_graphics(rep("images/knit-logo.png", 3))

```

返回:

This is pdfTeX, Version 3.14159265-2.6-1.40.18 (MiKTeX 2.9.6350 64-bit)
entering extended mode
! Undefined control sequence.
<recently read> \subfloat 

Error: Failed to compile miniSample.tex. See miniSample.log for more info.

谢谢

r knitr r-markdown bookdown
2个回答
1
投票

如果要使用subfigures,则必须在LaTeX中加载subfig包。这可以通过使用YAML中的header-includes参数来完成,正如here所解释的那样。

这是一个最小的,可重现的示例,它创建一个本地图像“temp.jpg”,然后生成三个子图:

---
output: pdf_document
header-includes:
   - \usepackage{subfig}
---

```{r}
jpeg(filename = "temp.jpg")
plot(cars)
dev.off()
```

```{r fig.show="hold", fig.cap = "a", fig.subcap = c("Your Caption", "Another Caption", "Isn't this great"), fig.ncol = 3, out.width="33%"}
knitr::include_graphics(rep("temp.jpg", 3))
```

enter image description here

看看这篇文章的原始描述:Subfigures or Subcaptions with knitr?


0
投票

找到了一个权宜之计:

  1. 使用magick::image_read将图像读入R中;
  2. 使用raster将图像转换为rasterGrob项目;
  3. textGrob创建一个注释;使用grid.arrange呈现;
  4. 使用grid.arrange将图像与笔记一起呈现。 plot_A <- magick::image_read(<path>) %>% rasterGrob(interpolate = TRUE) note <- textGrob("This is a note.") grid.arrange(plot_A, bottom = note)
© www.soinside.com 2019 - 2024. All rights reserved.