r - 带有 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 页为空白:
知道为什么会发生以及如何解决该问题吗?如果我将 2 个图分成 2 个不同的块,问题就解决了,但我想在一些块中绘制多个图,所以这不是一个解决方案。
提前致谢!
解决方案
归功于此链接。使用标题有助于使图居中。这只是一个建议,不是对您问题的明确回答。
---
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
```
推荐阅读
- python - 如何从矩阵中删除行
- python - Python比较关系数据库中两个表之间的数百万行和数百列
- python - 如何标记一个字符串(其中包含有关数学计算和浮点数的数据)?
- python - 使用python通过互联网在PC之间发送变量
- python - Gunicorn 找不到模块
- node.js - 如何将mongodb atlas与nodejs连接起来
- c++ - 将双 * 转换为 std::vector
给我错误 - javascript - 如何使用 JQuery 而不是 Javascript 来显示警报消息?
- r - 有效地存储具有大量预编译变量的数据集(并且可能只使用少数变量)
- python - Python:当您的脚本需要本地目录时,如何在云端运行 Cron 作业?