briatte / ggnetwork

Geoms to plot networks with ggplot2
https://briatte.github.io/ggnetwork/
146 stars 28 forks source link

fortify.igraph() fails when both vertices and edges have a color spec #69

Open nbenn opened 3 years ago

nbenn commented 3 years ago

When both vertices and edges on an igraph object have a color specification, as

n <- 4

mat <- diag(n)
mat <- matrix(c(0, mat[-length(mat)]), nrow = n,
              dimnames = rep(list(letters[seq_len(n)]), 2))

grp <- igraph::graph_from_adjacency_matrix(mat)
igraph::E(grp)$color <- "black"
igraph::V(grp)$color <- "white"

plot(grp)

ggnetwork::ggnetwork(grp)

the data.frames corresponding to nodes/edges cannot be rbound in

https://github.com/briatte/ggnetwork/blob/814542449f503793efde2344c6977c49956edb37/R/utilities.R#L237

because earlier, resulting from the merge call,

https://github.com/briatte/ggnetwork/blob/814542449f503793efde2344c6977c49956edb37/R/utilities.R#L208

where we have

Browse[2]> nodes
          x         y name color
1 0.0000000 0.0000000    a white
2 0.3217511 0.3219269    b white
3 0.6773566 0.6771699    c white
4 1.0000000 1.0000000    d white
Browse[2]> edges
          x         y       xend      yend color
2 0.3217511 0.3219269 0.01767284 0.0176825 black
3 0.6773566 0.6771699 0.33943778 0.3395956 black
4 1.0000000 1.0000000 0.69502916 0.6948527 black
Browse[2]> merge(nodes, edges, by = c("x", "y"), all = TRUE)
          x         y name color.x       xend      yend color.y
1 0.0000000 0.0000000    a   white         NA        NA    <NA>
2 0.3217511 0.3219269    b   white 0.01767284 0.0176825   black
3 0.6773566 0.6771699    c   white 0.33943778 0.3395956   black
4 1.0000000 1.0000000    d   white 0.69502916 0.6948527   black

which then goes on to give trouble. Trying out a fix such as

https://github.com/nbenn/ggnetwork/blob/4476d6b2b78336f773ea7351f835d4166c47e6a1/R/utilities.R#L208-L213

seems to resolve my issue. Although I do not know whether this causes other problems. Let me know if you are interested in a PR.