问题
I would like to position two visNetwork plots side-by-side for visual comparison. Multi-plot positioning is possible using igraph with par() or layout(). Is there a way to do this for visNetwork? Work-arounds/kludges (including RShiny, etc.) are acceptable answers - whatever works to provide a side-by-side visNetwork display. Note that ID numbers etc. overlap, so putting both networks into the same graph would be a lot of data manipulation that I wish to avoid.
Here is an example of the type of thing I am trying to do.
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')
Cheers, Tim
回答1:
If complete liberty is given I'd go with flexdashboard
. It's a handy rmarkdown
template to create interactive dashboards, from simple and quick ones to elaborate and complex.
install.packages('flexdashboard')
Then either create a new Rmd
from template 'Flex Dashboard'
, or copy the following into a new .Rmd
file
---
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')
```
Note that as the layout is responsive the default view in the Viewer pane in Rstudio stack the graphs vertically, but extending the pane or opening it in an external browser shows them side by side.
回答2:
Solution using Shiny through one app.R
file:
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)
来源:https://stackoverflow.com/questions/43519451/r-visnetwork-multiple-graph-layout