R Markdown

This is an R Markdown file (http://rmarkdown.rstudio.com), which has been Knit (i.e., compiled) to .html and published to RPubs. When working with .Rmd files, you will likely see something more like Supplemental Figure 1 (below) than a finished product like this page. But markdown is an easy to learn syntax and creating .html, .pdf, .rtf, and MS Word documents (.doc) can be done with the click of the Knit button or by using the render() command in the console.

The advantages to R Markdown is that you can combine R code (.r) with markdown syntax (.md) to narrate or annotate your analysis. You can even cite literature by calling to the reference in a nearby .bib file with the format @lastnameYear. For example, @Baker2016 pulls the information from references.bib (see Fig. 1) and uses the american-fisheries-society.csl file to format the citation, Baker et al. (2016), and reference (see References section below). You just need to make sure all the neccessary files are in the same directory (i.e., folder).

Annotating analysis

The utility of R Markdown really comes into play when you are analyzing data. First, you always want to write clean code, see Wickham’s The tidyverse style guide at https://style.tidyverse.org. Also, be kind to your future self and others by commenting on code (i.e., answer, what does this fuction do?). We can do this nicely with R Markdown:

# load FSA as we will need a few of it's functions
if (!require(FSA)){  # check to see if this package is installed
    install.packages("FSA")  # if not install it (or update)
    library(FSA)  # load the package
}
## Loading required package: FSA
## ## FSA v0.8.24. See citation('FSA') if used in publication.
## ## Run fishR() for related website and fishR('IFAR') for related book.
# load FSAdata as we will need some data
if (!require(FSAdata)){  # two spaces before " # " is recommended when line commenting
    install.packages("FSAdata")  # in R Studio you can auto-style code...
    library(FSAdata)  # ...just highlight the code and press ctrl + shift + A
}
## Loading required package: FSAdata
## ## FSAdata v0.3.8. See ?FSAdata to find data for specific fisheries analyses.
# load magrittr as we will be piping
if (!require(magrittr)){  
    install.packages("magrittr")  
    library(magrittr)  
}
## Loading required package: magrittr
# load dplyr as we will need mutate function
if (!require(dplyr)){  
    install.packages("dplyr")  
    library(dplyr)  
}
## Loading required package: dplyr
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
# read in data
ruf <- FSAdata::RuffeSLRH92 %>%  # that, " %>% ", is a pipe
    mutate(logW = log10(weight), logL = log10(length)) %>%  # it basically says, with this data (line 42)...
    select(-fish.id, -day)  # ...mutate (line 43) and select (line 44)

# have a look at first and last few lines of data
headtail(ruf)
##     month year indiv location length weight     sex     maturity age
## 1       4 1992     1   160170     90    9.3    male         ripe  NA
## 2       4 1992     2   160170    128   32.5  female         ripe  NA
## 3       4 1992     3   160170    112   19.0    male         ripe  NA
## 736     8 1992   734       11     41    1.1 unknown          yoy  NA
## 737     8 1992   735       11     26    0.2 unknown          yoy  NA
## 738     7 1992   736   150140    160   52.9  female nearly.spent  NA
##            logW     logL
## 1    0.96848295 1.954243
## 2    1.51188336 2.107210
## 3    1.27875360 2.049218
## 736  0.04139269 1.612784
## 737 -0.69897000 1.414973
## 738  1.72345567 2.204120
# plot the relationship between weight and length
plot(
    weight ~ length,  # take form: y-axis data ~ x-axis data (column names from dataframe)
    data = ruf,  # name of the dataframe
    pch = 19,  # point shape
    col = rgb(0, 0, 0, 0.3),  # point color
    xlab = "Total Length (mm)",  # x-axis label
    ylab = "Weight (g)"  # y-axis label
)

I have written another code chunk, but hidden the R code. So all you will see here is the output.

The thing is though I can annotate what and why I did something. For example:

Exploratory vizualizations suggested the relationship between weight and length was logarithmic in nature (see above). We therefore applied a log transformation to both weight and length variables, then re-plot the data. The log-transformed data appeared they would meet the assumption of linearity better than raw values. However, inspection of the quantile-quantile plot (Q–Q plot) and residual vs. fitted scatterplot indicate that model assumptions are not perfectly met.

The above data and plots were modified from scripts written by Derek Ogle and are available at http://derekogle.com/IFAR/scripts/. I also used a couple of his R packages, FSA (Ogle et al. 2019) and FSAdata. (There is a lot of cool stuff on his websites and his book, Introductory Fisheries Analyses with R, is super helpful). The great thing is, I just write the code and R Markdown places them in my document automatically. And even if I want to use a figure or image on my computer it’s as easy as ![text here](path-to-image).

I hope this very short R Markdown example has excited and inspired you to try it yourself. More thorough examples and references can be found in the main text to help get you started. If you would like to reproduce this very notebook on your local machine a .zip file can be downloaded here: https://www.researchgate.net/publication/334067406_Supplemental_Material_An_Example_R_Markdown_Analysis_Notebook.

References

Baker, B., C. Aldridge, and A. Omer. 2016. Water: Availability and Use, Publucation 3011. Mississippi State University; Mississippi State University Extension, Starkville, MS.

Ogle, D. H., P. Wheeler, and A. Dinno. 2019. FSA: Fisheries stock analysis.