1

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 Answers1

1

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]
MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • Sorry, I'm still confused as to how these igraph objects work. Why do I only have the option of keeping some of the values of my attribute when using simplify()? The values are the values. Also, when you describe the delete_vertices() function, what do you mean by if I want to drop both vertices that are adjacent to an edge with that given property? I just want to remove the observations where that condition is true. When I try your code and attempt to plot I get this error: Error in symbols(x = coords[, 1], y = coords[, 2], bg = vertex.color, : invalid symbol coordinates – Fully Aquatic Nov 05 '19 at 21:55
  • You are using `simplify` to remove redundant edges. You need to tell igraph which one to keep, or how to combine the edges. Why are you running `simplify` in the first place? You have `comm.freq` set up as an edge attribute, not a vertex attribute. You are were trying to delete vertices using edge indexes which is a total mixup. Since you used `sample` without `set.seed` I can't exactly replicate your results. It's possible all the vertices are removed in some cases. – MrFlick Nov 06 '19 at 02:32