R Markdown Notebook



RMarkdown presents your code alongside its output (graphs, tables, etc.) with conventional text to explain it, a bit like a notebook. RMarkdown makes use of Markdown syntax. An R Notebook is an R Markdown document with chunks that can be executed independently and interactively, with output visible immediately beneath the input. R Notebooks are a method of literate programming ⧉ that allows for direct interaction with R while producing a reproducible document with publication-quality output.

Markdown is thought as a “lightweight” markup language, hence the namemarkdown. That’s why formatting options are scarce. However, there are someextensions, for instance brought by RMarkdown.

One point of particular interest is the sizing of figures. Let’s look at someways how to size a figure with RMarkdown.

R Markdown Notebook

We take some data first:

Not let’s plot.

Define size in YAML header

We can define the size of figures globally in the YAML part, like this forexample.

R Markdown Notebook

Define figure size as global chunk option

As a first R-chunk in your RMD document, define the general chunk settings likethis:

Chunk options

We can set the chunk options for each chunk too. With figh.height andfig.width we can define the size. Note that the numbers default to inches asunit: {r fig1, fig.height = 3, fig.width = 5}.

For a plot of different size, change simple the numbers: {r fig2, fig.height = 3, fig.width = 3, fig.align = 'center'}.

Alternatively, you may change the aspect ratio of the image: {r fig3, fig.width = 5, fig.asp = .62}.

Note that the aspect ratio is based on the fig.width specified by you. Seehere.

Different options for different output formats

The options for figure sizing also depend on the output format (HTML vs. Latex, we do not mention Word here). For instance, in Latex percentage is allowed, as is specified on the optionspage: {r fig4, out.width = '40%'}.

But note that it appears to work in HTML too.

Differnce between figure size and output size

We are allowed to specify thefigure size, and secondly the size of the figure as to appear in the output. Forexample, if you set the size of a ggplot figure to large, then fonts etc. willappear tiny. Better do not scale up fig.height, but set out.widthaccordingly, eg., like this out.width = '70%'.

Alternatively, instead of using Rfor plotting, you can just load an image. Of course, it is possible to just usemarkdown for that: ![](path/to/figure/figure.png).

Change the figure size like this: ![](file.jpg){ width=50% }. Note that nospace are allowed around the = (equal sign), and the curly brace { needs tocome right after the ) brace; no space allowed.

Jupiter notebook markdown guide

Similarly, with path to local folder:

![](../../sebastiansauer.github.io/images/2016-10-17/unnamed-chunk-5-1.png.png)

{width=20% }

Centering is not really part of markdown. But there are some workarounds. See:

Jupyter Notebook R Markdown

![](https://sebastiansauer.github.io/images/2016-10-17/unnamed-chunk-5-1.png){width=20% }

I used this code:

We can use the knitr function include_graphics which is convenient, as it takes care for the different output formats and provides some more features (seehere the helpfile).

Note that online sources are allowed. Don`t forget to load knitr previously.

Just resize the image with your favorite photo/image manager such as Gimp, Photoshop, Preview App etc.

R Markdown Vs R Notebook

here. The Book “R for Data Science” byHadley Wickham and Garrett Grolemund (read here) is agreat resource too. Read chapter 28 on diagramshere.Pandoc’s user guide has some helpful comments on figures sizing with Pandoc’smarkdown .