0

Consider this simple example

mynodes_alt <- tibble(id = c(1,2,4,5),
                  mygroup = c(2,2,3,3))

myedges_alt <- tibble(from = c(1,1,4),
                  to = c(4,2,3),
                  power = c(3,3,3))

tbl_graph(nodes = mynodes_alt, edges = myedges_alt) 

# A tbl_graph: 4 nodes and 3 edges
#
# A rooted tree
#
# Node Data: 4 x 2 (active)
     id mygroup
  <dbl>   <dbl>
1     1       2
2     2       2
3     4       3
4     5       3
#
# Edge Data: 3 x 3
   from    to power
  <int> <int> <dbl>
1     1     4     3
2     1     2     3
3     4     3     3

As you can see, there are only 3 edges here. However, creating the network viz with ggraph generates a puzzling chart.

tbl_graph(nodes = mynodes_alt, edges = myedges_alt) %>%  
  ggraph(layout = 'grid') + 
  geom_node_point(aes(color = factor(mygroup))) +
  geom_edge_arc(aes(alpha = power, label = power)) + 
  geom_node_text(aes(label = id), repel = TRUE) +
  theme_graph()

enter image description here

What is going on? Where does that node 5 come from? It is supposed to have no edges.

Thanks!

Community
  • 1
  • 1
ℕʘʘḆḽḘ
  • 18,566
  • 34
  • 128
  • 235
  • Maybe it's because you don't have a `node == 3` but you have an edge `from = 4, to = 3`. – Rui Barradas Jun 18 '19 at 17:27
  • I dont know... I mean edge `1 -> 4` is not even present in the network chart. I am puzzled – ℕʘʘḆḽḘ Jun 18 '19 at 17:30
  • you may be right after all, but then this means its a bug in tidygraph. there is no node 3, so a fifth row is added to the nodes and missing node 3 becomes node 5. – ℕʘʘḆḽḘ Jun 18 '19 at 17:34
  • 1
    If you try `str(thegraph)`, you will see it's a list of 10 but at the 5th it throws an error, `Error in adjacent_vertices(x, i, mode = if (directed) "out" else "all") : At iterators.c:759 : Cannot create iterator, invalid vertex id, Invalid vertex id`. – Rui Barradas Jun 18 '19 at 17:35
  • nice catch. so likely a bug – ℕʘʘḆḽḘ Jun 18 '19 at 17:37
  • 1
    Yes, even when I corrected the edges tibble, and replaced 5 with 4 the error was still there. If it's a package bug you should contact the maintainer. – Rui Barradas Jun 18 '19 at 17:40

1 Answers1

0

turns out there is not bug at all! internally, the nodes in tidygraph are indexed by row order! So my ID variable is misleading.

In the edge data, the link from 1 to 4 actually means a link from the node in the first row to the node in the fourth row of the node data!

ℕʘʘḆḽḘ
  • 18,566
  • 34
  • 128
  • 235