The marriage between R and LaTeX – Part 2

In the first part of this series, I explained how an R plot can be integrated into a LaTeX document through the tikzDevice package (on the R side) and through the tikz package (on the LaTeX side). In this part I will expand more on these 2 important packages.

The tikz LaTeX package is in itself a plotting program even without the R environment enabling you to create stunning figures. It has a mathematical engine also. This package was written by Till Tantau [1].

In LaTeX include the following code in the preamble of your LaTeX document:

\usepackage{tikz}

The following section explains the tikz package on its own outside the context of integrating R into LaTeX. The idea here is that if you know how to plot using the package inside your LaTeX document, you can tweak the code  generated in the .tex file from R in case you don’t like something.

Drawing with the tikz LaTeX package

The component code that hold commands of plots for the tikz are normally in the form:

\begin{tikzpicture} …code…   \end{tikzpicture}

You can preferably include even the above code in a \figure block like the following:

\begin{figure}
\begin{tikzpicture} …code…   \end{tikzpicture}
\caption{My caption goes here}
\end{figure}

Although a lot of time, I don’t include the \begin{tikzpicture} …code …\end{tikzpicture} inside a \begin{figure} … \end{figure}. Why? because sometimes I need the effect of having a small picture or drawing integrated  inside my text without the need of any extra spacing that comes normally with a \begin{figure} … \end{figure} block. I advice you to try both and see what suits your situation.

To draw a line from point (0,0) to point (2,2), you would use the command \draw as follows:

\begin{figure}
\begin{tikzpicture}
\draw (0,0) – – (2,2) ;
\end{tikzpicture}
\caption{My caption goes here}
\end{figure}

Every command like the \draw command  have to end with a semicolon “;”.

There are many keys that you can put near the \draw command and other commands. These “keys” are surrounded by square brackets [ ]. Many keys can be given to a command, in this case they are seperated by a comma “,”. We will see many examples later.  Each key allows you to change the function of the tikz commands. The “- -” means “to” which you can actually replace by the word “to”. Like the following.

\begin{figure}
\begin{tikzpicture}
\draw (0,0)  to (2,2) ;
\end{tikzpicture}
\caption{My caption goes here}
\end{figure}

The “to” command is more used to draw curves than straight lines which I will cover later. You can draw from let’s say point (0,0) to (1,2) to then (1,0) to then (3,3). How do you do that?

\begin{figure}
\begin{tikzpicture}
\draw (0,0) – – (1,2) – – (1,0) – – (3,3) ;
\end{tikzpicture}
\caption{My caption goes here}
\end{figure}

You can put  as much \draw commands as you want, each on a line like the following  (don’t forget each tikz statement should end with a “;”):

\begin{figure}
\begin{tikzpicture}
\draw (0,0)  – – (1,2) – – (1,0) – – (3,3) ;
\draw (2,2) – – (1,4);
\end{tikzpicture}
\caption{My caption goes here}
\end{figure}

