在对话here之后,有没有办法在网格中组织输出图谱?连续放置一个或多个图形。

下面的代码将生成4个垂直排列的字母图。
有没有一种将它们组织在4x4网格中的方法?

我尝试使用tags$div,但将所有图形包装在一个div中。
有没有一种方法可以将CSS属性(例如display: inline-block;)应用于每个音标控件?或其他更好的方法?

```{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"))
```

当前输出屏幕截图:

r - 在R-Markdown中的网格上显示多个图表-LMLPHP

最佳答案

我想我已经弄清楚了,不确定最好的解决方案,但是添加带有display:inline-block;属性的wrapper div似乎效果很好。

我只是将这一行添加到生成每个音标的函数中:

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中的网格上显示多个图表-LMLPHP

关于r - 在R-Markdown中的网格上显示多个图表,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51007551/

10-14 15:56
查看更多