0

I am working with the R programming language.

I have the following graph network :

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)
edges <- data.frame(from = sample(1:20, 100, replace = TRUE), to = sample(1:20, 100, replace = TRUE))

nodes$shape <- 'circle'
edges$weight <- apply(edges, 1, function(x) mat[x["from"], x["to"]])

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 Now, I want to add "edge labels" to this network (based on the weight column):

  from to      weight
1    6 11 0.061566284
2   10 12 0.078716949
3   10  8 0.001003639
4    6 18 0.044656020
5   16  5 0.061479305
6   16 18 0.060615245

I found this link here and have been trying to adapt the code from here to add the labels : https://datastorm-open.github.io/visNetwork/edges.html

But I am having trouble doing this - I tried the following code but no labels appear:

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

Can someone please show me how to fix this?

Thanks!

stats_noob
  • 5,401
  • 4
  • 27
  • 83
  • 2
    That's an awful lot of labels to add to the plot. I'm sure this is technically feasible, but from a data viz point of view the end result will be horrendous. Is your actual network as busy as this? – Allan Cameron Apr 27 '23 at 19:07
  • @ Allan Cameron: Thank you for your reply! The actual network is only 5 nodes .... I tried to just make an arbitrary example for this problem. – stats_noob Apr 27 '23 at 19:09
  • Does this answer your question? [R: Adding Labels From Matrix to a Graph](https://stackoverflow.com/questions/76116391/r-adding-labels-from-matrix-to-a-graph) – mrhellmann Apr 27 '23 at 19:09
  • @ mrhellmann: thank you foryour reply! in this question that you linked, I have since made progress and have figured out how to add information from the matrix to the network data frame...now I am hoping that with this improvement, the question might become easier... – stats_noob Apr 27 '23 at 19:41
  • 1
    @stats_noob. If you don’t show the new status of your puzzlement there not going to be resolution in the minds of your viewers. Use [edit] facilities. – IRTFM Apr 28 '23 at 01:10

2 Answers2

1

I think I might have answered your question in a previous post: R: Understanding Double Lists In The Same Statement

Take the following line:

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

and change it to:

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

The edges data frame needs to have a label variable which is a character, not a numeric.

Jay Achar
  • 1,156
  • 9
  • 16
  • @ Jay Achar: Thank you so much for your answer! I have accepted it as the official answer - I will post the entire code based on your answer in case anyone wants the full code in the future! – stats_noob Apr 28 '23 at 14:20
0

Full code using the answer provided by Jay Achar (in case anyone wants to copy/paste start to finish):

 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)
 edges <- data.frame(from = sample(1:20, 100, replace = TRUE), to = sample(1:20, 100, replace = TRUE))
 
 nodes$shape <- 'circle'
 edges$weight <- apply(edges, 1, function(x) mat[x["from"], x["to"]])
 
 nodes$shape <- 'circle'
 edges$weight <- apply(edges, 1, function(x) mat[x["from"], x["to"]])
 edges$label <- as.character(apply(edges, 1, function(x) mat[x["from"], x["to"]]))
 

 nodes$label = paste("Node", 1:20)
 
 
 network <- visNetwork(nodes, edges) %>%
     visEdges(arrows = list(to = list(enabled = TRUE))) %>%
     visIgraphLayout(layout = "layout_in_circle") 
 
 network
stats_noob
  • 5,401
  • 4
  • 27
  • 83