I am creating a visualization using visNetwork in I want someone here help me with step by step process of creating the visualization in R. How can I create a visualization of 20 nodes with edges linking them. Thank you
-
1Maybe have a look to https://datastorm-open.github.io/visNetwork/ – Rémi Coulaud May 11 '19 at 16:11
1 Answers
visNetwork relies on data frames to indicate the node name and id, as well as the edges connected to the node in question. A minimal example of 20 nodes is as follows:
library(shiny)
library(visNetwork)
nodes <- data.frame(id = 1:20, label = 1:20)
edges <- data.frame(from = c(1:20), to = c(2:20,1))
server <- function(input, output, session) {
output$myNetId <- renderVisNetwork({
visNetwork(nodes, edges)
})
}
ui <- fluidPage(
visNetworkOutput("myNetId",
height <- "400px",
width <- "600")
)
shinyApp(ui <- ui, server <- server)
Going step by step, after importing the two libraries required we then specify the node data.frame which can include a label variable if you want text to populate next to the node. We've just specified 20 nodes going from 1 to 20.
the edges variable represents an edge list, with each line in the data.frame representing a start and end point for a single edge. A data.frame that looks like:
data.frame(from = c(1,1), to = c(2,3))
... Would produce two edges from node #1 to node #2 and #3. I've set it up so each node connects to the next sequential node like a circle.
The server is a shinyApp object that uses "myNetId" as a reference to how the visNetwork should operate, while the visNetworkOutput() function in the ui acts as a canvas indicating where in the screen the network should display. You can add additional parameters to the visNetwork() function using >%> as follows:
visNetwork(nodes, edges) %>%
visPhysics(solver = "barnesHut",
minVelocity = 0.1,
forceAtlas2Based = list(gravitationalConstant = -150)) %>%
visOptions(manipulation = TRUE, highlightNearest = FALSE) %>%
visEdges(arrows = 'to')
Hope that helps!

- 821
- 1
- 5
- 17
-
Thank you for providing this answer. When I implement this solution (and change `highlightNearest` to `TRUE`), I end up with a solution in which only the edges `from` a given node get highlighted. Can you explain that? See [simplified SO question here](https://stackoverflow.com/questions/70312952/r-visnetwork-visoptions-not-highlighting-desired-edge-using-highlightnearest-in). – TMo Dec 12 '21 at 21:30