I made a flowchart with R which I included in my R Markdown file.
The code:
```{r flowchart-data, echo = FALSE, message = FALSE, fig.cap = "Ablauf der Datenverarbeitung", fig.align = "right", fig.width = 7, fig.height = 6, out.extra = 'trim = {0 1.1cm 0 0}, clip', out.width=".7\\textwidth"}
library(grid)
library(Gmisc)
# grid.newpage()
# set some parameters to use repeatedly
leftx <- .2
midx <- .5
rightx <- .8
myBoxGrob <- function(text, ...) {
boxGrob(label = text, bjust = "top", box_gp = gpar(fill = "lightgrey"), ...)
}
# create boxes
(Pharmazie <- myBoxGrob("Verbrauchsdaten von der\n Spitalpharmazie (Excel-Tabelle)", x=leftx, y=1, width = 0.36))
(Finanzen <- myBoxGrob("Belegzahlen vom Ressort\n Finanzen (Excel-Tabelle)", x=rightx, y=1, width = 0.36))
(A <- myBoxGrob("Import der Daten aus Excel ins\n Microsoft Access (Datenbanksoftware)", x=midx, y=0.83, width = 0.45))
(B <- myBoxGrob("Zusammenführen der Informationen\n und erstellen neuer, berechneter Tabellen", x=midx, y=.66, width = 0.45))
(C <- myBoxGrob("Export der neu erstellten Tabellen\n in Form von Excel-Tabellen", x=midx, y=.49, width = 0.45))
(D <- myBoxGrob("Import der neuen Tabellen in R", x=midx, y=.32, width = 0.45))
(E <- myBoxGrob("Berechnung und grafische Darstellung\n der Grafiken und Tabellen", x=midx, y=.19, width = 0.45))
connectGrob(Pharmazie, A, "L")
connectGrob(Finanzen, A, "L")
connectGrob(A, B, "N")
connectGrob(B, C, "N")
connectGrob(C, D, "N")
connectGrob(D, E, "N")
```
What I would like:
- I want the text to use the space on the left of the figure.
- I would like for the figure caption to align with the center of the figure. Right now the caption is in the middle of the page, ignoring if the figure is centered, right or left aligned.
How can I achieve those things?
EDIT 1: I want to knit to pdf.
EDIT 2: As requested in the comments, how my page looks now:
There is a chunk option called
fig.env
with which one can switch from thefigure
to themarginfigure
environment. Unfortunetly, the list of possible environments does not includewrapfigure
. Therefore we will alter the plot chunk:The comments should clarify what we are actually doing here. Notice, that the expected value of
wrapfigure
is a list of two elements. The first one tells LaTeX to move the figure to either side of the page. The second element tells LaTeX the width of the wrapped figure. To move a figure with a width of0.7\\textwidth
to the right you setwrapfigure = list("R", 0.7)
(as you might have guessed,L
moves it to the left). All we have to do now is to include thewrapfig
package in the YAML and set this chunk option. Here is a reproducible example:Notice, that this solution most probably only works with a chunk creating a single plot. It should be possible to extent that to a chunk containing several figures.
As far as I know you can embed HTML code within a markdown document, so if you're knitting to HTML, you can do something like this:
Wrap the cell with a
div
tag with the style of alignment you want (no need to align the cell anymore), and as for the caption you can add a transparent box at the bottom with the caption as the text insideI struggled a lot with this as well, but for html-output. There is an argument to the r chunk that solved the problem for me:
out.extra='style="float:right; padding:10px"'