R visNetwork:多图形布局?

时间:2017-04-20 12:23:48

标签: r layout graph shiny igraph

我想将两个visNetwork图并排放置以进行视觉比较。使用带有par()或layout()的igraph可以进行多绘图定位。有没有办法为visNetwork做到这一点?解决方案/ kludges(包括RShiny等)是可接受的答案 - 无论是什么工作提供并排的visNetwork显示。请注意,ID号等重叠,因此将两个网络放在同一个图表中将是我希望避免的大量数据操作。

这是我想要做的事情的一个例子。

library(visNetwork)

# Network 1
nodes1 <- data.frame(id = 1:3)
edges1 <- data.frame(from = c(1,1), to = c(2,3))

# Network 2
nodes2 <- data.frame(id = 1:4)
edges2 <- data.frame(from = c(1,1,2,2), to = c(2,3,4,3))

# Plot both networks side-by-side?
par(mfrow=c(1,2))  # Want something like this (does not work)
visNetwork(nodes1, edges1) %>% visEdges(arrows = 'from')  
visNetwork(nodes2, edges2) %>% visEdges(arrows = 'from')

干杯, 添

2 个答案:

答案 0 :(得分:5)

如果给予完全自由,我会选择flexdashboard。这是一个方便的rmarkdown模板,用于创建交互式仪表板,从简单快速的复杂到复杂。

install.packages('flexdashboard')

然后从模板Rmd创建新的'Flex Dashboard',或将以下内容复制到新的.Rmd文件中

---
title: "DASHBOARD"
output: 
    flexdashboard::flex_dashboard:
        orientation: rows
---

```{r, echo=FALSE}
library(visNetwork)

# Network 1
nodes1 <- data.frame(id = 1:3)
edges1 <- data.frame(from = c(1,1), to = c(2,3))

# Network 2
nodes2 <- data.frame(id = 1:4)
edges2 <- data.frame(from = c(1,1,2,2), to = c(2,3,4,3))

```

Row 
-------------------------------------

### Chart 1

```{r}
visNetwork(nodes1, edges1) %>% visEdges(arrows = 'from')
```


### Chart 2

```{r}
visNetwork(nodes2, edges2) %>% visEdges(arrows = 'from')
```

请注意,由于布局是响应式的,Rstudio中的“查看器”窗格中的默认视图会垂直堆叠图形,但是扩展窗格或在外部浏览器中打开它会将它们并排显示。

enter image description here

答案 1 :(得分:1)

通过一个app.R文件使用Shiny的解决方案:

library(shiny)
library(visNetwork)

ui <- fluidPage(
  fluidRow(
    column(6,
           visNetworkOutput('vis1')),
    column(6,
           visNetworkOutput('vis2'))
  )
)

server <- function(input, output) {

  nodes1 <- data.frame(id = 1:3)
  edges1 <- data.frame(from = c(1,1), to = c(2,3))

  nodes2 <- data.frame(id = 1:4)
  edges2 <- data.frame(from = c(1,1,2,2), to = c(2,3,4,3))


  output$vis1 <- renderVisNetwork(
    {
      visNetwork(nodes1, edges1) %>% 
        visEdges(arrows = 'from') %>% 
        return
    })
  output$vis2 <- renderVisNetwork(
    {
      visNetwork(nodes2, edges2) %>% 
        visEdges(arrows = 'from') %>% 
        return
    })
}

shinyApp(ui = ui, server = server)