Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a tikz picture with 2 plots in a row

Tags:

r

statistics

tikz

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!

like image 630
user734124 Avatar asked May 05 '11 14:05

user734124


People also ask

Which is better PStricks or TikZ?

PStricks is more powerful and expressive than TikZ. In a dire emergency you even have the full power of PostScript. PStricks has a significant ecology of extra packages that have been built on top of it; TikZ is too new to have many such things.

How do you draw a thick line in TikZ?

You can change the thickness of every single line so that it matches your requirements. You can use: ultra thin, very thin, thin, semithick, thick, very thick and ultra thick. You can specify your custom width using the line width option of the \draw command. The default unit is pt.

How do you draw a line in TikZ?

One of the simplest and most commonly used commands in TikZ is the \draw command. To draw a straight line we use this command, then we enter a starting co-ordinate, followed by two dashes before the ending co-ordinate. We then finish the statement by closing it with a semicolon.

What is a path in TikZ?

A path is a series of straight and curved line segments. It is specified following a \path command and the specification must follow a special syntax, which is described in the subsections of the present section. \path<specification>; This command is available only inside a {tikzpicture} environment.


2 Answers

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.

like image 74
Greg Snow Avatar answered Oct 03 '22 08:10

Greg Snow


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

like image 43
Sharpie Avatar answered Oct 01 '22 08:10

Sharpie