【问题标题】:Blank page in knited PDF document with 2 ggplot() graphs带有 2 个 ggplot() 图形的针织 PDF 文档中的空白页
【发布时间】:2020-09-26 15:11:14
【问题描述】:

我在编写的 PDF R markdown 文档中遇到了空白页问题。看来,当我在单个代码块中绘制 2 个 ggplot() 图时,会在渲染图之前创建一个空白页。这是一个问题示例(完整的 .Rmd 文件,取消注释要运行的每个块的开头和结尾,UNCOMMENT 所在的位置!):

---
title: "Test"
author: "My Name"
output:
  pdf_document:
    number_sections: yes
    toc: yes
---

\newpage

# Plots

## First subtitle

Here I plot some data:

#UNCOMMENT```{r pressure, echo=FALSE, message = FALSE}
library(tidyverse)

# line chart
ggplot(pressure, aes(x = temperature, y = pressure)) +
        geom_line()

# step chart
ggplot(pressure, aes(x = temperature, y = pressure)) +
        geom_step()

#UNCOMMENT```

\newpage

# More plots

#UNCOMMENT```{r, echo = FALSE}

# line chart with points
ggplot(pressure, aes(x = temperature, y = pressure)) +
        geom_line() +
        geom_point()

# line chart
ggplot(pressure, aes(x = temperature, y = pressure)) +
        geom_line()

#UNCOMMENT```


编织时,PDF 输出在我的“更多绘图”块之前有一个空白页,即第 4 页是空白的:

[![pdf 输出][1]][1]

知道为什么会发生这种情况以及如何解决这个问题吗?如果我将 2 个图分成 2 个不同的块,问题就解决了,但我想在一些块中绘制多个图,所以这不是一个解决方案。

提前致谢!

【问题讨论】:

    标签: r ggplot2 r-markdown markdown knitr


    【解决方案1】:

    感谢this link。使用标题有助于使图居中。这只是一个建议,不是对您问题的明确回答。

    ---
     title: "Test"
     author: "My Name"
     output: pdf_document
     header-includes:
       - \usepackage{subfig}
    ---  
    
    ```{r echo =FALSE, results=FALSE}
    captions <- c("Caption 1",
                  "Caption 2", 
                  "Caption 3",
                  "Caption 4")
    ```
    
    ```{r, echo=FALSE, cache=FALSE, results=FALSE, warning=FALSE,  comment=FALSE, message= FALSE, eval =T,  fig.cap =    "Overall Caption", fig.subcap=captions, out.width='.49\\linewidth', fig.asp=1, fig.ncol = 2}
    library(ggplot2)
     # line chart
     p1<- ggplot(pressure, aes(x = temperature, y = pressure)) +
             geom_line()
    
     # step chart
     p2<-ggplot(pressure, aes(x = temperature, y = pressure)) +
             geom_step()
    
    
     # line chart with points
     p3<-ggplot(pressure, aes(x = temperature, y = pressure)) +
             geom_line() +
             geom_point()
    
     # line chart
     p4<-ggplot(pressure, aes(x = temperature, y = pressure)) +
             geom_line()
    
    
     p1
     p2
     p3
     p4
     ```      
    

    【讨论】:

    • 嗨,我在 x86_64-apple-darwin17.0(64 位)macOS Catalina 10.15.5 上使用 R 版本 4.0.0、rmarkdown_2.1。一个大的输出视图并没有改变问题......奇怪。
    • R 打印 pdf 并在第三方浏览器中显示,然后我从那里打印,你能更改 R 打开的 PDF 程序以查看 PDF 吗?
    • 是的,我尝试使用其他程序查看文件,但我仍然有这个空白页..
    • 我改了答案,请看上面
    • 谢谢,这确实很有用。我可以用它作为一个很好的解决方法,我也会将它用于其他目的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-28
    • 2020-10-30
    • 1970-01-01
    • 2011-07-22
    • 1970-01-01
    • 2015-10-06
    • 1970-01-01
    相关资源
    最近更新 更多