The following are some of the keys which are used frequently (actually there are tons of them) – at the end of this article, I will suggest few manuals which you can refer to in case you need more functions:

  • [help lines]: allows you to draw a grid of lines that helps you see your Cartesian graph. You can say \draw[help lines] (0,0) grid (4,4). This creates a grid of dashed lines from the point (0,0) to point (4,4).
  • [->], [<-],[|->],[<->] … : this kind of form allows you to precise the shape of the line(s) that looks like what you put inside the square brackets. What I mean here is that  say you have this statement: \draw[->] (2,0) – – (2,2) – – (5,5) that means you want tikz to draw “right arrow like” lines. That is quite handy by the way.
  • [ultra thin], [very thin], [thin], [semithick], [thick], [very thick]…. : allows you to change the thickness or width of the lines. Example: \draw[thick] (2,0) – – (2,2) – – (5,5)
  • [line width = x]: allows you instead of specifying the width or thickness of the line by words as above. You can specify how much exactly. x is by default in points. Example you can say \draw[line width = 12] (2,0) – – (2,2) – – (5,5). This draws line(s) of width 12 points passing through the points you specified. You can change the unit of x from points to cm or other allowed units when you specify that as follows: \draw[line width = 0.2cm] (2,0) –(2,2) — (5,5)
  • [colour] : specify what colour you want to draw with. Example: \draw[blue] (2,0) – -(2,2) – – (5,5). There are many names for colours you can specify like “red”, “blue”, “gray”, “lightgray” etc….
  • [dashed], [dotted] …. : Many keys like that specify how lines are.
  • [fill] or [fill=X]: If it happens the drawing commands produce  shapes or areas under lines, those would be filled by a certain colour. By default, it is black. Normally [fill] with a colour specified will fill the shape with the default colour. Example: \draw [fill = red] (0,0) rectangle (2.5,2); this draws a rectangle filled completely in red colour with a border that is in black (default colour of border is black) from origin point (0,0) with length 2.5 and width of 2. To make the border red also you would say: \draw [red, fill = red] (0,0) rectangle (2.5,2); . By the way there is another cool command \path which do the same as \draw but does not draw outlines (or borders). Example \path [fill=red] (0,0) rectangle (2.5,2); this draws a rectangle filled with red with NO Borders.
  • and many many more …

You can mix as much keys as you want. You have to separate them with  commas. Example:

\begin{tikzpicture}
\draw[yellow, dotted, line width=6] (0,0)  – – (1,2) – – (1,0) – – (3,3) ;
\end{tikzpicture}

Now to draw curves, geometric shapes etc… you can do

\begin{figure}
\begin{tikzpicture}
\draw [red] (0,0) rectangle (2.5,2);
\end{tikzpicture}
\caption{My caption goes here}
\end{figure}

This draws a rectangle in red from origin point (0,0) with length 2.5 and width of 2.
\begin{figure}
\begin{tikzpicture}
\draw [blue, thick] (3.5,3) circle [radius=1.5];
\end{tikzpicture}
\caption{My caption goes here}
\end{figure}
 This draws a circle in red with ultra thick border starting at point (3.5,3) and of radius 1.5.
\begin{figure}
\begin{tikzpicture}
\draw [violet, thin] (4,0) arc [radius=1, start angle=45, end angle= 120];
\end{tikzpicture}
\caption{My caption goes here}
\end{figure}
This draws an arc starting at point (4,0) of violet colour and “thin” line width, leaving this point at an angle of 45 degrees and stopping when the slope becomes 120 degrees.
 There are many other things you can do than these simple shapes and arc but it would require a whole book to explain everything. You can actually plot a function in tikz but first to create the effect of x-axis and y-axis in tikz you can write the following:
\begin{figure}
\begin{tikzpicture}
\draw [<->] (0,3) – – (0,0) – – (3,0);
\end{tikzpicture}
\caption{My caption goes here}
\end{figure}
The shape of <-> create the effect of two arrows one up and one down. Actually the first arrow begin from (0, 3) (on the y-axis) and go to the origin and then from the origin (0,0) to (3,0) on x-axis.

Let’s say I want to plot f(x) = x^2 + x + 0.3. How I can do that in tikz?

\begin{figure}
\begin{tikzpicture}
\draw [<->] (0,3) – – (0,0) – – (3,0);
\draw[red, ultra thick, domain = 0:0.8] plot (\x, {\x*\x + \x + 0.3});
\end{tikzpicture}
\caption{My caption goes here}
\end{figure}

The first \draw statement was explained before, it draws the x-axis  and y-axis. The second \draw statement has the plot function. You can actually do amazing stuff with this function but for brevity, I gave it 2 arguments, first being the \x in other words I want to draw a function over x. The other is argument in braces {}. So the form is like that: plot(\x,{function}). Realize I need to escape x with \.

The domain key shows the range over which f(x) is plotted. There are tons and tons of mathematical functions that we can draw like sin(\x), cos(\x), factorial(\x), sqrt(\x), pow(\x,y) {meaning x to the power y}, exp(\x), ln(\x), log10(\x), log2(\x), abs(\x) etc….

