Get startedGet started for free

Plot options

1. Plot options

Now that we've added plots to the report, let's discuss how to modify each plot and how they appear throughout the report.

2. Figure dimensions

Oftentimes we'll want to modify the figure dimensions. There are a number of ways to do this. The first, is using fig-dot-width and fig-dot-height to specify the width and height of the figure in inches in the final report.

3. Figure dimensions

For example, if we want the figure to be 5 by 3 inches, we put a comma after the code chunk name within the curly braces and include fig-dot-width equals 5 and fig-dot-height equals 3. Note that each additional option is separated by a comma.

4. Figure dimensions

Alternatively, instead of specifying these dimensions separately, we can use fig-dot-dim equals c 5 comma 3 in parentheses, and we'll get the same result with a single argument instead of two.

5. Output dimensions

We're also able to modify the dimensions of the figure using out-dot-width and out-dot-height.

6. Output dimensions

These arguments determine the dimensions of the output of the images in the knit document, and are specified as percentages. For example, out-dot-width equals 50 percent, will result in the figure occupying 50 percent of the width of the report once it's knit. We include the percentage in quotations. Here, we use single quotes but double quotes can also be used. Specifically using single or double quotes is important when quotes are used within quotes, for example, when we added formatting to the date field in the YAML header.

7. Figure alignment

We can also modify the alignment of each figure in the report using fig-dot-align. The options for this are left, right, or center, listed in quotations.

8. Figure alignment

Here, we've used left alignment for the figure and see that this is reflected in the knit document.

9. Local vs. global options

As we set the options for the figures in each code chunk, we may notice that some code chunk options are repeated throughout the report. When this happens, we may want to consider setting these options globally instead of locally. When we're specifying options locally, we add them to each code chunk and they impact only the code contained in that chunk. If we set the options globally instead, they will apply to all figures throughout the code chunks in the report.

10. Setting options globally

Notice the code chunk that appears when we create a new Markdown file, at the beginning of the file, in the code chunk named setup. In this chunk, the knitr package is being called with knitr and two colons. The knitr package is what runs each code chunk and knits the document. In this chunk, knitr will use the specified options as global defaults for each chunk in the document when the file is knit. Following opts_chunk dollar sign set(), we can add the options that we want to set globally to the parentheses before the echo equals TRUE argument. For example, here we add fig-dot-align equals left in quotations, and when we knit the report we see the alignment reflected in all figures throughout the document. We'll discuss the include and echo options that appear in this chunk later in the course.

11. Adding captions

In addition to modifying the formatting of the figure, we can also add a caption to each figure, using fig-dot-cap and including the caption in quotations. Here, we've added a caption to label the first plot as Figure 1-point-1 and to specify which fiscal years the plot displays.

12. Let's practice!

Let's practice!

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.