Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show edge attributes as label with igraph

I am using igraph in R for network analysis. I want to display an edge attribute on each line in the plot. An example is below

df <- data.frame(a = c(0,1,2,3,4),b = c(3,4,5,6,7))
nod <- data.frame(node = c(0:7),wt = c(1:8))
pg <- graph_from_data_frame(d = df, vertices = nod,directed = F)
plot(pg)

I want the value of the "wt" feature to show up between each node on the line, or preferably, in a little gap where the line breaks.

Is it possible to make this happen?

like image 256
user2997345 Avatar asked May 19 '17 19:05

user2997345


1 Answers

Use the parameter edge.label to assign labels of the edges, I used - probably wrong - nod$wt. Of course, you could assign other labels.

You could use the following code:

# load the package
library(igraph)

# your code
df <- data.frame(a = c(0,1,2,3,4),b = c(3,4,5,6,7))
nod <- data.frame(node = c(0:7),wt = c(1:8))
pg <- graph_from_data_frame(d = df, vertices = nod,directed = F)

# plot function with edge.label added
plot(pg, edge.label = nod$wt)

Please, let me know whether this is what you want.

like image 187
KoenV Avatar answered Oct 21 '22 21:10

KoenV