The mathematical engine in tikz package is really powerful. Even the \draw command can understand a lot of mathematical constants like Pi=3.141592… or e=2.718… etc so you can include those constants as you wish.

\begin{figure}
\begin{tikzpicture}
\draw [<->] (0,3) – – (0,0) – – (3,0);
\draw [green,domain=0:2*pi] plot (\x, {(sin(\x r)* ln(\x+1))/2});
\draw[red, ultra thick, domain = 0:0.8] plot (\x, {\x*\x + \x + 0.3});
\end{tikzpicture}
\caption{My caption goes here}
\end{figure}

Now how you put labels inside your pictures or graphs? This is done by the \node command.

\begin{figure}
\begin{tikzpicture}
\draw [thick, <->] (0,3) – – (0,0) – – (3,0);
\node at (1,1) {Hey! I am at this point};
\end{tikzpicture}
\caption{My caption goes here}
\end{figure}

What \node command do is to create a label at the point you specify. The text has its centre at this exact point (1,1). You can specify a relative position to a certain point by giving the \node command certain location keys (“below”, “above”, “left”, “right”, “below right”, “above left”, “above right” etc…). See the following examples:

\begin{figure}
\begin{tikzpicture}
\draw [thick, <->] (0,3) – – (0,0) – – (3,0);
\node[below] at (1,1) {Hey! I am below this point};
\end{tikzpicture}
\caption{My caption goes here}
\end{figure}

This put a label but below point (1,1).

\begin{figure}
\begin{tikzpicture}
\draw [thick, <->] (0,3) – – (0,0) – – (3,0);
\node[below right] at (1,1) {Hey! I am below to the right of this point};
\end{tikzpicture}
\caption{My caption goes here}
\end{figure}

This puts a label but below and to the right of the point (1,1).

You can label the x- axis or y-axis by doing the following per example:

\begin{figure}
\begin{tikzpicture}
\draw [thick, <->] (0,3) – – (0,0) – – (3,0);
\node[below right] at (1,0) {$x$};
\node[left] at (0,1) {$y$};
\end{tikzpicture}
\caption{My caption goes here}
\end{figure}

This puts the letter x below the arrow the correspond to the x-axis. It also put the letter y at the left of y-axis which the effect that you normally want.

Important keys to tweak of \tikzpicture for R plots

So that’s fine but you probably wondering what are the tweaks that I talked about in the beginning of the article that I might need to make my R plots looks even better on the LaTeX side. By now you know that these plots are included in .tex files which contain  huge blocks of \begin{tikzpicture} …..huge code…. \end{tikzpicture}. You include that .tex file using the \include LateX command.

I explained very few details of \tikzpicture and prove to you that you can create many beautiful things. Actually I did not even scratch the surface of this.

I found from experience that I need to use the keys [scale=X], [xscale =X], [yscale =X] to tweak my R plots. I am sorry that I did not mention them above when I talked about keys because I want their explanation belongs to this section. Another reason is these type of scale keys belong to  the level of the tikzpicture itself not to level of statement commands like the \draw, \path or  \node … which in turn have their own scale keys.

In part 1, we generated a barplot in a .tex file. First let me show you the content  of the generated barplot .tex file that comes from R:

