R Markdown and Images

R Markdown has great options for both generating your images on the fly (at knit-time) and embedding existing images. I’ll describe here options for displaying existing pre-rendered images, like charts, graphs, and illustrations.

How to Include Images

The working directory for locating images is the very same directory where your .Rmd document is saved. Place your images in the same folder and life will be easy. If you are inclined to organize, you can also save them in a folder that is within the folder where your .Rmd file is saved, say, in a folder named ‘figures’ or ‘images’. If you do that, you just have to add the directory to each of your reference to the image file name. But the base, working directory where images will be first searched for is the folder where the .Rmd file is saved. In this tutorial, that is where the images are located.

R can deal with many image types, I find myself most frequently using .PNG, .JPG / .JPEG, and .TIF images. It is good to save them at a resolution around 300 dpi, but the code can handle any resolution you choose. The easiest way to include images is by inserting text like the following:

![](Theory.jpg)

Which produces this:

Scaling an Image

For this we use the function include_graphics in the knitr package. This is R code and must be placed inside a code chunk, unlike the call to ![](Theory.jpg) used in the first example, which lives in the text area (and is pure Markdown syntax).

A great thing about using knitr::include_graphics("Theory.jpg") is that you can add on scaling specifications to the chunk header, such as out.width = "50%" and they will be reflected in the displayed image.

Which produces this:

Another import chunk header option is fig.align='center'

Which produces this:

Figure Captions

A caption can be added in the chunk header.

The option is:

fig.cap="Figure 1. The hallowed theory of sampling variation and sampling distributions of parameter estimates"

Which produces this:

Figure 1. The hallowed theory of sampling variation and sampling distributions of parameter estimates

Figure 1. The hallowed theory of sampling variation and sampling distributions of parameter estimates

Header 5: A Better Figure Caption Option

Personally, I like to use headers of rank 5 to denote my figure captions.

##### Figure 2. Odocoileus hemionus metatarsals

I like the bold font they produce, and modest font size. I also like that this kind of captions is written out in the text area of the document, not buried inside the chunk header (who wants their text to be inside a chunk header?). This way, they make my documents easier to navigate, and they show up in the outline / Table of Contents shown in RStudio just to the right of the code pane. This is another great aid for navigating your document when writing.

Figure 2. Odocoileus hemionus metatarsals

Further Reading

There are many helpful guides that go into greater depth on this topic. The beautiful text by Yihui Xie written to describe Bookdown is very helpful and includes great R Markdown coding suggestions.

I hope this was useful!

-Brian

brianwoodresearch.com