关注话题 here , 有没有办法在网格中组织输出 dygraphs?将一个或多个图表排成一行。
下面的代码将生成 4 个垂直排列的 dygraph。
有没有办法在 4x4 网格中组织它们?
我尝试使用 tags$div
但它将所有图形包装在一个 div 中。
有没有办法应用 CSS 属性,例如 display: inline-block;
到每个 dygraph 小部件?或任何其他更好的方法?
```{r}
library(dygraphs)
library(htmltools)
makeGraphs = function(i){
dygraph(lungDeaths[, i], width = 300, height = 300, group = "lung-deaths")%>%
dyOptions(strokeWidth = 3) %>%
dyRangeSelector(height = 20)
}
lungDeaths <- cbind(mdeaths, fdeaths, ldeaths, mdeaths)
res <- lapply(1:4, makeGraphs )
htmltools::tagList(tags$div(res, style = "width: 620px; padding: 1em; border: solid; background-color:#e9e9e9"))
```
当前输出截图:
最佳答案
我想我想通了,不确定它是最好的解决方案,但是添加了一个带有 display:inline-block;
的包装器 div。属性(property)似乎运作得很好。
我刚刚将此行添加到生成每个 dygraph 的函数中:
htmltools::tags$div(theGraph, style = "padding:10px; width: 250px; border: solid; background-color:#e9e9e9; display:inline-block;")
所以更新后的代码如下所示:
```{r graphs}
library(dygraphs)
library(htmltools)
makeGraphs = function(i){
theGraph <- dygraph(lungDeaths[, i], width = 400, height = 300, group = "lung-deaths")%>%
dyOptions(strokeWidth = 3) %>%
dyRangeSelector(height = 20)
htmltools::tags$div(theGraph, style = "padding:10px; width: 450px; border: solid; background-color:#e9e9e9; display:inline-block;")
}
lungDeaths <- cbind(mdeaths, fdeaths, ldeaths, mdeaths)
res <- lapply(1:4, makeGraphs )
htmltools::tagList(res)
```
输出截图:
关于r - 在 R-Markdown 中的网格上显示多个 dygraph,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51007551/