0.1 Load packages

library(knitr)
library(dplyr)
library(ggplot2)

0.2 R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00

0.3 Including Plots

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.

0.4 R Markdown syntax

Plain text

End a line with two spaces

to start a new paragraph.

italics and italics

bold and bold

superscript2

strikethrough

link

1 Header 1

1.1 Header 2

1.1.1 Header 3

1.1.1.1 Header 4

1.1.1.1.1 Header 5
1.1.1.1.1.1 Header 6

endash: –

emdash: —

ellipsis: …

inline equation: \(A = \pi*r^{2}\)

image:

horizontal rule (or slide break):



block quote

  • unordered list
  • item 2
  • sub-item 1
  • sub-item 2
  1. ordered list
  2. item 2
  • sub-item 1
  • sub-item 2
Table Header Second Header
Table Cell Cell 2
Cell 3 Cell 4

##############START HERE ##############START HERE ##############START HERE ##############START HERE ##############START HERE ##############START HERE

output: html_document: toc: yes toc_float: yes number_sections: yes

1.2 Add additonal settings to YAML

1.3 Images and Figures

Make and print plot directly in markdown
weight versus fuel efficiency

weight versus fuel efficiency

Save plot to computer (will save to rmd working directory, not r working directory):

Print plot in markdown from computer

1.4 Table

By default, the table outputs produced in R Markdown will look like the output you would see in your console.

##   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1          5.1         3.5          1.4         0.2  setosa
## 2          4.9         3.0          1.4         0.2  setosa
## 3          4.7         3.2          1.3         0.2  setosa
## 4          4.6         3.1          1.5         0.2  setosa
## 5          5.0         3.6          1.4         0.2  setosa
## 6          5.4         3.9          1.7         0.4  setosa

However, if you prefer that data be displayed with additional formatting you can use the knitr::kable function. For example:

Sepal.Length Sepal.Width Petal.Length Petal.Width Species
5.1 3.5 1.4 0.2 setosa
4.9 3.0 1.4 0.2 setosa
4.7 3.2 1.3 0.2 setosa
4.6 3.1 1.5 0.2 setosa
5.0 3.6 1.4 0.2 setosa
5.4 3.9 1.7 0.4 setosa
speed dist
Min. : 4.0 Min. : 2.00
1st Qu.:12.0 1st Qu.: 26.00
Median :15.0 Median : 36.00
Mean :15.4 Mean : 42.98
3rd Qu.:19.0 3rd Qu.: 56.00
Max. :25.0 Max. :120.00

You can also customize these more

Sepal Length Sepal Width Petal Length Petal Width Species
5.1 3.5 1.4 0.2 setosa
4.9 3.0 1.4 0.2 setosa
4.7 3.2 1.3 0.2 setosa
4.6 3.1 1.5 0.2 setosa
5.0 3.6 1.4 0.2 setosa
5.4 3.9 1.7 0.4 setosa

Make new column headers entirely

We Need Five Names Here
5.1 3.5 1.4 0.2 setosa
4.9 3.0 1.4 0.2 setosa
4.7 3.2 1.3 0.2 setosa
4.6 3.1 1.5 0.2 setosa
5.0 3.6 1.4 0.2 setosa
5.4 3.9 1.7 0.4 setosa

Specify alignment

Sepal.Length Sepal.Width Petal.Length Petal.Width Species
5.1 3.5 1.4 0.2 setosa
4.9 3.0 1.4 0.2 setosa
4.7 3.2 1.3 0.2 setosa
4.6 3.1 1.5 0.2 setosa
5.0 3.6 1.4 0.2 setosa
5.4 3.9 1.7 0.4 setosa

Specify all center alignment

Sepal.Length Sepal.Width Petal.Length Petal.Width Species
5.1 3.5 1.4 0.2 setosa
4.9 3.0 1.4 0.2 setosa
4.7 3.2 1.3 0.2 setosa
4.6 3.1 1.5 0.2 setosa
5.0 3.6 1.4 0.2 setosa
5.4 3.9 1.7 0.4 setosa

Add caption

Table 1. An example table caption.
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
5.1 3.5 1.4 0.2 setosa
4.9 3.0 1.4 0.2 setosa
4.7 3.2 1.3 0.2 setosa
4.6 3.1 1.5 0.2 setosa
5.0 3.6 1.4 0.2 setosa
5.4 3.9 1.7 0.4 setosa

Add model summary table

Table 1. An example table caption.
Estimate Std. Error t value Pr(>|t|)
(Intercept) 6.5262226 0.4788963 13.627631 0.0000000
Sepal.Width -0.2233611 0.1550809 -1.440287 0.1518983

Change row and col names

Table 1. An example table caption.
Estimate Std. Error t value p value
Intercept 6.5262226 0.4788963 13.627631 0.0000000
Sepal Width -0.2233611 0.1550809 -1.440287 0.1518983
Add formatting using extra styles from package “kableExtra” –> will only work for HTML documents Doing this for word output: https://taehoonh.me/content/post/alternative-to-kable-function-when-knitting-to-ms-word.html
Table 1. An example table caption.
Estimate Std. Error t value p value
Intercept 6.5262226 0.4788963 13.627631 0.0000000
Sepal Width -0.2233611 0.1550809 -1.440287 0.1518983

Your turn: Create a markdown document in which you 1. Create a ggplot of the cty versus displ from the mpg data set 2. Add a linear regression line to your plot 3. Run a linear model and print the output table in your markdown doc

2 Plot

3 Table

Table 1. City miles per gallon versus engine displacement.
Estimate Std. Error t value p value
(Intercept) 25.991467 0.4821444 53.90805 0
displ -2.630482 0.1301887 -20.20515 0