html Rmarkdown 中的上图标题

Caption above figure in html Rmarkdown

在 RMarkdown 中编织到 HTML 时,是否可以将标题移动到我的图形上方?似乎在 PDF 中是可能的,即 when knitting to PDF,但我不知道如何为 HTML 复制它。我正在使用 bookdown 来给数字编号。

当我运行这样的事情时:

```{r fig.cap= "caption"}
df <- data.frame(letter = letters[1:5], value = 1)

ggplot(df, aes(as(factor(1), value, fill = letters))) +
  geom_bar(stat = "identity")

```

标题显示在图的底部,但我想显示在图的上方。

您可以这样做,但是如果您设置 echo = TRUE,标题将出现在代码上方...

---
title: "Untitled"
author: "Stéphane Laurent"
date: "29 février 2020"
output: html_document
---

```{r setup, include=FALSE}
knitr::knit_hooks$set(htmlcap = function(before, options, envir) {
  if(before) {
    paste('<p class="caption">', options$htmlcap, "</p>",sep="")
  }
})
```

```{r, echo = FALSE, htmlcap="Hello Dolly"}
library(ggplot2)
ggplot(diamonds,aes(price,carat)) + geom_point()
```

这不是一个成功的答案,但代码输出显示 bookdown 格式的选择,否则会为数字编号,通过包含由提出的解决方案来抑制Stéphane,或者如下所示,来自 dyrland 建议的 link 的解决方案。

---
title: "Untitled"
author: "Internet"
date: "29 février 2020"
output:
  bookdown::html_document2
---

```{r setup2}
#https://whosebug.com/a/26743812/4927395 
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()
```

解决方案能否在顶部同时包含编号和标题?

N.B 原始问题的作者确实提到了 bookdown,但没有提供完整的工作示例来证明这一点。如果其他人认为更有用,很高兴编辑原始问题。

编辑 Yihui 在他的回答中表明 fig.topcaption=TRUE 有一个简单的选择 - 谢谢!不幸的是,该标题虽然保留了正确的编号,但仍被推到底部(在情节人物的情况下)。示例如下:

---
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 here

解决了 Plotly 标题位置问题

硬编码函数永远不是我的首选选项,但它确实有效(为表创建另一个函数。)(源代码在这里:R Markdown HTML Number Figures

现在,要弄清楚如何引用这些...

---
title: "Untitled"
author: "Internet"
date: "29 fevrier 2020"
output:
  bookdown::html_document2
---

```{r setup2}
# 
library(knitr)
library(ggplot2)

capFigNo <- 1
capFig <- function(x){
      x <- paste0("Figure ",capFigNo,": ",x)
    capFigNo <<- capFigNo + 1
    x
}


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

```


```{r echo = TRUE, fig.cap=capFig("Hello Dolly")}
#the trick is to wrap your caption in your number prefixing function.
ggplot(diamonds,aes(price,carat)) + geom_point()
```

```{r echo = TRUE, fig.cap=capFig("Hello Dolly2")}
ggplot(diamonds,aes(price,carat)) + geom_point()    
```

对于 HTML 输出,您可以设置块选项 fig.topcaption = TRUE 以将标题放在数字上方。下面是一个最小的例子(它适用于 html_documentbookdown 的 HTML 输出格式):

---
title: "Reprex"
output:
  html_document: null
  bookdown::html_document2: null
---

```{r, fig.cap='A caption.', fig.topcaption=TRUE}
plot(cars)
```