在 R Markdown 中将 dygraph 图居中

Centering a dygraph plot in R Markdown

我正在尝试将 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 居中对齐?

dygraph 包创建 HTML 小部件,其行为不同于 R Markdown 中的标准图形,后者是静态图像。基于 ,我们可以添加一些自定义 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)
```