Individual Assignment - Working With R
Markdown Documents 2 Intro to Quantitative Ecology:
In addition to giving your code chunks names, can use chunk options to further customize the behavior of your code chunks in RMarkdown.
Take a moment to read the description of code chunks on this page.
Pay special attention to the code chunk options eval
,
echo
, and results
.
You can also use code chunks to control the appearance of your plots. The three most important chunk options are:
fig.width
: the height of the output figurefig.height
: the width of the output figurefig.asp
: the aspect ratio of the output figure.fig.align
: the alignment of the figure. May be
‘center’, ‘right’, or ‘left’.As an example, let’s plot a histogram of the penguin flipper lengths:
hist(
penguins$flipper_length_mm,
xlab = "Flipper Length (mm)",
main = "Histogram of Penguin Flipper Length")
To create this oversized plot, I’ve used the chunk options
fig.width = 10
and fig.height = 12
:
You can think of the aspect ratio as the ratio of the height of the plot to its width. For example, an aspect ratio of 1/2 will produce a plot that is twice as wide as it is tall.
Here, I’ve used manually set the aspect ratio to 1/2 with the chunk
option fig.asp = 0.5
.
How could you modify the chunk option to make the plot twice as high as it is wide?
One way to organize contents in your RMarkdown html document is to use tabsets. I use the extensively to organize materials on the course website and assignment pages.
Tabsets are related to the concept of headers in RMarkdown. The R Markdown Cheetsheet shows how to use the number symbol, #, to create different level headers.
To initiate a tabset, you must first create a new header. The name of
the header is then followed with the text {.tabset}
. Every
sub-header that follows will be in a separate tab. To terminate the
tabset, simply create another header of the same level as the first
one.
It’s easier to see the pattern using an example.
The following markdown produces a tabset:
## Example Tabset {.tabset}
The following subheaders will each be displayed in a tab:
### Tab 1
This is the *first* tab.
### Tab 2
The second tab is **much better**.
### Tab 3
I have a scatterplot:
```{r scatterplot_example_tab}
plot(rnorm(10), rnorm(10))
```
### Tab 4
I have a histogram
```{r histogram_example_tab, fig.aspect = 1/2}
hist(rnorm(100))
```
The following subheaders will each be displayed in a tab:
This is the first tab.
The second tab is much better.
I have a scatterplot:
plot(rnorm(10), rnorm(10))
I have a histogram
hist(rnorm(100))
It’s often helpful to add images and links to your document.
The syntax for both is very similar, we’ll start with links.
Links are easy in RMarkdown. The syntax is:
[link text](link address)
Note that the text you want to appear goes in the square brackets while the web address goes in the parentheses.
For example, if I wanted to post a link to one of my favorite videos on YouTube, I could use the following text (not in a code chunk):
Here is [a link to](https://youtu.be/D3tdW9l1690) a great video about map projections.
The resulting knitted text is:
Here is a link to a great video about map projections.
Images are a little bit more complicated, mainly because by default
RStudio will search for the image file in the directory where your
current .Rmd
file is located.
Fortunately, we can use inline R code and
the here()
function to access images in a common
location.
I can insert the following text (not in a code chunk) into my RMarkdown document to insert an image of my dog, Betsy:
![Picture of Betsy](`r here("data", "betsy_01.jpg")`)
Things to notice:
here()
function to
specify the location of my image file.data
subdirectory of
my main project folder.betsy_01.jpg
.You can customize your document’s look by using one of R Markdown’s
built-in themes. You can set the theme in the document’s YAML
header. For example, I like to use the readable
theme
for many of the pages for this class:
---
title: "mike's example page"
output:
html_document:
theme: readable
---
Check out this RMarkdown theme gallery for some examples.
If you have a long document, a floating table of contents helps the user to navigate easily.
Check out this page for an example of how to add a floating table of contents.
For this assignment, you’ll be creating a RMarkdown document using all of the new skills you’ve learned.
Your document needs to have the following first-level sections
For this section, you’ll use code chunk options to modify the appearance of your code and figures. In this section, you need to create three second-level headers. Your second-level headers will be tabs (review above to read about how to create a tabset)
In this tab, you’ll create a code chunk that plots a figure. You can use any data and plot type you like. For example, you might make a histogram of penguin flipper lengths.
In this tab, you’ll create another figure, but this time you need to use a code chunk option to hide the code.
In your last tab, you’ll create a third figure using code chunks to:
data
subdirectory.
Remember the filename.