Creating a dynamic force network with the visNetwork package

The visNetwork package is one among the most popular packages in the R community, mainly because it lets you display networks and interact with them without having to invest too much time.

This recipe will get you up and running with this package, showing you all that you need to know to start exploring your network in a fully interactive way.

Getting ready

In order to get started with this recipe, you will need to install and load the visNetwork package to actually produce your network visualizations.

We will also use the jsonlite package to parse the dataset we will use from the JSON format to the data frame:

install.packages(c("visnetwork","jsonlite"))
library(visnetwork)
library(jsonlite)

How to do it...

  1. Download the dataset from the Web and assign it to the Energy object:
    URL <- paste0("https://cdn.rawgit.com/christophergandrud/networkD3/",
      "master/JSONdata/energy.json")
    Energy <- jsonlite::fromJSON(URL)
  2. Now create the nodes object:
    nodes <- data.frame(label = Energy$nodes$name , id = 1:length(Energy$nodes$name))
  3. Create the edges object:
    edges <- data.frame( from = Energy$links$source,
      to = Energy$links$target,
      value = Energy$links$value)
  4. Next, visualize the network:
    visNetwork(nodes,edges)

    This will result in the following interactive visualization:

    How to do it...
  5. Add explicative tooltips to your edges:
    edges <- data.frame( from = Energy$links$source,
      to = Energy$links$target,
      value = Energy$links$value,
      title = paste0("flow = ", Energy$links$value))

    This will make the following tooltip appear when hovering over the edge.color:

    How to do it...
  6. Next, we will add a legend to the plot.

    Adding a legend will require you to introduce groups in your data. Therefore, we will need to modify the nodes object:

    nodes <- data.frame(label = Energy$nodes$name ,
      group = c("Group A", "Group B"),
      id = c(1:length(Energy$nodes$name)))

    A legend will then be added when you run the following code:

    visNetwork(nodes,edges) %>%
      visLegend() 
    How to do it...
  7. We will now add navigation buttons:

    Adding a navigation button requires that you specify the visInteraction parameter:

    visNetwork(nodes,edges) %>%
      visInteraction(navigationButtons = TRUE)

    Let's take a look at the following image:

    How to do it...

How it works...

In step 1 we download the dataset from the Web and assign it to the Energy object. The dataset we used shows data for energy flows right from their production to their consumption or waste.

The dataset is composed of a nodes data frame and a links data.frame, as we can clearly see when we run str(Energy):

List of 2
 $ nodes:'data.frame': 48 obs. of  1 variable:
  ..$ name: chr [1:48] "Agricultural 'waste'" "Bio-conversion" "Liquid" "Losses" ...
 $ links:'data.frame': 68 obs. of  3 variables:
  ..$ source: int [1:68] 0 1 1 1 1 6 7 8 10 9 ...
  ..$ target: int [1:68] 1 2 3 4 5 2 4 9 9 4 ...
  ..$ value : num [1:68] 124.729 0.597 26.862 280.322 81.144 …

The nodes data frame contains label of our nodes, while the links data frame basically contains the connection between our nodes weighted by an amount (value attribute) representing the flow going from one node to another.

To apply this recipe to your data, you should have them arranged in two distinct data frames with the following structure:

  • The nodes data frame:
    • Nodes: This is a vector of all your nodes' names, with no duplications
  • The links data frame:
    • From: This is a numeric column showing the first node of every connection in your diagram. Let's say that if you want to introduce a connection between the first and the third nodes defined with Nodes, you should write 0 here (and 3 within the to argument) as shown in this example; be aware that first node has value 0 and not 1
    • To: This is a numeric column showing the end of every connection within your diagram
    • Weight: This is the value of your connection, meaning how much of your flow passes through this connection

The second data frame is useful for you to underline, in that the second data frame is rightly named hedge list, where each observation represents a hedge of your network.

In step 2 we create nodes object. The nodes object required by the visNetwork() function must be composed of a numeric ID and a character label. That is why we create a nodes data.frame using the name attribute from the Energy object and adding a sequence from 1:length(Energy$nodes$name) as the IDs.

In step 3 we create the edges object. In a way analogous to what we have done with nodes, we will now create edges for our network, taking data from the links data.frame within the Energy object.

In step 4 we visualize your network. This is where your data visualization comes to life. Running visNetwork on your previously created visNetwork object will make an interactive plot showing up in your RStudio viewer pane.

Nodes composing the network can be fully rearranged by dragging and dropping, giving you the possibility to have the clearest possible picture of your data.

This is not exactly like making the Mosè talk, but we are getting closer.

In step 5 we explicative tooltips to your edges. Tooltips are added to the plot, giving a title column within the edges data frame. This attribute will automatically be recognized as a tooltip source by the visNetwork() function that we call to visualize our network.

In step 6 we add a legend. The visLegend() function is piped into visInteraction using the %>% operator, which is made available in R by the magrittR package that produces a typical command-line pipe functionality into the R language.

In step 7 we add navigation buttons. Especially when dealing with complex and heavily populated networks, adding navigation buttons will be of great help.

These tools will give the ability to zoom in to the plot, making it possible for us to focus on particular communities within the network and fully understand relationships among their members.

There's more...

Since visNewtork is a really popular package, you will find plenty of resources if you surf the Web.

Nevertheless, if you are interested in deepening your knowledge about this instrument, I warmly suggest that you look for the official documentation on its website (http://dataknowledge.github.io/visNetwork/), which will give you a complete and up-to-date view of its functionalities.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset