如何在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.
谢谢
如果要使用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))
```
看看这篇文章的原始描述:Subfigures or Subcaptions with knitr?
找到了一个权宜之计:
magick::image_read
将图像读入R中;raster
将图像转换为rasterGrob
项目;textGrob
创建一个注释;使用grid.arrange
呈现;grid.arrange
将图像与笔记一起呈现。
plot_A <- magick::image_read(<path>) %>% rasterGrob(interpolate = TRUE)
note <- textGrob("This is a note.")
grid.arrange(plot_A, bottom = note)