为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

慕尼黑的夜晚无繁华 2023-09-04 15:27:52
下面的 markdown 显示了使用 成功到达 ggplot 图形顶部的标题,但使用plotly对象则fig.topcaption=TRUE不行的问题。我在这里了解了Fig.topcaption 。底层代码似乎fig.topcaption位于knit中,尽管它要么与绘图数据不兼容,要么可能与 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 回答

?
达令说

TA贡献1821条经验 获得超6个赞

以下技巧适用于 HTML 输出。

我们可以将图形格式化为表格(仅将图像作为单元格)并将段落(图形标题所在的位置)格式化为表格标题并将其放在顶部。


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


```{css plotly-caption, echo = FALSE}

div.figure {

  display: table;

}

div.figure p {

  display: table-caption;

  caption-side: top;

}

```


查看完整回答
反对 回复 2023-09-04
  • 1 回答
  • 0 关注
  • 73 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信