【问题标题】:R Markdown and Plotly: fig.align not working with HTML outputR Markdown 和 Plotly:fig.align 不适用于 HTML 输出
【发布时间】:2017-11-09 03:02:54
【问题描述】:

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

【问题讨论】:

    标签: r r-markdown plotly


    【解决方案1】:

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

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

    【讨论】:

    • 这几乎对我有用...plot_ly 绘图居中,但跨越了整个 html 文本宽度(因此无论如何它都会居中)。但是,当我使用上面的代码时,图形标题消失了……你的代码是否发生了这种情况?
    【解决方案2】:

    正如 cmets 中所指出的,上面提出的解决方案使我的 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>
    

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

    ```{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>
    

    【讨论】:

      猜你喜欢
      • 2017-04-25
      • 2016-10-17
      • 1970-01-01
      • 1970-01-01
      • 2013-03-01
      • 2020-07-23
      • 1970-01-01
      • 2022-06-16
      • 1970-01-01
      相关资源
      最近更新 更多