需求:假设有两个图片需要并列放在一排(居中),并分别在下方添加“图1、这是图1”、“图2、这是图2”两个标题,且在一行。
以上需求主要针对word和html,当然knitr对pdf的可控参数比较多,比较灵活,但我的工作场景大多还是word和html(这取决于领导的接受能力?)。
对通过gridExtra把图片组合在一起的方案不在讨论之列。
statckoverflow上相关的讨论已经看过,对word,图片居中,两个并列一排似乎都不能实现,不知道理解的对不对?通过template设置来实现好像也不现实,不同的图片可能排版不同?利用fig.cap可以添加标题,但fig.cap长度如果在1个以上,就干脆不显示了,不确定算不算bug?
对html,利用fig.show="hold",out.width="40%",图片可以并列在一排,但这时fig.cap只能显示第二个,利用fig.show="asis"图片不能排在一行。
利用fig.subcap,均无效果。
还有一个疑问:fig.show为什么不在html_document函数的参数中?平时只看html_document的参数,几乎忘了fig.show的存在。
欢迎热烈讨论,赐教!
以下是一个简单测试的Rmd:
---
title: "Test plot output of knitr"
author: "test"
date: "2017-08-10"
output:
html_document:
fig_caption: true
word_document:
fig_caption: true
---
setting fig.show='asis':
for html show two captions, but not in one row!
for word, show caption only when length(fig.cap)<2
```{r test1, fig.cap=c("Fig one caption", "and yet an other"), out.width='35%',fig.show='asis',echo=FALSE,warning=FALSE,message=FALSE}
ggplot2::qplot(1:5)
ggplot2::qplot(6:10)
```
setting fig.show='hold',
for html only show the last caption
for word show no caption
```{r test2, fig.cap=c("Fig one caption", "and yet an other"), out.width='35%',fig.show='hold',echo=FALSE,warning=FALSE,message=FALSE}
ggplot2::qplot(1:5)
ggplot2::qplot(6:10)
```
setting fig.subcap
```{r test3, fig.cap="main fig cap",fig.subcap=c("subFig one caption", "another subFig
caption"),fig.scap=NA,out.width='35%',fig.show='hold',echo=FALSE,warning=FALSE,message=FALSE}
ggplot2::qplot(1:5)
ggplot2::qplot(6:10)