hierarchical edge bundle: geom_node_text visualize only the text from the connections

21 views Asked by At

I have two graphs, the first graph is taxonomic, the second graph represents a second level of connections. The perfect graph is a hierarchical edge bundle. However, I need to visualize only the text from the secon level connections My data:

hierarchy=data.frame(from=c("1","1","1","1","1.1","1.1","1.2","1.2","1.3","1.3","1.4","1.4"),
                    to=c("1.1","1.2","1.3","1.4","1.1.1","1.1.2","1.2.1","1.2.2","1.3.1","1.3.2","1.4.1","1.4.2"))
rel = data.frame(from=c("1.3.1","1.3.1","1.4.2","1.4.2"),
                 to=c("1.1.2","1.3.2","1.2.1","1.1.1"))

Normally the code to make a "hierarchical edge bundle" with "cactustree" layout is:

hierarchy_gr = as_tbl_graph(hierarchy)
vertices_hierarchy = hierarchy_gr %>% V()
from <- match( rel$from, vertices_hierarchy$name)
to <- match( rel$to, vertices_hierarchy$name)
ggraph(hierarchy_gr, 'cactustree',upright=TRUE) + 
#  geom_node_circle(aes(fill = depth), size = 0.25, alpha = 0.2) + 
  geom_conn_bundle(data = get_con(from = from, to = to), colour="red",linewidth=1,tension = 1) +
  geom_node_text(aes(label = name, size=0.5),check_overlap = TRUE) +
  theme(legend.position = "none") +
  coord_fixed()

and the result is: enter image description here

but I would something like this: enter image description here

I have no idea how to make this. I read some from stackoverflows, but maybe I am at the moment overflowed!

Thank you

1

There are 1 answers

0
Enrico Gabrielli On

I found that this works:

ggraph(hierarchy_gr, 'cactustree',upright=TRUE) + 
  geom_conn_bundle(data = get_con(from = from, to = to, n = n, attr = attr),aes(edge_color=attr,edge_width=n),tension = 1) +
  geom_node_text(aes(label = name,filter=name=="1.3.1"|name=="1.4.2", size=0.5),check_overlap = TRUE) +
  theme(legend.position = "none") +
  coord_fixed()

ok:

ggraph(hierarchy_gr, 'cactustree',upright=TRUE) + 
  geom_conn_bundle(data = get_con(from = from, to = to, n = n, attr = attr),aes(edge_color=attr,edge_width=n),tension = 1) +
  geom_node_text(aes(label = name,filter=name %in% rel$from, size=0.5),check_overlap = TRUE) +
  geom_node_text(aes(label = name,filter=name %in% rel$to, size=0.5),check_overlap = TRUE) +
  theme(legend.position = "none") +
  coord_fixed()