在R-Markdown中的网格上显示多个图表 [英] Displaying multiple dygraphs on a grid in R-Markdown

查看:341
本文介绍了在R-Markdown中的网格上显示多个图表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在对话之后在这里,是否存在一种在网格中组织输出图的方法?连续放置一个或多个图形.

下面的代码将生成4个垂直排列的字形. 有没有办法将它们组织在4x4网格中?

我尝试使用tags$div,但是它将所有图形包装在一个div中. 有没有一种方法可以将诸如display: inline-block;之类的CSS属性应用于每个音标小部件?或其他更好的方法?

```{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似乎效果很好.

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

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) 

```

输出截图:

Following the conversation here, is there a way to organize the output dygraphs in a grid? To Have one or more graph in a row.

The code below would generate 4 dygraphs arranged vertically. Is there a way to organize them in a 4x4 grid?

I tried using tags$div but it wraps all the graphs in one div. Is there a way to apply a CSS property such as display: inline-block; to each dygraph widget? or any other better method?

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

Current output screenshot:

解决方案

I think I figured it out, not sure its the best solution, but adding a wrapper div with a display:inline-block; property seems to work quite well.

I just added this line to the function that generates each dygraph:

htmltools::tags$div(theGraph, style = "padding:10px; width: 250px; border: solid; background-color:#e9e9e9; display:inline-block;")

so the updated code looks like this:

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

```

Output Screenshot:

这篇关于在R-Markdown中的网格上显示多个图表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