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).
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).
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With