briatte / ggnet

Network visualization with ggplot2
https://briatte.github.io/ggnet/
194 stars 33 forks source link

Add weights in correct order of edge list #45

Closed vkartha closed 3 years ago

vkartha commented 3 years ago

Hi, when supplying a data.frame object of directed node edge lists, I would then like to highlight the size of the edges by a certain weight. The tutorial shows how one can add a variable representing the edge weights, but I am unable to add them in the right order (realized this when I noticed the order did not match the variable that was in the data.frame). Is there a way to ensure this?

dat <- data.frame("Item"=sample(letters[1:10],5),"Event"=sample(1:10,5),weight=runif(sample(1:4,5)))

net <- network(dat,
               directed = TRUE,
               matrix.type="edgelist")

set.edge.value(x = net,attrname = "weight",value = dat$weight)

all.equal(dat$weight,net %e% "weight") # Returns FALSE, leading to incorrect weights being used for edge thickness.

How can I ensure the order in the net object matches that of the data,.frame, so that I can also add other variables (e.g. color specifications for arrows) to match the right edge lists?.

briatte commented 3 years ago

Hi @vkartha — I am closing this issue because it is unrelated to ggnet per se, but if you want an explanation for why network::set.edge.value behaves as it does, check this Gist.