3

I'm currently using R's ggplot2 and tikzDevice packages to produce graphics and introduce them in LaTeX documents, but I'm struggling with the resulting big white spaces between the figures and the captions, as you can see if you compare the images (I've manually highlighted the spaces to make it clearer):

ggplot2 plot tikz plot

Here's my MWE:

The R code:

library(ggplot2)
library(tikzDevice)

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

tikz(file = "Rplots.tex", width = 4, height = 4)
qplot(x, geom = "histogram")
dev.off()

and the LaTeX code:

\documentclass{article}
\usepackage{tikz}

\begin{document}

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

\begin{figure}
\centering
\begin{tikzpicture}[scale=3]
    \clip (-0.1,-0.2)
    rectangle (1.8,1.2);
    \draw[step=.25cm,gray,very thin]
    (-1.4,-1.4) grid (3.4,3.4);
    \draw (-1.5,0) -- (2.5,0);
    \draw (0,-1.5) -- (0,1.5);
    \draw (0,0) circle (1cm);
    \filldraw[fill=green!20!white,
    draw=green!50!black]
    (0,0) -- (3mm,0mm)
    arc (0:30:3mm) -- cycle;
\end{tikzpicture}
\caption{\texttt{tikz} plot.}
\end{figure}

\end{document}

I'd like to know how to get rid of the great space between the caption and the figure via ggplot2.

PS. R version: 3.2.3, ggplot2 version: 2.1.0, tikzDevice version: 0.10-1. I've taken the code for the second plot from Tobias Oetiker's The Not so Short Introduction to LaTeX 2e, version 5.05, page 116.

  • have you tried changing the plot margin via the ggplot2 theme? I still don't see how it's specific to the tikz device. If you look at the example I posted below, the exact same margin is obtained for the pdf device. – baptiste Mar 20 '16 at 04:29

3 Answers3

5

It's been so long since I asked this question, but I wanted to answer what was the problem in the end.

As you can see in this question on the differences between \input{} and \include{}, the problem has to do with the fact that \include{} does a \clearpage before and after \include{}, that generates the white space I was talking about.

On the other hand, using \input{} is the equivalent to copy and pasting the code into the LaTeX document, that will prevent the white spaces.

2

it sounds more like a LaTeX question; I think the standard way to tweak the space is to set \abovecaptionskip

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

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

\end{document}

enter image description here

baptiste
  • 75,767
  • 19
  • 198
  • 294
  • Thank you for your response! That did the trick, but it alters every figure in the document. Apparently just the tikz figures (as far as I can see) created with `tikzDevice` has that enormous white space between them and the captions, so I thought that may be some way to fix this via `tikzDevice`. Am I wrong? – Jorge Esteban Mendoza Mar 16 '16 at 21:26
  • i haven't noticed unusually large spacing in the above example. Do you have an example showing that the standard pdf device produces much smaller margins than the tikz graphics output for the same plot? – baptiste Mar 16 '16 at 22:12
0

I still don't get your problem, but let me suggest this complete example showing the exact same spacing for the tikz and standard pdf device when used in a knitr document.

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

enter image description here

enter image description here

baptiste
  • 75,767
  • 19
  • 198
  • 294