Search code examples
rdiagrammer

How do you resize grViz object in the DiagrammeR package


Am struggling to resize the grViz object in the DiagrammeR package. I would want for instance the object of to be of size 18cm wide and a height of 14cm. When I try the height and width properties in the package, I don't seem to get the right measurement. Or alternatively is there a way to save the grViz as an image so one can just pull that image in the Rmarkdown?

Here is code:

---
title: "Flowchart on Trial"
author: "Moses Otieno"
date: "25/05/2021"
output: word_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
library(DiagrammeR) # Flowchart
```


```{r test}
nodedata2 <- list(a = 1000, b = 940, c=900, d= 500, e=400, f = 230, g = 260, h = 10, i = 180, j = 190, k = 30 )

 grViz("digraph flowchart {
      # node definitions with substituted label text
      node [fontname = Helvetica, shape = rectangle]        
      tab1 [label = '@@1']
      tab2 [label = '@@2']
      tab3 [label = '@@3']
      tab4 [label = '@@4']
      tab5 [label = '@@5']
      tab6 [label = '@@6']
      tab7 [label = '@@7']
      tab8 [label = '@@8']
      tab9 [label = '@@9']
      tab10 [label = '@@10']
      tab11 [label = '@@11']


      # edge definitions with the node IDs
      tab1 -> tab2;
      tab2 -> tab3;
      tab3 -> {tab4 tab5}; 
      tab4 -> {tab6 tab7 tab8};
      tab5 -> {tab9 tab10 tab11};

      }

      [1]: paste('Sampled n=', nodedata2$a,  'participants')
      [2]: paste('Contacted ', nodedata2$b)
      [3]: paste('Enrolled n=', nodedata2$c)
      [4]: paste('Male  n=',nodedata2$d)
      [5]: paste('Female n=',nodedata2$e) 
      [6]: paste('Drug A n=', nodedata2$f)
      [7]: paste('Drug B n= ',  nodedata2$g)
      [8]: paste('Refusals n=', nodedata2$h)
      [9]: paste('Drug A n=', nodedata2$i)
      [10]: paste('Drug B n=', nodedata2$j)
      [11]: paste('Refusals n=', nodedata2$k)
      
      ")




```

```

Solution

  • You can save it as an image using -

    library(rsvg)
    library(DiagrammeRsvg)
    
    plot %>% export_svg %>% charToRaw %>% rsvg_png("graph.png")
    

    where plot is the grViz diagram. You can also specify height and width in rsvg_png.