虽然没有得出成功的答案,但是代码输出显示使用bookdown格式时会标记数字,但使用Stéphane提出的解决方案或者以下来自dyrland建议链接的解决方案可以避免这种情况。
---
title: "Untitled"
author: "Internet"
date: "29 février 2020"
output:
bookdown::html_document2
---
```{r setup2}
#https://dev59.com/ZH3aa4cB1Zd3GeqPd3wK#26743812
library(knitr)
knit_hooks$set(plot = function(x, options) {
paste('<figure><figcaption>', options$fig.cap, '</figcaption><img src="',
opts_knit$get('base.url'), paste(x, collapse = '.'),
'"></figure>',
sep = '')
}) #comment out to restore numbering
library(ggplot2)
```
```{r, echo = TRUE, fig.cap="Hello Dolly"}
ggplot(diamonds,aes(price,carat)) + geom_point()
```
解决方案能否同时包括编号和标题置于顶部?
注:原问题的作者提到了bookdown,但未提供完整的工作示例以演示此功能。如果其他人认为更有用,我很乐意编辑原问题。
编辑
Yihui在他的答案中展示了使用fig.topcaption=TRUE
这个方便的选项-谢谢!不幸的是,该标题(对于plotly图形)虽然保留了正确的编号,但仍被推到底部。以下是示例:
---
title: "Untitled"
author: "Internet"
date: "29 février 2020"
output:
bookdown::html_document2
---
```{r setup, message=FALSE, echo=FALSE}
library(knitr)
library(ggplot2)
library(plotly)
```
Here is a ggplot object with caption at the top as desired.
```{r, fig.cap="Hello ggplot", fig.topcaption=TRUE, message=FALSE, echo=FALSE}
ggplot(diamonds,aes(price,carat)) + geom_point()
```
Here is the previous ggplot converted to a plotly object with caption reverting to the bottom.
```{r, fig.cap="Hello plotly", fig.topcaption=TRUE, message=FALSE, echo=FALSE}
my_ggplot <- ggplot(diamonds,aes(price,carat)) + geom_point()
ggplotly(my_ggplot)
```
Caption reverts to bottom even if plotly object is not created from ggplot object
```{r, fig.cap="Hello plotly2", fig.topcaption=TRUE, message=FALSE, echo=FALSE}
plot_ly(
x=c(1,2,3),
y=c(5,6,7),
type='scatter',
mode='lines')
```
编辑2
使用CSS解决了Plotly字幕位置问题,链接在此