10 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.
- Teaching 10 minutes
- Exercises 10 minutes
- How do I change the height and width of a figure?
- How to I change the type of output of a figure? (e.g., PDF, TIFF, PNG, JPG, SVG)
- Can I set all the figure features globally?
- How do I save the figures?
- Learn how to set individual figure height, width, aspect, and print size
- Learn how to set global parameters for your chunks
- Get a copy of all of your figures
10.4 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:
fig.align: How do you want your figure aligned? Takes one of the following inputs: “default”, “center”, “left”, or “right”? (demo)
fig.cap: Would you like a caption for your figure? It takes a character vector as input: “My Amazing Graph”
fig.width: How tall and wide would you like your figure in inches? Each takes one number (e.g., 7, or 9) [Note: these numbers are not quoted]
out.width: The height and width of your plot in the final file. This can be handy if you like the current aspect ratio of your plot, but you want to shrink it by say 50% - which you would do with “50%”. You can also include LaTeX output or HTML output. Say for example, “.8//linewidth” or “8cm” for LaTeX, or “300px” for HTML.
For demonstration purposes, let’s take a plot from earlier and show how it’s output can change.
10.4.1 Your Turn
- Open exercise
exercises/02-rmd-figures-chunks/02-rmd-figures-chunks.RmdCreate three figures, with the respective dimensions (
- Now add to those figures, the following:
fig.align = "center"
out.width = 50%
10.5 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:
(As mentioned earlier), I typically set this up in a code chunk at the start of a document.
10.5.1 Your Turn
- Set the global options in your document to set:
10.6 Keeping your figures
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
You’ll notice that this creates some folders called “FILENAME_files” - you can control the specific name of the folder by setting
fig.path like so:
Which would instead create a new folder aclled “figs”.
10.7 Your Turn
- Save your images in your document by setting