Show edge attributes as label with igraph
Asked Answered
F

1

10

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?

Flexuosity answered 19/5, 2017 at 19:9 Comment(2)
There are five edges but eight attributes ; how should they be arrangedArmandarmanda
Good point! Let's say it's the sum of the wt variable for each node.Flexuosity
G
16

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.

Graner answered 19/5, 2017 at 20:35 Comment(2)
great! I didn't realize it was that easyFlexuosity
Glad I could help (I had to look it up too ;-). Please feel free to mark the answer as helpful if you feel that it was.Graner

© 2022 - 2024 — McMap. All rights reserved.