【问题标题】:Plot created in knitr via cache=TRUE do not keep the specified margin通过 cache=TRUE 在 knitr 中创建的绘图不保留指定的边距
【发布时间】:2019-01-09 19:19:12
【问题描述】:

我正在使用 cache=T chunck 选项通过 knitr 创建绘图。我正在使用 Rstudio。

边距未显示在输出 pdf 或保存在文件夹中的绘图中。但是,如果我在外边距上添加一个框,它会按预期工作。

是否有一个块选项来控制这个?我查看了https://yihui.name/knitr/options 上的绘图选项,但没有找到任何方法。

这是一个 Rmd 文件和创建的 2 个图像,注意使用 par(mar=c(4,4,4,4)theme(plot.margin=unit(rep(2,4),"cm"))

---
title: "Untitled"
output: pdf_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE,cache=T,dev='jpeg',fig.height=5,fig.width=5)
library(ggplot2)
```

```{r base}
par(mar=c(4,4,4,4))
plot(mtcars$cyl,mtcars$mpg)
```

```{r ggplot}
ggplot(data = mtcars) +
  geom_point(aes(x=cyl,y=mpg))+
  theme(plot.margin=unit(rep(2,4),"cm"))
```

```{r base_with_box}
par(mar=c(4,4,4,4))
plot(mtcars$cyl,mtcars$mpg)
box("outer", col='blue')  
```

【问题讨论】:

    标签: r ggplot2 knitr


    【解决方案1】:

    pdf_document 默认情况下会裁剪 PDF 绘图。见?rmarkdown::pdf_document。要禁用此功能:

    output:
      pdf_document:
        fig_crop: false
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-01-02
      • 2020-12-01
      • 2020-12-12
      • 2021-08-19
      • 2020-05-07
      • 2018-03-02
      • 1970-01-01
      相关资源
      最近更新 更多