Home > Blockchain >  Changing edge color based on attribute
Changing edge color based on attribute

Time:12-09

I'm working on the visual representation of a network on R software, using the igraph package.

I have a data set with links between all the nodes and, for each link/edge, the district that they are assign to.

So, I would like to change the edge color of each edge, based on each district they are assign to. The table above shows the structure of the table.

nodei nodej depot1 depot2
4 5 1 0

In this case the link (4-5) is assigned to depot1, so the edge color of the edge should be green, for example.

CodePudding user response:

Here is an exmaple that edges associated with 1 will be colored in "red", and "green" otherwise:

g <- make_ring(5)
g %>%
  set_edge_attr(
    name = "color",
    value = c("green", "red")[1   (rowSums(ends(., E(.)) == "1") > 0)]
  ) %>%
  plot()

In your case, you could replace "1" by "depot1" and have a try.

enter image description here

CodePudding user response:

I tried to understand your question, I created a node type to color the edges and nodes.

library(igraph)

data <- read.table(text = "
N     D  type
1     6 A
3     7 B
7     8 A
4     5 B
7    10 A
4     6 B
1     7 A
6     8 B
7     9 B
6     10 A ", header=T )

nodes <- data.frame(id=unique(c(data$N, data$D)) )
nodes$type <- c("A","B") # this if for the layout
nodes$x <- c(1,3,7,4,7, 4,  1,6,7,6) 
nodes$y <- c(6,7,8,5,10,,5, 7,8,9,10)
nodes
                 
G <- graph_from_data_frame(dd, vertices = nodes ) 

V(G)$color <- ifelse( V(G)$type == "A",  "red", "green")
E(G)$color <- ifelse( E(G)$type == "A", "red","green")

edge_attr(G)
vertex_attr(G)
plot(G)

enter image description here

  • Related