% Created by tikzDevice version 0.10.1 on 2017-03-24 17:04:39
% !TEX encoding = UTF-8 Unicode
\begin{tikzpicture}[x=1pt,y=1pt]
\definecolor{fillColor}{RGB}{255,255,255}
\path[use as bounding box,fill=fillColor,fill opacity=0.00] (0,0) rectangle (433.62,433.62);
\begin{scope}
\path[clip] ( 0.00, 0.00) rectangle (433.62,433.62);
\definecolor{drawColor}{RGB}{0,0,0}
\definecolor{fillColor}{RGB}{190,190,190}
\path[draw=drawColor,line width= 0.4pt,line join=round,line cap=round,fill=fillColor] ( 62.50, 64.40) rectangle (160.33,384.42);
\path[draw=drawColor,line width= 0.4pt,line join=round,line cap=round,fill=fillColor] (179.90, 64.40) rectangle (277.72,320.42);
\path[draw=drawColor,line width= 0.4pt,line join=round,line cap=round,fill=fillColor] (297.29, 64.40) rectangle (395.12,171.07);
\end{scope}
\begin{scope}
\path[clip] ( 0.00, 0.00) rectangle (433.62,433.62);
\definecolor{drawColor}{RGB}{0,0,0}
\node[text=drawColor,anchor=base,inner sep=0pt, outer sep=0pt, scale= 1.00] at (111.42, 39.60) {3};
\node[text=drawColor,anchor=base,inner sep=0pt, outer sep=0pt, scale= 1.00] at (228.81, 39.60) {4};
\node[text=drawColor,anchor=base,inner sep=0pt, outer sep=0pt, scale= 1.00] at (346.20, 39.60) {5};
\end{scope}
\begin{scope}
\path[clip] ( 0.00, 0.00) rectangle (433.62,433.62);
\definecolor{drawColor}{RGB}{0,0,0}
\node[text=drawColor,anchor=base,inner sep=0pt, outer sep=0pt, scale= 1.20] at (228.81,404.88) {\bfseries Car Distribution};
\node[text=drawColor,anchor=base,inner sep=0pt, outer sep=0pt, scale= 1.00] at (228.81, 15.60) {Number of Gears};
\end{scope}
\begin{scope}
\path[clip] ( 0.00, 0.00) rectangle (433.62,433.62);
\definecolor{drawColor}{RGB}{0,0,0}
\path[draw=drawColor,line width= 0.4pt,line join=round,line cap=round] ( 49.20, 64.40) — ( 49.20,363.09);
\path[draw=drawColor,line width= 0.4pt,line join=round,line cap=round] ( 49.20, 64.40) — ( 43.20, 64.40);
\path[draw=drawColor,line width= 0.4pt,line join=round,line cap=round] ( 49.20,107.07) — ( 43.20,107.07);
\path[draw=drawColor,line width= 0.4pt,line join=round,line cap=round] ( 49.20,149.74) — ( 43.20,149.74);
\path[draw=drawColor,line width= 0.4pt,line join=round,line cap=round] ( 49.20,192.41) — ( 43.20,192.41);
\path[draw=drawColor,line width= 0.4pt,line join=round,line cap=round] ( 49.20,235.08) — ( 43.20,235.08);
\path[draw=drawColor,line width= 0.4pt,line join=round,line cap=round] ( 49.20,277.75) — ( 43.20,277.75);
\path[draw=drawColor,line width= 0.4pt,line join=round,line cap=round] ( 49.20,320.42) — ( 43.20,320.42);
\path[draw=drawColor,line width= 0.4pt,line join=round,line cap=round] ( 49.20,363.09) — ( 43.20,363.09);
\node[text=drawColor,rotate= 90.00,anchor=base,inner sep=0pt, outer sep=0pt, scale= 1.00] at ( 34.80, 64.40) {0};
\node[text=drawColor,rotate= 90.00,anchor=base,inner sep=0pt, outer sep=0pt, scale= 1.00] at ( 34.80,107.07) {2};
\node[text=drawColor,rotate= 90.00,anchor=base,inner sep=0pt, outer sep=0pt, scale= 1.00] at ( 34.80,149.74) {4};
\node[text=drawColor,rotate= 90.00,anchor=base,inner sep=0pt, outer sep=0pt, scale= 1.00] at ( 34.80,192.41) {6};
\node[text=drawColor,rotate= 90.00,anchor=base,inner sep=0pt, outer sep=0pt, scale= 1.00] at ( 34.80,235.08) {8};
\node[text=drawColor,rotate= 90.00,anchor=base,inner sep=0pt, outer sep=0pt, scale= 1.00] at ( 34.80,277.75) {10};
\node[text=drawColor,rotate= 90.00,anchor=base,inner sep=0pt, outer sep=0pt, scale= 1.00] at ( 34.80,320.42) {12};
\node[text=drawColor,rotate= 90.00,anchor=base,inner sep=0pt, outer sep=0pt, scale= 1.00] at ( 34.80,363.09) {14};
\end{scope}
\end{tikzpicture}

