简体   繁体   English

在RMarkdown / knitr中绘制边距

[英]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. 在RStudio中我的情节看起来很完美,但是当我通过“编织HTML”按钮生成HTML时,我的边距被切断了。

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, eg: 我有一个较大的下边距,以容纳横跨x轴的垂直标签,以及一个宽的左边距,以保持y轴标题左侧的一些大数字,例如:

```{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; x轴和y轴标签/标题都受到影响; 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). 使用默认的mgp设置,我的ylab设置看起来很好,但是如果值较大,它似乎被推离“画布”(或R中的任何术语)。

I also notice that knitr/rmarkdown does not recognize the globally set par() options. 我还注意到knitr / rmarkdown无法识别全局设置的par()选项。 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. 例如,在设置上述内容后, barplot(injury_top12, ylab = "Injuries")将不会识别marmgplas选项,但如果我将选项复制到barplot()调用本身,则las = 2mgp = c(4, 1, 0) mar mgp = c(4, 1, 0)开始工作,但仍然无法识别mar

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 -e "rmarkdown::render('Readme.Rmd')"从命令行生成HTML,这显示了同样的问题。

I am using R Studio 0.98.1103, knitr is 1.11, rmarkdown is 0.8, OS is ubuntu linux. 我使用的是R Studio 0.98.1103,knitr是1.11,rmarkdown是0.8,OS是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)
```

When I knit this Rmd doc, the first plot does not use the las or mgp options set in par() . 当我编写这个Rmd文档时,第一个图不使用par()设置的lasmgp选项。 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). 第二个绘图确实如此,但是x轴标签被切掉,y轴标题几乎完全被推出框架(y的尾部略微可见)。

示例图片

For people that want directly the answer hidden within the comment and a tweet, your .Rmd file should look like: 对于直接想要在评论和推文中隐藏答案的人,您的.Rmd文件应如下所示:

---
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. 在你不想包括的块中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM