Facet order management in ggraph

I want to plot using faces where edges differ between panels. Panels are automatically sorted alphabetically (as usual in ggplot). A simple example:

library(igraph)
library(ggraph)

g <- make_empty_graph() + 
  vertices(1:2) +
  edges(1:2, 2:1, g = c('b', 'a'))

ggraph(g, 'kk') + 
  geom_edge_link(arrow = grid::arrow()) + 
  geom_node_label(aes(label = name)) +
  facet_edges(~g)

enter image description here

This is great, node positions are preserved, but the edges vary depending on g.

However, I want to choose the display order of the faces. So, in this case, first b, then a, like me, I ordered them when creating the chart above.

In ggplotone could change the order of the factor g. However, when creating a layout, it is not displayed g:

create_layout(g, 'kk')
           x             y name ggraph.orig_index circular ggraph.index
1 -0.9021575 -1.410825e+00    1                 1    FALSE            1
2 -1.0000000  1.224606e-16    2                 2    FALSE            2

Changing the edge attributes to a coefficient manually, changing the order, but the labels are tied to a number:

g2 <- make_empty_graph() + 
  vertices(1:2) +
  edges(1:2, 2:1, g = factor(c('b', 'a'), levels = c('b', 'a')))

ggraph(g2, 'kk') + 
  geom_edge_link(arrow = grid::arrow()) + 
  geom_node_label(aes(label = name)) +
  facet_edges(~g)

enter image description here

How can I provide a custom order for faces?

+4
2

ggraph , ggplot2, . , .

, - igraph , node . PR igraph, , tidygraph, . tidygraph ggraph, .

igraph , igraph, vertex.attributes() :

vertex.attributes(graph)$factor_attr <- value

, , edge.attributes

+2

, ( ), , :

my_lab <- function(labels) {
  list(g = c('b', 'a'))
}

ggraph(g2, 'kk') + 
  geom_edge_link(arrow = grid::arrow()) + 
  geom_node_label(aes(label = name)) +
  facet_edges(~g, labeller = my_lab)

enter image description here

+4

Source: https://habr.com/ru/post/1677983/


All Articles