Ken*_* D. 6 r rstudio knitr r-markdown
在 RStudio 中,我的图看起来很完美,但是当我通过“knit HTML”按钮生成 HTML 时,我的边距被切断了。
我正在使用基本图形包来创建一个简单的条形图。
我有一个很大的下边距来容纳跨 x 轴的垂直标签,还有一个很宽的左边距以将 y 轴标题保持在一些大数字的左侧,例如:
```{r set-graph-options}
par(mar = c(12, 6, 4, 2) + 0.1, mgp = c(4, 1, 0), las = 2)
```
Run Code Online (Sandbox Code Playgroud)
x 轴和 y 轴标签/标题都会受到影响;使用默认mgp设置,我的ylab设置看起来不错,但是如果值较大,它似乎被推离了“画布”(或 R 中的任何术语)。
我还注意到 knitr/rmarkdown 无法识别全局设置par()选项。比如上面的设置后,barplot(injury_top12, ylab = "Injuries")将不识别mar, mgp, 或las选项,但是如果我将选项复制到barplot()调用本身中,las = 2&mgp = c(4, 1, 0)开始工作,但mar仍然无法识别。
我尝试使用命令从命令行生成 HTML R -e "rmarkdown::render('Readme.Rmd')",这也出现了同样的问题。
我使用的是 R Studio 0.98.1103,knitr 是 1.11,rmarkdown 是 0.8,操作系统是 ubuntu linux。
示例.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)
```
Run Code Online (Sandbox Code Playgroud)
当我编织这个RMD文档,第一个情节不使用las或mgp在设置选项par()。第二个图是,但 x 轴标签被切断,y 轴标题几乎完全被推出框架(y 的尾部略微可见)。

对于想要直接隐藏在评论和推文中的答案的人,您的.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")
```
Run Code Online (Sandbox Code Playgroud)
如果您不想在输出中包含用于设置全局边距的代码,只需添加:
```{r,include=FALSE}
par(mar=c(5,5,0,0))
```
Run Code Online (Sandbox Code Playgroud)
在您不想包含的块中。
| 归档时间: |
|
| 查看次数: |
10802 次 |
| 最近记录: |