5

按照这里的对话,有没有办法在网格中组织输出 dygraphs?连续有一个或多个图形。

下面的代码将生成 4 个垂直排列的 dygraphs。有没有办法将它们组织成 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"))
```

当前输出截图:

在此处输入图像描述

4

1 回答 1

7

我想我想通了,不确定它是最好的解决方案,但是添加一个带有display:inline-block;属性的包装器 div 似乎效果很好。

我刚刚将这一行添加到生成每个 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) 

```

输出截图: 在此处输入图像描述

于 2018-06-24T07:29:42.297 回答