我们可以将标题保留在rmarkdown的html输出中的可绘制对象的顶部吗? [英] Can we keep the caption at the top of plotly objects in html output from rmarkdown?

查看:67
本文介绍了我们可以将标题保留在rmarkdown的html输出中的可绘制对象的顶部吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下标记显示了使用fig.topcaption=TRUE字幕成功到达ggplot图形顶部但不使用 plotly 对象的字幕的问题.

The following markdown shows the problem that captions that successfully go to the top of ggplot figures with the use of fig.topcaption=TRUE, but do not with plotly objects.

我在此处了解了fig.topcaption. fig.topcaption的基础代码似乎位于针织,尽管它可能与可绘图图形不兼容,或者可能是与pandoc或html/html窗口小部件相关的,也可能是从rmarkdown到最终输出的链中其他位置.

I learnt about fig.topcaption here. The underlying code for the fig.topcaption appears to live here in knitr, though either that is not compatible with plotly figures, or it could be pandoc, or html/html widgets-related, or somewhere else in the chain from the rmarkdown to final output.

我对现在的工作感到很满意-建议?

I'm happy enough with a work around for now - suggestions?

---
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')
```

推荐答案

以下

The following trick will work arround HTML output.
We can format the figure as a table (with image as it only cell) and the paragraph (where the figure caption live) as a table caption and place it on the top.

只需将此CSS块放置在YAML之下:

Just place this CSS chunk below YAML:

```{css plotly-caption, echo = FALSE}
div.figure {
  display: table;
}
div.figure p {
  display: table-caption;
  caption-side: top;
}
```

这篇关于我们可以将标题保留在rmarkdown的html输出中的可绘制对象的顶部吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