rdiagrammer

how to assign the title to the flowchart generated using DiagrammeR


could you please suggest a way to assign the title to the consort diagram generated using DiagrammeR package. Also i tried to color the boxes of all the nodes using fillcolor=blue in the node, but that does not seem to work,

library(tidyCDISC)
library(tidyverse)
library(ggalluvial)
library(DiagrammeR)


data('adsl', package='tidyCDISC')

# randomized population
pop1 <- adsl %>% rename_all(tolower) %>% filter(saffl=='Y') %>% count() %>% 
  mutate(col1=paste0('Safety Population\n(N=',n,')'))

popt <- adsl %>% rename_all(tolower) %>% filter(saffl=='Y') %>% group_by(trt01a) %>% count() %>% 
  mutate(col1=paste0(trt01a,'\n(N=',n,')'))

DiagrammeR::grViz("
              digraph box  {
               graph [layout=dot, overlap = true, fontsize = 10]
               
               node [shape=box, fontname = Helvetica, color = blue, fillcolor = red]
               
               total_population [label = '@@1']; 
               placebo [label = '@@2'];
               xanomeline_low_dose [label = '@@3'];
               xanomeline_high_dose [label = '@@4'];
              
               
               total_population  -> placebo total_population -> xanomeline_low_dose total_population -> xanomeline_high_dose 
               
                            }
              
              [1]:  pop1$col1 
              [2]:  popt$col1[1]
              [3]:  popt$col1[3]
              [4]:  popt$col1[2]
             
                  ")


Solution

  • For the title see How do you center a title for a diagram output to SVG using dot? or search the graphviz docs at https://graphviz.org. To get different fontsizes for the title and node labels, set different fontsize values in the graph and node declarations.`

    To fill you need to set style=filled in the node declaration (or the individual node instantiations); again see the docs

    Your example (without the extra packages) and code additions indicated by comments:

    library(DiagrammeR)
    
    var = 1:4
    DiagrammeR::grViz("
                  digraph box  {
                  
                        graph [label = 'Main title', # title
                               labelloc='t';         # title location at 't'op
                               layout=dot, 
                               overlap = true, 
                               fontsize = 20]        # title fontsize
                        
                        node [shape=box, 
                              fontname = Helvetica, 
                              color = blue, 
                              style = filled,        # set node style to filled 
                              fillcolor = red, 
                              fontsize = 10]         # node label fontsize
                        
                        total_population [label = '@@1']; 
                        placebo [label = '@@2'];
                        xanomeline_low_dose [label = '@@3'];
                        xanomeline_high_dose [label = '@@4'];
                        
                        total_population  -> placebo total_population -> 
                        xanomeline_low_dose total_population -> xanomeline_high_dose 
                        }
    
                       [1]:  var[1]
                       [2]:  var[2]
                       [3]:  var[3]
                       [4]:  var[4]
                     ")