6

I've got a question concerning the creation of two plots in a row, using R with Package tikz device to send the tikz to Latex. What I want to do is, plotting 2 graphs in a row. What I did first was to create to plots in the par(mfrow=c(1,2)) ambiance. But this doesn't work good, because the Plots are like vertical rectangles, when printing with Latex. What I want to have is to get two pictures in a row which are quadratic. So what I did next, I created a layout:

Layout<- matrix(c(1, 2), nrow = 1, ncol=2, byrow = TRUE)
nf <- layout(mat = Layout, widths = c(1,1),heights = c(1,1), respect = TRUE)
layout.show(nf)

and plotted the two graphs. The result is quadratic (that's good), but when I add a caption to the two plots (in Latex) it is far out from the graph. What am I suppose to do? Any thougts are appreciated!

Sharpie
  • 17,323
  • 4
  • 44
  • 47
user734124
  • 489
  • 8
  • 20
  • Thanks so far! But is there a way to get rid of the whitespace when plotting two plots in the following layout: `Layout<- matrix(c(0,0,1,2), nrow = 2, ncol=2, byrow = TRUE)` `nf <- layout(mat = Layout, widths = c(1,1), heights = c(1,1), respect = TRUE)` `layout.show(nf)` Again: The plots have to be in a quadratic form! "par(mfrow=c(1,2))" or vice versa doesn't help! Any thought is really appreciated! Regrads from Germany! PS: maybe it can be achieved wit the split.screen command. Further information `??split.screen`. But I absolutely don't understand how to use this :( – user734124 May 09 '11 at 10:09

2 Answers2

6

You specified respect=TRUE, so that means that your plots within the device only take up half of the height (if using defaults on the tikz device) and the rest of the height is filled with whitespace. When you add a caption it is offset from the plots by that whitespace. When you open the tikz device set the height and the width so that the height is close to half the width and you should end up with a lot less whitespace in the end and the caption will be closer to the plots.

Greg Snow
  • 48,497
  • 6
  • 83
  • 110
  • Thanks so far! But is there a way to get rid of the whitespace when plotting two plots in the following layout: `Layout<- matrix(c(0,0,1,2), nrow = 2, ncol=2, byrow = TRUE)` `nf <- layout(mat = Layout, widths = c(1,1), heights = c(1,1), respect = TRUE)` `layout.show(nf)` Again: The plots have to be in a quadratic form! `par(mfrow=c(1,2))` or vice versa doesn't help! Any thought is really appreciated! Regrads from Germany! PS: maybe it can be achieved wit the split.screen command. Further information ??split.screen. But I absolutely don't understand how to use this :( – user734124 May 10 '11 at 10:28
  • Doesn't setting the height and width as Greg suggests work? What happens wrong when you do so? – Aaron left Stack Overflow May 10 '11 at 16:05
  • If I load the tikzDevice package then type tikz(height=3.5), copy and paste the commands above, then do dev.off. I get a TeX file that I then add some comments to and the documentclass and begin{document} etc., then run it and get a figure that is full size with the text right next to the boxes without any whitespace and the boxes look nice and square. Can you show the exact commands you are using and how the results don't match with what you want? – Greg Snow May 10 '11 at 19:25
2

As Greg mentioned, you have to adjust the width and the height of the plot canvas if you want square plots but don't want R to fill in large borders of whitespace.

Here is a minimal example using Sweave:

\documentclass{article}
\usepackage{Sweave}
\usepackage{tikz}
<<echo=FALSE,results=hide>>=
  require(tikzDevice)
@

\begin{document}

\begin{figure}
<<echo=FALSE,results=hide>>=
  # Standard LaTeX article class has a \textwidth of ~4.5in
  # Therefore, divide by 2 to get the right height.
  tikz('layout-ex.tex', width = 4.5, height = 2.25)

  Layout<- matrix(c(1, 2), nrow = 1, ncol=2, byrow = TRUE)
  nf <- layout(mat = Layout, widths = c(1,1),heights = c(1,1), respect = TRUE)
  layout.show(nf)

  dev.off()
@

  \centering
  \input{layout-ex}
  \label{fig:layout-ex}
  \caption{A layout with two sub-figures}
\end{figure}

\end{document}

The resulting figure looks like this:

Example of using Layout

Sharpie
  • 17,323
  • 4
  • 44
  • 47
  • Yes this helps, but now the figure is pretty small :( How can I adjust the seize of the figure? I didn't use sweave, but it should make no difference... – user734124 May 10 '11 at 16:59
  • @user734124 Just adjust the LaTeX margins so you can have a wider figure and re-calculate the appropriate width and height: `tikz(width = \textwidth, height = 1/2 \textwidth)`. – Sharpie May 10 '11 at 17:02
  • Could you please also mention, how to get the Sweave Package? I'm a total Latex noob, sorry to bother you! @ Sharpie: where do I have to drop this command? Is it possible to use this command without having sweave? – user734124 May 10 '11 at 17:11
  • The "Sweave package" is provided by R in the file `Sweave.sty`. You can discover its location with the following R command: `file.path(R.home('share'),'texmf','tex','latex','Sweave.sty')`. Then, just copy the file to the same folder as your Sweave document [or put it in a texmf tree](http://tex.stackexchange.com/questions/1137/where-do-i-place-my-own-sty-files-to-make-them-available-to-all-my-tex-files) to make it available to all of your documents. – Sharpie May 10 '11 at 17:20
  • The command I showed was just a template for performing the size calculation. If you [adjust the LaTeX page layout](http://en.wikibooks.org/wiki/LaTeX/Page_Layout) such that `\textwidth` was 6 inches, then you would start the TikZ figure using `tikz(..., width = 6, height = 3)` in order to use your 1 x 2 layout without having R insert a border. This does not require Sweave. – Sharpie May 10 '11 at 17:25
  • awesome! I got it :) Thank you Sharpie! – user734124 May 11 '11 at 07:35