R Markdown和Plotly:fig.align无法使用HTML输出

Joh*_*tud 5 r r-markdown plotly

fig.align 没有使用R Markdown HTML输出和情节,我寻求一些帮助!

这是一个MWE:

---
title: "Untitled"
output: html_document
editor_options: 
chunk_output_type: console
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, fig.align = "center")
library(plotly)
```


```{r}
plot_ly(
  x = c("giraffes", "orangutans"),
  y = c(20, 14),
  name = "SF Zoo",
  type = "bar")
```


```{r, fig.align = 'right'}
plot_ly(
  x = c("giraffes", "orangutans"),
  y = c(20, 14),
  name = "SF Zoo",
  type = "bar")
```

```{r, fig.align = 'center'}
plot_ly(
  x = c("giraffes", "orangutans"),
 y = c(20, 14),
 name = "SF Zoo",
type = "bar")
```
Run Code Online (Sandbox Code Playgroud)

Mar*_*zer 8

与普通图不同,图形图形具有不同的HTML设置.你可以做的是使用闪亮的包,并在每个情节周围包裹另一个div容器:

library(shiny)
div(plot_ly(
x = c("giraffes", "orangutans"),
y = c(20, 14),
name = "SF Zoo",
type = "bar"), align = "right")
Run Code Online (Sandbox Code Playgroud)

  • 这几乎对我有用......“plot_ly”图居中但跨越整个 html 文本宽度(因此无论如何它都会居中)。然而,当我使用上面的代码时,图形标题消失了......这发生在你身上吗? (2认同)

小智 5

正如评论中指出的,上面提出的解决方案使我的plotly绘图跨越了整个 html 文本宽度。以下内容对我有用,而不会延长情节:

---
title: "Untitled"
output: html_document
chunk_output_type: console
---

```{css, echo=FALSE}
.center {
  display: table;
  margin-right: auto;
  margin-left: auto;
}
```

<div class = 'center'>
```{r, echo = F, message = F}
library(plotly)
plot_ly(
  x = c("giraffes", "orangutans"),
  y = c(20, 14),
  name = "SF Zoo",
  type = "bar")
```
</div>
Run Code Online (Sandbox Code Playgroud)

为了还显示被剪断的代码,这个解决方案有点难看,因为被剪断的代码也会居中。我使用以下解决方法:

```{r plot1, results = F, message = F}
library(plotly)
plot_ly(
  x = c("giraffes", "orangutans"),
  y = c(20, 14),
  name = "SF Zoo",
  type = "bar")
```

<div class = 'center'>
```{r plot1, echo = F}
```
</div>
Run Code Online (Sandbox Code Playgroud)