首页 > 解决方案 > 带有 2 个 ggplot() 图形的针织 PDF 文档中的空白页

问题描述

我在我的编织 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] PDF输出

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

提前致谢!

标签: rggplot2r-markdownmarkdownknitr

解决方案


归功于此链接。使用标题有助于使图居中。这只是一个建议,不是对您问题的明确回答。

---
 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
 ```      

推荐阅读