R LaTeX中tikzDevice和ggplot2生成的标题和图形之间的空白太多

R LaTeX中tikzDevice和ggplot2生成的标题和图形之间的空白太多,r,ggplot2,latex,tikz,R,Ggplot2,Latex,Tikz,我目前正在使用R的ggplot2和tikzDevice软件包生成图形,并在LaTeX文档中介绍它们,但我正在努力解决图形和标题之间产生的大空白问题,如果您比较图像,您可以看到(我已手动突出显示这些空白以使其更清晰): 这是我的MWE: R代码: library(ggplot2) library(tikzDevice) set.seed(1) x <- rnorm(200) tikz(file = "Rplots.tex", width = 4, height = 4) qplot(

我目前正在使用R的
ggplot2
tikzDevice
软件包生成图形,并在
LaTeX
文档中介绍它们,但我正在努力解决图形和标题之间产生的大空白问题,如果您比较图像,您可以看到(我已手动突出显示这些空白以使其更清晰):

这是我的MWE:

R代码:

library(ggplot2)
library(tikzDevice)

set.seed(1)
x <- rnorm(200)

tikz(file = "Rplots.tex", width = 4, height = 4)
qplot(x, geom = "histogram")
dev.off()
我想知道如何通过
ggplot2
消除标题和图形之间的巨大空间


PS.R版本:3.2.3,
ggplot2
version:2.1.0,
tikzDevice
version:0.10-1。我从Tobias Oetiker的《LaTeX 2e简介》,5.05版,第116页中获取了第二个情节的代码。

听起来更像是一个LaTeX问题;我认为调整空间的标准方法是将
\superscaptionskip

\documentclass{article}
\usepackage{tikz}
\setlength{\abovecaptionskip}{-15pt} 
\begin{document}

\begin{figure}
\centering
\include{Rplots}
\caption{\texttt{ggplot2} plot.}
\end{figure}

\end{document}

我仍然不明白您的问题,但让我提出一个完整的示例,说明在knitr文档中使用tikz和标准pdf设备时,两者的间距完全相同

---
title: "Untitled"
header-includes:
- \usepackage{caption}
output: 
  pdf_document: 
    fig_caption: yes
---

First, we try this


```{r pdf, echo=FALSE, dev='pdf', fig.height=2, fig.width=2, fig.cap='This is a standard graphic.'}
library(ggplot2)
ggplot() + theme(plot.background=element_rect(colour = "red"))
```

Next, we try this

```{r tikz, echo=FALSE, dev='tikz', fig.height=2, fig.width=2, fig.cap='This is a tikz graphic.'}
ggplot() + theme(plot.background=element_rect(colour = "red"))
```

\newpage

## Fixing space

\captionsetup{skip=0pt}

```{r pdf2, echo=FALSE, dev='pdf', fig.height=2, fig.width=2, fig.cap='This is a standard graphic.'}
library(ggplot2)
ggplot() + theme(plot.background=element_rect(colour = "red"))
```

```{r tikz2, echo=FALSE, dev='tikz', fig.height=2, fig.width=2, fig.cap='This is a tikz graphic.'}
ggplot() + theme(plot.background=element_rect(colour = "red"))
```


我很久没有问过这个问题了,但我想回答到底是什么问题

正如您在上的问题中所看到的,问题与以下事实有关:
\include{}
\include{}
之前和之后执行
\clearpage
,这生成了我所说的空白


另一方面,使用
\input{}
相当于将代码复制并粘贴到LaTeX文档中,这将防止出现空白。

感谢您的回复!这确实起了作用,但它改变了文档中的每个数字。显然,只有使用
tikzDevice
创建的tikz图形(据我所知)与标题之间有巨大的空白,因此我认为这可能是通过
tikzDevice
解决此问题的某种方法。我错了吗?在上面的示例中,我没有注意到异常大的间距。你有没有一个例子表明,对于同一个绘图,标准pdf设备产生的边距比tikz图形输出的边距小得多?嗨,巴蒂斯特,很抱歉反应太晚。我已更新了我的问题以使问题更清楚。您是否尝试过通过ggplot2主题更改绘图边距?我仍然不知道它是如何特定于tikz设备的。如果你看一下我在下面发布的示例,pdf设备获得了完全相同的边距。
---
title: "Untitled"
header-includes:
- \usepackage{caption}
output: 
  pdf_document: 
    fig_caption: yes
---

First, we try this


```{r pdf, echo=FALSE, dev='pdf', fig.height=2, fig.width=2, fig.cap='This is a standard graphic.'}
library(ggplot2)
ggplot() + theme(plot.background=element_rect(colour = "red"))
```

Next, we try this

```{r tikz, echo=FALSE, dev='tikz', fig.height=2, fig.width=2, fig.cap='This is a tikz graphic.'}
ggplot() + theme(plot.background=element_rect(colour = "red"))
```

\newpage

## Fixing space

\captionsetup{skip=0pt}

```{r pdf2, echo=FALSE, dev='pdf', fig.height=2, fig.width=2, fig.cap='This is a standard graphic.'}
library(ggplot2)
ggplot() + theme(plot.background=element_rect(colour = "red"))
```

```{r tikz2, echo=FALSE, dev='tikz', fig.height=2, fig.width=2, fig.cap='This is a tikz graphic.'}
ggplot() + theme(plot.background=element_rect(colour = "red"))
```