As you can see many things appears in the above code that you are now roughly familiar with and there are many commands and keys that  I did not cover and could not cover in this article. If you realize in the beginning of the code, we have the [x=1pt, y=1pt] keys on the tikzpicture level. These are the unit lengths of the X and Y coordinates. There is a Z coordinate but is not needed in 2D plots. The unit is  normally 1, but could be anything. I advice not to change these. To scale the whole figure you can use the [scale = x] key as follows:

\begin{figure}
\begin{tikzpicture}[scale =3]
\draw [thick, <->] (0,3) – – (0,0) – – (3,0);
\node[below right] at (1,0) {$x$};
\node[left] at (0,1) {$y$};
\draw (0,0) — (3,3)
\end{tikzpicture}
\caption{My caption goes here}
\end{figure}

This actually scales the whole figure 3 fold bigger (on both the x-axis or horizontally and on the y-axis or vertically). It is like you  have magnified the figure 3x. The default is scale =1 and means no magnification nor minimization. If you say [scale=0.5], by this you minimize or shrink the figure in half. This is very useful sometimes.

\begin{figure}
\begin{tikzpicture}[scale = 0.5]
\draw [thick, <->] (0,3) – – (0,0) – – (3,0);
\node[below right] at (1,0) {$x$};
\node[left] at (0,1) {$y$};
\draw (0,0) — (3,3)
\end{tikzpicture}
\caption{My caption goes here}
\end{figure}

The above code, minimizes/shrinks the figure in half. You can change the magnification/minimization or scaling if you want on one level only like the horizontal level by changing the [xscale=x].

\begin{figure}
\begin{tikzpicture}[xscale = 3]
\draw [thick, <->] (0,3) – – (0,0) – – (3,0);
\node[below right] at (1,0) {$x$};
\node[left] at (0,1) {$y$};
\draw (0,0) — (3,3)
\end{tikzpicture}
\caption{My caption goes here}
\end{figure}

This magnifies the figure horizontally 3 fold bigger. Same for the [yscale =X] key but this applies to the vertical scale of your figure or plot.

\begin{figure}
\begin{tikzpicture}[yscale = 0.5]
\draw [thick, <->] (0,3) – – (0,0) – – (3,0);
\node[below right] at (1,0) {$x$};
\node[left] at (0,1) {$y$};
\draw (0,0) — (3,3)
\end{tikzpicture}
\caption{My caption goes here}
\end{figure}

This shrinks the figure on the y scale (vertical level) by half. You can of course use both like the following example:

\begin{figure}
\begin{tikzpicture}[xscale =2.5, yscale = 0.5]
\draw [thick, <->] (0,3) – – (0,0) – – (3,0);
\node[below right] at (1,0) {$x$};
\node[left] at (0,1) {$y$};
\draw (0,0) — (3,3)
\end{tikzpicture}
\caption{My caption goes here}
\end{figure}

Returning back to our generated R plot that we are including in our LaTeX document. We can scale the figure on the horizontal and vertical scale  if we deem necessary.

% Created by tikzDevice version 0.10.1 on 2017-03-24 17:04:39
% !TEX encoding = UTF-8 Unicode
\begin{tikzpicture}[x=1pt,y=1pt, xscale =1.2 , yscale = 1.2]
\definecolor{fillColor}{RGB}{255,255,255}

\end{scope}
\end{tikzpicture}

Hope you enjoyed this tutorial. Please leave your comments and suggestion below.

Few resources for more information on the tikz package

  • A very minimal introduction to TikZ found here.
  • TikZ and PGF  manual for version 1.18 found here.
  • Tikz pour l’impatient which is in French. I found it very useful. You can find it here.
  • Tikz & PGF manual 2.20 can be found here (a guide of 880 pages).

The next part 3 will discuss ways to transform any R table into a LaTeX table.

References

[1] Crémer, Jacques. “A very minimal introduction to TikZ.” Manuscript 0 (0) (2011): 1-24.

Leave a Reply

Your email address will not be published. Required fields are marked *