Add annotations to visNetwork plot of separate paths

30 views Asked by At

Using the following data frames:

sample_edgelist <- tibble(index = c(1,1,1,1,2,2,2,2,3,3,3,3),
                          from = c("1-E050", "1-X050", "1-C058", "1-D052", "2-E050", "2-X050", 
                                   "2-C058", "2-E051", "3-E050", "3-X050", "3-C058", "3-H058"),
                          to = c("1-X050", "1-C058", "1-D052", "1-E030", "2-X050", 
                                 "2-C058", "2-E051", "2-E030", "3-X050", "3-C058", "3-H058", "3-E030"),
                          group = c("E050", "E050", "E050", "E050", "E050", "E050", 
                                    "E050", "E050", "E050", "E050", "E050", "E050"))
sample_nodelist <- tibble(id = c("1-E050", "1-X050", "1-C058", "1-D052", 
                                 "1-E030", "2-E050", "2-X050", "2-C058", "2-E051", "2-E030", "3-E050", 
                                 "3-X050", "3-C058", "3-H058", "3-E030"), 
                          label = c("Hypertension", "Other Minor Acute (incl general symptoms)", 
                                    "Acute ENT, Upper Respiratory Condition (incl. Colds, Croup)",
                                    "Minor acute respiratory (incl cough, bronchitis)", "CAD/Arrhythmia w/o heart failure",
                                    "Hypertension", "Other Minor Acute (incl general symptoms)", 
                                    "Acute ENT, Upper Respiratory Condition (incl. Colds, Croup)",
                                    "Signs, Symptoms Cardiovascular System (incl chest pain, palpitations)",
                                    "CAD/Arrhythmia w/o heart failure", "Hypertension", "Other Minor Acute (incl general symptoms)",
                                    "Acute ENT, Upper Respiratory Condition (incl. Colds, Croup)",
                                    "Neuromuscular Signs & Symptoms", "CAD/Arrhythmia w/o heart failure"), 
                          color = c("#65a495", "#bfd8e9", "#6ba5cd", "#0d2637", "#418bbf", "#65a495", "#bfd8e9", 
                                    "#6ba5cd", "#040c12", "#418bbf", "#65a495", "#bfd8e9", "#6ba5cd", "#d4e5f0", "#418bbf"), 
                          shape = c("dot","dot", "dot", "dot", "dot", "dot", "dot", "dot", "dot", "dot", "dot", "dot", "dot", "dot", "dot"), 
                          value = c(25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25), 
                          font_size = c(24,24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24))

With this code:

library(tidyverse)
library(visNetwork)
visgraph <-  visNetwork(sample_nodelist, sample_edgelist, directed=T)

visgraph %>%
  visEdges(arrows = "to",
           font = list(size = 15, vadjust = -10)) %>%
  visNodes(color = list(background = sample_nodelist$color, border = "#2c7fb8"), 
           font = fontlist, 
           widthConstraint = wcon) %>%
  visHierarchicalLayout(direction = "LR",
                        sortMethod = 'directed',
                        shakeTowards= 'leaves',
                        levelSeparation = spacebetweennodes, #150,
                        nodeSpacing = 60,
  )

I can create three distinct progression paths using visNetwork.

enter image description here

What I'm interested in doing, is adding an annotation to the beginning that would reflect the ranking/score of each pathway. In the sense of the plot looking like:

Rank 1 (Score 19,597) FIRST NODE -> SECOND NODE etc for that pathway

Rank 2 (Score 19,582) FIRST NODE -> SECOND NODE etc for the rest of the plot

I have a data frame with the rank/count information

sample_index <- tibble(count = c(19597, 19582, 19555), position = 1:3)

My issue is I have no idea how to approach doing what I want, as I have figured out how to label the edges with frequency info etc, but the way I'm using visNetwork (to show individual networks instead of the whole) doesn't seem to lend itself to this.

0

There are 0 answers