RMarkdown/knitr中的图边距 [英] Plot margins in RMarkdown/knitr
问题描述
在RStudio中,我的图看起来很完美,但是当我通过编织HTML"按钮生成HTML时,边界被切断了.
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.
我有一个较低的下边距来容纳x轴上的垂直标签,还有一个较宽的左边距来将y轴标题保持在一些大数字的左边,例如:
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)
```
x轴和y轴标签/标题均受影响;在默认的mgp
设置下,我的ylab
设置看起来不错,但是在更大的值下,它似乎被推离了画布"(或R中的任何术语).
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).
我还注意到knitr/rmarkdown无法识别全局设置的par()
选项.例如,设置完上述内容后,barplot(injury_top12, ylab = "Injuries")
将不会识别任何mar
,mgp
或las
选项,但是如果我将这些选项复制到barplot()
调用本身中,则las = 2
& mgp = c(4, 1, 0)
开始起作用,但仍无法识别mar
.
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.
我尝试使用R -e "rmarkdown::render('Readme.Rmd')"
命令从命令行生成HTML,这也出现了同样的问题.
I have tried generating the HTML from the command line with the command R -e "rmarkdown::render('Readme.Rmd')"
, which exhibited the same problem.
我正在使用R Studio 0.98.1103,knitr是1.11,rmarkdown是0.8,操作系统是ubuntu linux.
I am using R Studio 0.98.1103, knitr is 1.11, rmarkdown is 0.8, OS is ubuntu linux.
Example.Rmd:
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)
```
当我编织此Rmd文档时,第一个图不使用在par()
中设置的las
或mgp
选项.第二个图可以,但是x轴标签被切除,并且y轴标题几乎完全从框架中推出(y的尾部稍微可见).
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).
推荐答案
对于直接希望在答案和推文中隐藏答案的人,您的.Rmd
文件应如下所示:
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))
```
在您不想包含的块中.
这篇关于RMarkdown/knitr中的图边距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!