0

I am working with the R programming language.

In a previous question (R: Adding Labels From Matrix to a Graph), I learned how to make the following network graph:

library(tidyverse)
library(visNetwork)
library(htmlwidgets)


set.seed(123)
mat <- matrix(runif(19*20), nrow = 19, ncol = 20)
mat <- t(apply(mat, 1, function(x) x/sum(x)))
mat <- rbind(mat, c(rep(0, 19), 1))


nodes <- data.frame(id = 1:20, 
                    label = paste("Node", 1:20))

edges <- data.frame(from = sample(1:20, 100, replace = TRUE), 
                    to = sample(1:20, 100, replace = TRUE),
                    label = paste("Edge", 1:100))                          

nodes$shape <- 'circle'

# create the network graph
network <- visNetwork(nodes, edges) %>%
  visEdges(arrows = list(to = list(enabled = TRUE))) %>%
  visIgraphLayout(layout = "layout_in_circle") 

network

enter image description here

My Question: I am trying to understand how this command works list(to = list(enabled = TRUE))

For example, suppose I make the following modification:

set.seed(123)
mat <- matrix(runif(19*20), nrow = 19, ncol = 20)
mat <- t(apply(mat, 1, function(x) x/sum(x)))
mat <- rbind(mat, c(rep(0, 19), 1))


nodes <- data.frame(id = 1:20, 
                    label = paste("Node", 1:20))


edges <- data.frame(from = sample(1:20, 100, replace = TRUE), 
                    to = sample(1:20, 100, replace = TRUE))

edges$label <- apply(edges, 1, function(x) mat[x["from"], x["to"]])

network <- visNetwork(nodes, edges) %>%
  visEdges(arrows = list(to = list(enabled = TRUE))) %>%
  visIgraphLayout(layout = "layout_in_circle") 

enter image description here

Suddenly I lose the edge labels on the graph?

Can someone please help me understand how the list(to = list(enabled = TRUE)) statement works and why the modification I made makes me lose the edge labels? Is there a way to prevent this from happening?

Thanks!

stats_noob
  • 5,401
  • 4
  • 27
  • 83

1 Answers1

1

I'm no expert on the visNetwork package, but it seems to be based on vis.js which is documented here.

In your second example, the loss of the labels is not related to list(to = list(enabled = TRUE)), but is due to the fact that the newly calculated labels are numerical rather than characters.

To fix this, you can change this line:

edges$label <- apply(edges, 1, function(x) mat[x["from"], x["to"]])

To this:

edges$label <- as.character(apply(edges, 1, function(x) mat[x["from"], x["to"]]))

You would need to format the output to improve the appearance, but you get the idea.

In this code chunk:

network <- visNetwork(nodes, edges) %>%
  visEdges(arrows = list(to = list(enabled = TRUE))) %>%
  visIgraphLayout(layout = "layout_in_circle") 

The list(to = list(enabled = TRUE))) is designed to replicate the javascript options that can be passed in vis.js - see the documentation here.

Find the reference for arrows.to.enabled and you will see the description:

Toggle the arrow on or off. This option is optional, if undefined and the scaleFactor property is set, enabled will be set to true.

The JS object is converted to a list in R which is why the syntax is slightly different. Once you see them side-by-side, the structure can be easy to understand.

Jay Achar
  • 1,156
  • 9
  • 16