Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plot margins in RMarkdown/knitr

Within RStudio my plots look perfect, but when I generate HTML via the "knit HTML" button my margins are being cut off.

I am using the base graphics package to create a simple barplot.

I have a large lower margin to accommodate vertical labels across the x-axis, and a wide left margin to keep the y-axis title to the left of some large numbers, e.g.:

```{r set-graph-options}
par(mar = c(12, 6, 4, 2) + 0.1, mgp = c(4, 1, 0), las = 2)
```

Both the x-axis and y-axis labels/titles are affected; with the default mgp setting my ylab setting appears fine, but with a larger value it seems to be pushed off the "canvas" (or whatever the terminology is in R).

I also notice that knitr/rmarkdown does not recognize the globally set par() options. For example, after setting the above, barplot(injury_top12, ylab = "Injuries") will recognize none of the mar, mgp, or las options, but if I copy the options into the barplot() call itself, las = 2 & mgp = c(4, 1, 0) begin to work, but mar is still not recognized.

I have tried generating the HTML from the command line with the command R -e "rmarkdown::render('Readme.Rmd')", which exhibited the same problem.

I am using R Studio 0.98.1103, knitr is 1.11, rmarkdown is 0.8, OS is ubuntu linux.

Example.Rmd:

```{r echo = F, example-set-par}
library(knitr)
opts_knit$set(cache = FALSE, verbose = TRUE, global.par = TRUE)
par(mar = c(12, 6, 4, 2) + 0.1, mgp = c(4, 1, 0), las = 2)
d <- c("This is a longer than usual label" = 355,
       "Another quite long label" = 144,
       "A third longish label for a barplot" = 222)
```

Plot one depends on values set by `par()`:

```{r echo = F, example-plot-using-par}
barplot(d, ylab = "Arbitrary numbers")
```

Plot two explicitly sets options:

```{r echo = F, example-plot-with-explicit-options}
barplot(d, ylab = "Arbitrary numbers", mar = c(12, 6, 4, 2) + 0.1, mgp = c(4, 1, 0), las = 2)
```

When I knit this Rmd doc, the first plot does not use the las or mgp options set in par(). The second plot does, but the x-axis labels are cut off, and the y-axis title is pushed almost entirely out of the frame (the tail of the y is slightly visible).

example image

like image 616
Kenneth D. Avatar asked Sep 28 '15 16:09

Kenneth D.


People also ask

How do I set margins in R plot?

To visualize how R creates plot margins, look at margin Figure 11.20. You can adjust the size of the margins by specifying a margin parameter using the syntax par(mar = c(bottom, left, top, right)) , where the arguments bottom , left … are the size of the margins. The default value for mar is c(5.1, 4.1, 4.1, 2.1).

What does knitr :: Opts_chunk set echo true mean?

The first code chunk: ```{r setup, include=FALSE} knitr::opts_chunk$set(echo = TRUE) ``` is used to specify any global settings to be applied to the R Markdown script. The example sets all code chunks as “echo=TRUE”, meaning they will be included in the final rendered version.

How can you compile the R Markdown document using knitr package?

The usual way to compile an R Markdown document is to click the Knit button as shown in Figure 2.1, and the corresponding keyboard shortcut is Ctrl + Shift + K ( Cmd + Shift + K on macOS). Under the hood, RStudio calls the function rmarkdown::render() to render the document in a new R session.


1 Answers

For people that want directly the answer hidden within the comment and a tweet, your .Rmd file should look like:

---
title: exemple
header of your Rmd
---

```{r}
library(knitr)
opts_knit$set(global.par = TRUE)
```
The important think in the previous chunk is  to put global.par=TRUE 

Plot one depends on values set by `par()`:

```{r}
par(mar=c(5,5,0,0)) #it's important to have that in a separate chunk
``` 
Now we just plot a point with the good margin set by the global `par()`

```{r}
plot(1,1,main="a plot with the margin as set globally")
```

if you don't want to include the code used to set the global margin in the output just add :

```{r,include=FALSE}
par(mar=c(5,5,0,0))
``` 

in the chunk you don't want to include.

like image 126
Simon C. Avatar answered Sep 28 '22 21:09

Simon C.