【问题标题】:Centering a dygraph plot in R Markdown在 R Markdown 中将 dygraph 图居中
【发布时间】:2018-12-08 23:15:45
【问题描述】:

我正在尝试使 dygraph 居中,但它仍保持左对齐。下面是我的最小示例,突出显示 fig.align='center' 如何与正常绘图一起工作:

---
output: html_document
---

```{r fig.align='center'}
plot(cars)
```

```{r fig.align='center'} 
library(dygraphs)
lungDeaths <- cbind(ldeaths, mdeaths, fdeaths)
dygraph(lungDeaths) %>%
dySeries("mdeaths", label = "Male") %>%
dySeries("fdeaths", label = "Female") %>%
dyOptions(stackedGraph = TRUE) %>%
dyRangeSelector(height = 20)
```

如何使 dygraph 居中对齐?

【问题讨论】:

    标签: r r-markdown knitr centering r-dygraphs


    【解决方案1】:

    dygraph 包创建 HTML 小部件,其行为与 R Markdown 中的标准图形不同,后者是静态图像。基于this answer,我们可以添加一些自定义CSS来强制HTML小部件居中对齐:

    ---
    output: html_document
    ---
    
    <style>
    .html-widget {
        margin: auto;
    }
    </style>
    
    ```{r fig.align='center'} 
    library(dygraphs)
    lungDeaths <- cbind(ldeaths, mdeaths, fdeaths)
    dygraph(lungDeaths) %>%
    dySeries("mdeaths", label = "Male") %>%
    dySeries("fdeaths", label = "Female") %>%
    dyOptions(stackedGraph = TRUE) %>%
    dyRangeSelector(height = 20)
    ```
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-08-31
      • 2017-09-10
      • 2015-12-06
      • 2019-04-05
      • 1970-01-01
      • 1970-01-01
      • 2017-10-20
      相关资源
      最近更新 更多