0

Let's have a data frame containing edges and their weights as follows.

> data <- data.frame(From = sample(1:10,15,replace = T),
+                    To = sample(1:10,15,replace = T),
+                    Weight = runif(15,min = 0,max = 1))
> data
   From To    Weight
1     3  1 0.4846809
2     5  5 0.3007253
3     3  9 0.4345911
4     4  6 0.2100013
5     9  9 0.6778775
6     7  9 0.6316653
7     5  3 0.2996755
8     6  3 0.4550394
9     3  9 0.3162794
10    7  1 0.7933087
11    9  2 0.3738579
12    9  4 0.9152347
13    1  3 0.6452292
14    8  3 0.3732314
15    6  8 0.9751309

I constructed, the graph using Igraph library as follows.

> g <- graph.data.frame(data,directed = F)
> g
IGRAPH a2074bf UN-- 9 15 -- 
+ attr: name (v/c), Weight (e/n)
+ edges from a2074bf (vertex names):
 [1] 3--1 5--5 3--9 4--6 9--9 9--7 3--5 3--6 3--9 7--1 9--2 4--9 3--1 3--8 6--8

I tried it a little bit and color the edges according to their weight range. Below is my code, where I colored the edges as Purple if they have a weight between 0.4 to 1. Here is my code to color the edges.

#Network Visualization
mycolor <- ifelse(E(g)$Weight>=0.04,"purple",
                   ifelse(E(g)$Weight>=0.001,"green",
                          ifelse(E(g)$Weight>=0.00001,"brown",
                                 ifelse(E(g)$Weight>=0.000007,"blue",
                      ifelse(E(g)$Weight>=0.00000001,"red","black")))))

nodes <- data.frame(id = V(g)$name, title = V(g)$name)
edges <- get.data.frame(g, what="edges")[1:2]
edges$color <- mycolor
visNetwork(nodes, edges,main = "Network Visualization")

The issue with this coloring scheme is that

  1. Group of edges has the same color i.e., the edges with a weight between 0.4 to 1 will have Purple color. However, I want to color each edge differently according to its weight.
  2. If I assign colors manually, then it will create a disturbance in the legend. Also will be difficult to interpret and find the edges with high weight.

Now, I want to visualize the network with the VisNetwork library. And want to color the edges according to their weight. I found that HeatMap coloring is best for values in Weight. Is there any way to color the edges using the HeatMap functionality? Also want to add a color tray to the legend.

Moreover,

  1. I need to add legend as follows with range 0 to 1.
  2. enter image description here
  3. Also if you have a solution using any other library, it will be appreciated.

1 Answers1

0

This is not the proper answer, but close to the solution. For the coloring, we need to generate colors according to the Weights. We can generate colors through this function.

genkolor <- function(w) {
   k <- vector()
   for (i in 1:length(w)) {
      k[i] <- rgb(1 - w[i],1 - w[i],1 - w[i])
   }
   return(k)
}

Now, assign the color to edges as follows.

nodes <- data.frame(id = V(g)$name, title = V(g)$name)
edges <- get.data.frame(g, what="edges")[1:2]
nodes$shape <- "circle"
nodes$label <- nodes$id
edges$width <- 6
edges$label <- E(g)$Weight
edges$color <- genkolor(E(g)$Weight)
visNetwork(nodes,edges,main = "Network Visualization") %>%
   visIgraphLayout(layout = "layout_nicely")

Here is the output. Where black is the max weight color and white is the min weight color.

enter image description here