我们可以在 rmarkdown 的 html 输出中将标题保留在 plotly 对象的顶部吗?

Mar*_*eal 7 html r ggplot2 r-markdown plotly

下面的降价显示了标题使用 成功到达 ggplot 图形顶部的问题fig.topcaption=TRUE,但没有使用plotly对象。

在这里了解了 fig.topcaption 。的底层代码fig.topcaption似乎存在于knitr 中,尽管它与绘图图形不兼容,或者它可能与 pandoc、html/html 小部件相关,或者在从 rmarkdown 到最终输出的链中的其他地方。

我现在对解决方法很满意 - 建议?

---
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')
```
Run Code Online (Sandbox Code Playgroud)

Rad*_*tić 6

以下技巧将适用于 HTML 输出。
我们可以将图形格式化为表格(图像仅作为单元格)和段落(图形标题所在的位置)作为表格标题并将其放在顶部。

只需将此 CSS 块放在 YAML 下方:

```{css plotly-caption, echo = FALSE}
div.figure {
  display: table;
}
div.figure p {
  display: table-caption;
  caption-side: top;
}
```
Run Code Online (Sandbox Code Playgroud)

  • 简单的降价的好解决方案。在 Bookdown 中,对我来说也很有效 - 我使用了该 css 代码并将其粘贴在我的“style.css”文件中。 (2认同)