rmd4sci-melbourne

Customising your figures

When you produce figures, you usually want to tweak them a little bit. A bit wider, perhaps a bit taller. Perhaps a different image type other than “png”, because the journal requires “TIFF” or “EPS”. Maybe you need 600dpi because you’re going to print it really big. So, how do you control these features?

You can control the size and features of figures with the chunk options. In this section, we are going to talk more specifically about how to customise your figures.

Overview

Questions

Objectives

Which chunk options should you care about for this?

There are many chunk options that control your output, but only a few that you really need to worry about for your figures:

For demonstration purposes, let’s take a plot from earlier and show how it’s output can change.

Your Turn {.exercise}

  1. Open exercise excercises/02-rmd-figures-chunks/02-rmd-figures-chunks.Rmd Create three figures, each with the following attributes:
    • 2x2
    • 10x10
    • 4x7
    • “center”
    • “out.width = 50%”

Setting global options

If we repeat adding the same chunk options for each figure, we might want to consider setting them globally. We can do this with the following code:

knitr::opts_chunk$set(chunk_option1 = TRUE, ...)

(As mentioned earlier), I typically set this up in a code chunk at the start of a document.

Your Turn {.exercise}

  1. Set the global options in your document to set:
    • fig.height
    • figh.width
    • dev

Keeping your figures {.demo}

You can set the options for your figures, which will change how they appear on the page, but this won’t save the figures anywhere. In order to save the figures to file, you need to edit the YAML option keep_md: true:

---
title: "Awesome report"
author: "You"
output:
  html_document:
    keep_md: true
---

Your Turn {.exercise}

  1. Save your images in your document by setting keep_md: true

Further Reading