0

I have an edge list in Excel that looks like this:

from   to   weighted
a      b      5
c      b      0
d      b      20
a      c      1
b      c      30
d      f      1

How can I read this edge list and visualize it as a weighted network? I currently have:

library(igraph)
library("readxl")

data <- read_excel("Sample.xlsx")
g <- graph_from_data_frame(data, directed = TRUE, vertices = NULL)
plot(g)

But the plot doesn't show the edge weights. What function should I use to visualize the weights?

user20650
  • 24,654
  • 5
  • 56
  • 91
  • To plot the thickness of the edge by weight see https://stackoverflow.com/questions/22301119/change-edge-thickness-in-igraph-plot-r-according-to-edge-attributes or add weight as labels: https://stackoverflow.com/questions/44077403/show-edge-attributes-as-label-with-igraph – user20650 Jun 16 '23 at 15:47

1 Answers1

0

U can use the plot parameters like edge.width , vertex.size, edge.color etc to visualize ur plot. Try below approach to do this

library(igraph)
library(readxl)

data <- read_excel("Sample.xlsx")
g <- graph_from_data_frame(data, directed = TRUE, vertices = NULL)

# Use ur weighted column data here
edge_weights <- E(g)$weighted
max_weight <- max(edge_weights)
min_weight <- min(edge_weights)
scaled_weights <- rescale(edge_weights, to = c(1, 10))  

plot(g, edge.width = scaled_weights, edge.color = "gray", vertex.color = "lightblue", vertex.size = 30)
  • thank you for yor answer! I try to run this but fllowing warning: > edge_weights <- E(g)$weighted > max_weight <- max(edge_weights) Warning message: In max(edge_weights) : no non-missing arguments to max; returning -Inf > min_weight <- min(edge_weights) Warning message: In min(edge_weights) : no non-missing arguments to min; returning Inf > scaled_weights <- rescale(edge_weights, to = c(1, 10)) Error in rescale(edge_weights, to = c(1, 10)) : could not find function "rescale" > – Tiya Lei Jun 19 '23 at 09:32