我们能否在从rmarkdown生成的html输出中保持plotly对象的标题在顶部?

7
以下的Markdown显示了一个问题,即使用fig.topcaption=TRUE可以成功将标题置于ggplot图形顶部,但在plotly对象中无法实现。我在这里学到了有关fig.topcaption的信息。 fig.topcaption的基础代码似乎存在于knitr中,但是可能与plotly图形不兼容,或者可能与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')
```
1个回答

6
以下技巧可以解决HTML输出问题。
我们可以将图像格式化为表格(仅用作单元格的图像),并将段落(其中包含图像标题)格式化为表格标题,并将其放置在顶部。
只需将此CSS块放置在YAML下方即可:
```{css plotly-caption, echo = FALSE}
div.figure {
  display: table;
}
div.figure p {
  display: table-caption;
  caption-side: top;
}
```

2
适用于简单rmarkdown的好解决方案。在Bookdown中,这个解决方案也很适用 - 我使用了那个css代码并将其粘贴到我的style.css文件中。 - Mark Neal

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接