hierarchical edge bundle: geom_conn_bundle attribute from another data, over same structure

15 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"),
                 attr=c("e","f","g","f"))

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 I have an additional dataframe that describes the attributes of relations based on surveys. The relations, the connections, remain the same, but some connected attributes change. e.g.:

survey = data.frame(loc=c("A","A","B","C","C","D","D"),
                    from=c("1.3.1","1.3.1","1.4.2","1.3.1","1.4.2","1.3.1","1.4.2"),
                    n=c(2,5,3,4,1,3,3))

The aim is to have as an end result various hierarchical edge bundling, with the same final hierarchical structure, but displaying only the relations according to the dataframe of the 'surveys', and with the linked attributes defining e.g. the width of the connections. Such as this one: enter image description here Excuse me for not posting test codes. I'm trying, but I'm still rather in a quandary as to how it can work. So I decided to try to ask a question here.

1

There are 1 answers

1
Enrico Gabrielli On

Sorry, very simple:

rel_survey = survey %>% 
  filter(loc=="D") %>% 
  group_by(loc,from) %>% 
  summarise(n=sum(n)) %>% 
  inner_join(rel,by=join_by(from==from))
from <- match( rel_survey$from, vertices_hierarchy$name)
to <- match( rel_survey$to, vertices_hierarchy$name)
n = rel_survey$n
attr = rel_survey$attr    
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, size=0.5),check_overlap = TRUE) +
      coord_fixed()

but I have not yet managed to solve the issue of only displaying the texts of connected nodes (see hierarchical edge bundle: geom_node_text visualize only the text from the connections