Whenever I make graphs from R to be included in Latex, I find myself scuttling back and forth between R to specify the size of the output and Latex to play with the \includegraphics[scale=??].

It takes a lot of trial and error because:

  1. I don't see the change interactively. I only know whether something works after compiling (which adds up time annoyingly).
  2. Different R outputs, e.g. pdf, png, have different measurement units (pdf is in inch, png is in what unit? I'm not even sure). As someone who's only familiar with metric units, this adds to the confusion.
  3. When the graphics appears on Latex, it also gets changed yet another time due to the border length of the document.

Adding these three sources of unpredictability, it takes an inordinate amount of time to simply specify size. Ideally, I want to be making changes interactively only at one place instead of going back and forth.

Is there any best practice I should follow?

  • I use R and knitr quite frequently and never have the issue you are referring to. In order to speed up compilation time, I have a separate document all it does it process chunks, I use the chunk option dev=c('pdf','tikz') option to that both a pdf object and a tikz object gets built, then in the main document, I have a command which detects if the document is in draft mode, and if so, uses include graphics on the pdf object, else, uses the tikz version. In order to specify width and height, use the fig.width and fig.height chunk options, the measurements are in inches. – Nicholas Hamilton Jun 20 '13 at 1:44
  • 1
    Is knitr worth learning? I've read multiple opinions about this, and some people insist that data analysis and typesetting should be separate. But I'm not sure what is the reasoning behind that? – Heisenberg Jun 20 '13 at 1:45
  • I love it, you can write all your 'R' code in rstudio or the like, and simply read in the file in latex using knitr. Use the chunk flag ##@knitr MYCHUNKNAME to identify parts in your script. – Nicholas Hamilton Jun 20 '13 at 1:50
  • What you have heard is correct btw, you should keep them separate, but you also need a method to generate consistent graphs for your document, and ggplot2 / R / knitr is my preferred method as it so easy. – Nicholas Hamilton Jun 20 '13 at 2:14

I think what you may be looking for is instead of using the scale option you should use the width option in \includegraphics.

There are different widths you can use \textwidth or \linewidth are likely what you are looking for.

\includegraphics[width=.5\textwidth]{...} will scale your graphic to be half of the text width. If your graphic is inside an environment with a different line width, like a list, then you may want to use \linewidth instead of \textwidth.

You may find it useful to take a look at Werner's answer to a similar question.

  • Is it possible to specify height in latex as well since the ratio of the graphics also matters? I can specify the width+height in R, then specify width again in Latex, but eliminating one of the two steps would be nice. – Heisenberg Jun 20 '13 at 1:47
  • 1
    @Anh Specifying width + height in LaTeX is not recommended, as it would change the aspect ratio of the original image. Just output the graphs as PDF from R with the aspect ratio you want and a "big enough" resolution, then specify the desired width in LaTeX as a fraction of \textwidth, as GDanger suggests. – Xavier Jun 20 '13 at 1:51
  • I forgot to mention that you can also specify an absolute width such as [width=20cm] or [width=200pt]. Although best practice would be to fit to the width of the current environment unless a specific size is required. Of course, you could only specify height and have width adjusted to the aspect ratio, but best practice again would be to work with width. – GDanger Jun 20 '13 at 2:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.