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
- 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.
- 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,