-
Notifications
You must be signed in to change notification settings - Fork 41
/
changing-figures.Rmd
94 lines (62 loc) · 3.55 KB
/
changing-figures.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# 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
* **Teaching** 10 minutes
* **Exercises** 10 minutes
## Questions
* 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?
## Objectives
* 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
## 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.height` & `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.height` & `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.
* with `fig.height`, `fig.width`, `out.width`, `dev`
### Your Turn {.exercise}
1. Open exercise `exercises/02-rmd-figures-chunks/02-rmd-figures-chunks.Rmd` Create three figures, with the respective dimensions (`fig.height` and `fig.width`)
* 2x2
* 10x10
* 4x7
2. Now add to those figures, the following:
* `fig.align = "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:
```r
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`:
```YAML
---
title: "Awesome report"
author: "You"
output:
html_document:
keep_md: true
---
```
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:
```r
knitr::opts_chunk$set(fig.path = "figs")
```
Which would instead create a new folder aclled "figs".
## Your Turn {.exercise}
1. Save your images in your document by setting `keep_md: true`
## Further Reading
- [Official knitr documentation](https://yihui.name/knitr/options/#plots)