Home > other >  Draw a graph indicating the weight of each arc
Draw a graph indicating the weight of each arc

Time:12-16

I would like to draw a small graph indicating its weights in the arcs. For example, the following code:

library(igraph)
actors <- data.frame(name=c("0","1","2", "3", "4", "5","6"))
relations <- data.frame(from=c("0", "0", "1", "2", "3", "3","4","5","4"),
                          to=c("1", "2", "3", "3", "5", "4","5","6","6"),
                        weight=c(2,6,5,8,15,10,6,6,2))
g <- graph_from_data_frame(relations, directed=F, vertices=actors)
plot(g)

draws the graph without the information about weights: enter image description here How could I insert this information on the picture?

CodePudding user response:

You can use set_edge_attr to assign weight to the width (or label) attribute, e.g.,

g %>%
  set_edge_attr(name = "width", value = relations$weight) %>%
  plot()

or

g %>%
  set_edge_attr(name = "label", value = relations$weight) %>%
  plot()

CodePudding user response:

If you want to represent edge weight as thickness you can do:

plot(g, edge.width = relations$weight)

enter image description here

If you want to use labels you can do:

plot(g, edge.label = relations$weight)

enter image description here

Of course you can combine these and tweak the size, color etc. The full list of layout parameters is in the igraph plot docs.

  • Related