我正在尝试使用 knitr 插入带有一个标题的并排图像:
---
title: "XY"
author: "Doogan"
date: '`r format(Sys.Date(), "%B %d, %Y")`'
graphics: yes
output:
pdf_document:
toc: no
fig_caption: true
fontsize: 11pt
geometry: margin=1cm
---
```{r Directory, echo=F, message=F, warning=FALSE, paged.print=TRUE}
image.dir <- "your/directory/"
knitr::opts_chunk$set(echo = FALSE)
```
```{r 'xy_images'
,fig.show='hold',fig.pos='H',out.height='7.5cm',out.width='10cm',fig.cap="X
image (A) and Y image (B)"}
knitr::include_graphics(file.path(image.dir, 'X.png'))
knitr::include_graphics(file.path(image.dir, 'Y.png'))
```
上述代码生成两个堆叠的图像,带有两个单独的(相同)标题:
当删除 fig.cap 调用时,图形正确对齐,但没有标题。
我怎样才能改变代码块以便图像并排对齐并且只显示一个标题?
答案1
我找到了一种解决此问题的方法,即在同一个绘图窗口中将 .png 图像绘制为栅格:
```{r 'xy_images', fig.width = 8,fig.height = 4.5,
fig.show='hold',fig.pos='H'fig.cap="X image (A) and Y image (B)"}
library(png);library(raster)
X <-readPNG("X.png")
Y <-readPNG("Y.png")
#set up figure
par(mar=c(0,0,0,0), xpd=NA, mgp=c(0,0,0),
oma=c(0,0,0,0), ann=F, mfrow = c(1,2))
plot.new()
usr<-par("usr")
#fill plot with images
rasterImage(X, usr[1], usr[3], usr[2], usr[4])
rasterImage(Y, usr[1]+1.1, usr[3], usr[2]+1.1, usr[4])
```