在R Markdown中将笔画图居中

时间:2018-12-08 23:15:45

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

我正在尝试使笔画居中,但它保持左对齐。以下是我的最小示例,重点介绍了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)
```

enter image description here

如何使笔形图居中对齐?

1 个答案:

答案 0 :(得分:0)

图表程序包创建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)
```

enter image description here