Hurlikus
Hurlikus

Reputation: 419

How to get rid of white space beneath flowchart made with DiagrammeR and knitted to pdf?

What I want to do is to create a basic flowchart with DiagrammeR within a code chunk in my R Markdown file and make it referencable (if possible like I did with my ggplot2 figures -> fig.cap = " " in the code chunk header).

My code so far:

```{r flowchart-ablauf, fig.cap = "Ablauf der Datenverarbeitung", fig.align = 'right', echo = FALSE}
DiagrammeR::grViz(diagram = "
              digraph rmarkdown {

              graph [nodesep = 1.5, ranksep = 1.5]                  

              node [shape = box, fontname = Helvetica, fontsize = 80, penwidth = 6, fixedsize = true, width = 16.5, height = 3]
                'Verbrauchsdaten von der\nSpitalpharmazie (Excel-Tabelle)'; 'Belegzahlen vom Ressort\nFinanzen (Excel-Tabelle)'; 

              node [shape = box, fontname = Helvetica, fontsize = 80, penwidth = 6, fixedsize = true, width = 20.5, height = 3]
              'Import der Daten aus Excel ins\nMicrosoft Access (Datenbanksoftware)'; 'Zusammenführen der Informationen und\nerstellen neuer, berechneter Tabellen';
              'Export der neu erstellten Tabellen\nin Form von Excel-Tabellen'; 'Berechnung und grafische Darstellung\nder Grafiken und Tabellen'

              node [shape = box, fontname = Helvetica, fontsize = 80, penwidth = 6, fixedsize = true, width = 20.5, height = 2]
              'Import der neuen Tabellen in R';

              #Pfeile
              edge [arrowhead = vee, arrowsize = 4, penwidth = 6]
              'Verbrauchsdaten von der\nSpitalpharmazie (Excel-Tabelle)' -> 'Import der Daten aus Excel ins\nMicrosoft Access (Datenbanksoftware)';
              'Belegzahlen vom Ressort\nFinanzen (Excel-Tabelle)' -> 'Import der Daten aus Excel ins\nMicrosoft Access (Datenbanksoftware)';
              'Import der Daten aus Excel ins\nMicrosoft Access (Datenbanksoftware)' -> 'Zusammenführen der Informationen und\nerstellen neuer, berechneter Tabellen';
              'Zusammenführen der Informationen und\nerstellen neuer, berechneter Tabellen' -> 'Export der neu erstellten Tabellen\nin Form von Excel-Tabellen'
              'Export der neu erstellten Tabellen\nin Form von Excel-Tabellen' -> 'Import der neuen Tabellen in R'
              'Import der neuen Tabellen in R' -> 'Berechnung und grafische Darstellung\nder Grafiken und Tabellen'
              }
              ")

```

How it looks after beeing knitted to pdf: enter image description here

I have a couple of questions:

  1. Why do I have to make the fontsize so big to make it readable? I mean it is at 80 and it's still smaller than the fontsize of the text which set to 11pt in the yaml header?!
  2. Why is the "fig. align = 'right'" not doing anything?
  3. Most importantly: How can I get rid of the huge white space beneath the flowchart (between the flowchart and the figure caption)?

Upvotes: 2

Views: 772

Answers (1)

Hurlikus
Hurlikus

Reputation: 419

I've found a working solution here

That is how my code ended up looking:

---
 header-includes:
  - \usepackage{wrapfig}
  - \usepackage{lipsum}
output: 
   pdf_document:
    keep_tex: true
 ---


```{r, include = F}
defOut <- knitr::knit_hooks$get("plot")  # save the default plot hook 
knitr::knit_hooks$set(plot = function(x, options) {  # set new plot hook ...
  x <- defOut(x, options)  # first apply the default hook
  if(!is.null(options$wrapfigure)) {  # then, if option wrapfigure is given ...
    # create the new opening string for the wrapfigure environment ...
    wf <- sprintf("\\begin{wrapfigure}{%s}{%g\\textwidth}", options$wrapfigure[[1]], options$wrapfigure[[2]])
    x  <- gsub("\\begin{figure}", wf, x, fixed = T)  # and replace the default one with it.
    x  <- gsub("{figure}", "{wrapfigure}", x, fixed = T)  # also replace the environment ending
   }
   return(x)
   })
```

```{r flowchart-ablauf, echo = F, fig.cap = "Ablauf der Datenverarbeitung", fig.align = 'right', out.extra = 'trim = {5cm 6.5cm 12.5cm 4cm}', out.width=".5\\textwidth", wrapfigure = list("R", .5)}
DiagrammeR::grViz(diagram = "
              digraph rmarkdown {

              graph [nodesep = 1.5, ranksep = 1.5]                  

              node [style = filled, fillcolor = Gray90, shape = box, fontname = Helvetica, fontsize = 80, penwidth = 6, fixedsize = true, width = 16.5, height = 3]
                'Verbrauchsdaten von der\nSpitalpharmazie (Excel-Tabelle)'; 'Belegzahlen vom Ressort\nFinanzen (Excel-Tabelle)'; 

              node [shape = box, fontname = Helvetica, fontsize = 80, penwidth = 6, fixedsize = true, width = 20.5, height = 3]
              'Import der Daten aus Excel ins\nMicrosoft Access (Datenbanksoftware)'; 'Zusammenführen der Informationen und\nerstellen neuer, berechneter Tabellen';
              'Export der neu erstellten Tabellen\nin Form von Excel-Tabellen'; 'Berechnung und grafische Darstellung\nder Grafiken und Tabellen'

              node [shape = box, fontname = Helvetica, fontsize = 80, penwidth = 6, fixedsize = true, width = 20.5, height = 2]
              'Import der neuen Tabellen in R';

              #Pfeile
              edge [arrowhead = vee, arrowsize = 4, penwidth = 6]
              'Verbrauchsdaten von der\nSpitalpharmazie (Excel-Tabelle)' -> 'Import der Daten aus Excel ins\nMicrosoft Access (Datenbanksoftware)';
              'Belegzahlen vom Ressort\nFinanzen (Excel-Tabelle)' -> 'Import der Daten aus Excel ins\nMicrosoft Access (Datenbanksoftware)';
              'Import der Daten aus Excel ins\nMicrosoft Access (Datenbanksoftware)' -> 'Zusammenführen der Informationen und\nerstellen neuer, berechneter Tabellen';
              'Zusammenführen der Informationen und\nerstellen neuer, berechneter Tabellen' -> 'Export der neu erstellten Tabellen\nin Form von Excel-Tabellen'
              'Export der neu erstellten Tabellen\nin Form von Excel-Tabellen' -> 'Import der neuen Tabellen in R'
              'Import der neuen Tabellen in R' -> 'Berechnung und grafische Darstellung\nder Grafiken und Tabellen'
              }
              ")

```

Upvotes: 2

Related Questions