1
votes

I am trying to do something similar to this and this post. I have an igraph object and want to remove vertices(arrows) based on an values in a column of the edges dataframe, color the edges(circles) by a group, and change the line/arrow size based on the same column in the edges dataframe. Here is some reproducible code that looks exactly like my data:

  # Data
edges <- data.frame(
  "agency.from" = c(rep("a",4),rep("b",4),rep("c",4),rep("d",4)),
  "agency.to" = c(rep(c("a","b","c","d"),4)),
  "comm.freq" = sample(0:5,16, replace=TRUE))

nodes <- data.frame(
  "agency" = c("a","b","c","d"),
  "group" = c("x", "y", "x", "y"),
  "state" = c("i", "j", "j", "i"))

# make igraph object
net <- graph_from_data_frame(d=edges, vertices=nodes, directed=T)
plot(net)

# remove loops
net2 <- simplify(net, remove.multiple = T, remove.loops = T)
plot(net2)

Which gives me: this

# remove vertices where communication frequency is 1 and 0
net3 <- delete.vertices(net2, which(E(net2)$comm.freq == 1))
net4 <- delete.vertices(net3, which(E(net2)$comm.freq == 0))

plot(net4)

Which does not change the plot at all

Then I try to change the colors and sizes:

# color edges by group
colrs <- c("gray50", "tomato")
V(net4)$color <- colrs[V(net4)$group]
plot(net4)

# make size of arrow based on communication frequency
plot(net4, edge.width = E(net4)$comm.freq * 5, edge.arrow.size = E(net4)$comm.freq)

And still nothing changes

I followed the code provided in the other posts and I'm just really confused why nothing will work.

Any help is much appreciated!

1

1 Answers

1
votes

The simplify() function removed your edge attributes. You need to specify how you want those values to be preserved when simplifying your graph. If you just want to keep the first possible value, you can do

net2 <- simplify(net, remove.multiple = T, remove.loops = T, edge.attr.comb=list("first"))

And then you use delete.vertices but you are passing indexes for edges, not vertices. If you want to drop both vertices that are adjacent to an edge with that given property, it should look more like

net3 <- delete_vertices(net2, V(net2)[.inc(E(net2)[comm.freq==1])])
net4 <- delete_vertices(net3, V(net3)[.inc(E(net3)[comm.freq==0])])

And then for the colors you have values like "x" and "y" for group, but you are indexing into the colrs vector which has no idea what "x" and "y" correspond to. It would be better to use a named vector. For example

colrs <- c(x="gray50", y="tomato")
V(net4)$color <- colrs[V(net4)$group]