0.1 What can RMarkdown be used for?

  1. HTML Reports & PDF Reports
  2. HTML Slide Decks & PowerPoint
  3. Interactive Dashboards
  4. Books with bookdown
  5. Websites with blogdown

0.2 Key Resources

# PDF Knitting Setup: https://yihui.name/tinytex/ 
# install.packages("tintex")
# tinytex::install_tinytex()

1 Write with Markdown

2 Header 1

2.1 Header 2

2.1.1 Header 3

Plain text.

Type bold.

Type italics.

Type bold + italics.

Talk about code - the tidyverse is awesome

Unordered List:

  • Item 1
  • Item 2

Ordered List:

  1. First point
  2. Second point
  3. More points

3 Tabset

3.1 Tab 1

This is Tab 1

3.2 Tab 2

This is Tab 2

4 Images

PSU Logo
PSU Logo

5 Code

# Import stock prices
stocks <- tq_get(c("TSLA", "AMZN"),
                 get = "stock.prices",
                 from = "2016-01-01",
                 to = "2017-01-01")
stocks
## # A tibble: 504 × 8
##    symbol date        open  high   low close    volume adjusted
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>     <dbl>    <dbl>
##  1 TSLA   2016-01-04  15.4  15.4  14.6  14.9 102406500     14.9
##  2 TSLA   2016-01-05  15.1  15.1  14.7  14.9  47802000     14.9
##  3 TSLA   2016-01-06  14.7  14.7  14.4  14.6  56686500     14.6
##  4 TSLA   2016-01-07  14.3  14.6  14.2  14.4  53314500     14.4
##  5 TSLA   2016-01-08  14.5  14.7  14.1  14.1  54421500     14.1
##  6 TSLA   2016-01-11  14.3  14.3  13.5  13.9  61371000     13.9
##  7 TSLA   2016-01-12  14.1  14.2  13.7  14.0  46378500     14.0
##  8 TSLA   2016-01-13  14.1  14.2  13.3  13.4  61896000     13.4
##  9 TSLA   2016-01-14  13.5  14    12.9  13.7  97360500     13.7
## 10 TSLA   2016-01-15  13.3  13.7  13.1  13.7  83679000     13.7
## # ℹ 494 more rows

6 Plots

Plotting works as expected. Try changing:

Static plots:

g <- ggplot(data = stocks) + 
  geom_point(mapping = aes(x = volume, y = adjusted, color = symbol))

g
Revenue by Category

Revenue by Category

Interactive plots:

ggplotly(g)

7 Tables

Static Tables:

stocks %>% head() %>% knitr::kable()
symbol date open high low close volume adjusted
TSLA 2016-01-04 15.38133 15.42533 14.60000 14.89400 102406500 14.89400
TSLA 2016-01-05 15.09067 15.12600 14.66667 14.89533 47802000 14.89533
TSLA 2016-01-06 14.66667 14.67000 14.39867 14.60267 56686500 14.60267
TSLA 2016-01-07 14.27933 14.56267 14.24467 14.37667 53314500 14.37667
TSLA 2016-01-08 14.52400 14.69600 14.05133 14.06667 54421500 14.06667
TSLA 2016-01-11 14.26733 14.29667 13.53333 13.85667 61371000 13.85667

Dynamic Tables:

stocks 
## # A tibble: 504 × 8
##    symbol date        open  high   low close    volume adjusted
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>     <dbl>    <dbl>
##  1 TSLA   2016-01-04  15.4  15.4  14.6  14.9 102406500     14.9
##  2 TSLA   2016-01-05  15.1  15.1  14.7  14.9  47802000     14.9
##  3 TSLA   2016-01-06  14.7  14.7  14.4  14.6  56686500     14.6
##  4 TSLA   2016-01-07  14.3  14.6  14.2  14.4  53314500     14.4
##  5 TSLA   2016-01-08  14.5  14.7  14.1  14.1  54421500     14.1
##  6 TSLA   2016-01-11  14.3  14.3  13.5  13.9  61371000     13.9
##  7 TSLA   2016-01-12  14.1  14.2  13.7  14.0  46378500     14.0
##  8 TSLA   2016-01-13  14.1  14.2  13.3  13.4  61896000     13.4
##  9 TSLA   2016-01-14  13.5  14    12.9  13.7  97360500     13.7
## 10 TSLA   2016-01-15  13.3  13.7  13.1  13.7  83679000     13.7
## # ℹ 494 more rows

8 Footnotes

This is some text with a Footnote1. This is a second Footnote2.

9 Code Along 1 – Data Visualization with ggplot2

9.1 Viewing the mpg Dataset

# View the built-in mpg dataset
mpg
## # A tibble: 234 × 11
##    manufacturer model      displ  year   cyl trans drv     cty   hwy fl    class
##    <chr>        <chr>      <dbl> <int> <int> <chr> <chr> <int> <int> <chr> <chr>
##  1 audi         a4           1.8  1999     4 auto… f        18    29 p     comp…
##  2 audi         a4           1.8  1999     4 manu… f        21    29 p     comp…
##  3 audi         a4           2    2008     4 manu… f        20    31 p     comp…
##  4 audi         a4           2    2008     4 auto… f        21    30 p     comp…
##  5 audi         a4           2.8  1999     6 auto… f        16    26 p     comp…
##  6 audi         a4           2.8  1999     6 manu… f        18    26 p     comp…
##  7 audi         a4           3.1  2008     6 auto… f        18    27 p     comp…
##  8 audi         a4 quattro   1.8  1999     4 manu… 4        18    26 p     comp…
##  9 audi         a4 quattro   1.8  1999     4 auto… 4        16    25 p     comp…
## 10 audi         a4 quattro   2    2008     4 manu… 4        20    28 p     comp…
## # ℹ 224 more rows

The mpg dataset contains fuel economy data for different car models. Each row is a car, and columns show info like engine size, fuel type, and MPG.


9.2 Basic Scatterplot: Engine Size vs Highway MPG

ggplot(data = mpg) +
  geom_point(aes(x = displ, y = hwy))

As engine displacement (displ) increases, highway fuel efficiency (hwy) tends to decrease. This shows a negative trend.


9.3 Adding Color by Class and Facet Wrap

ggplot(data = mpg) +
  geom_point(aes(x = displ, y = hwy, color = class)) +
  facet_wrap(~ class)

Here, each point is colored by class, and facet_wrap() splits the plot into smaller charts for each vehicle type.

10 Apply the dplyr Verbs – Chapter 5

10.1 Filter Rows

stocks |> 
  filter(adjusted > 500)
## # A tibble: 0 × 8
## # ℹ 8 variables: symbol <chr>, date <date>, open <dbl>, high <dbl>, low <dbl>,
## #   close <dbl>, volume <dbl>, adjusted <dbl>

10.2 arrange

stocks |> 
  arrange(desc(adjusted))
## # A tibble: 504 × 8
##    symbol date        open  high   low close   volume adjusted
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>    <dbl>    <dbl>
##  1 AMZN   2016-10-05  41.9  42.3  41.8  42.2 69382000     42.2
##  2 AMZN   2016-10-10  42.2  42.3  42.0  42.1 36542000     42.1
##  3 AMZN   2016-10-06  42.2  42.4  42.0  42.1 53680000     42.1
##  4 AMZN   2016-10-07  42.3  42.3  41.9  42.0 48524000     42.0
##  5 AMZN   2016-10-24  41.2  41.9  41.1  41.9 81218000     41.9
##  6 AMZN   2016-09-30  41.6  42.0  41.6  41.9 88612000     41.9
##  7 AMZN   2016-10-03  41.8  42.0  41.6  41.8 55388000     41.8
##  8 AMZN   2016-10-25  42.0  42.2  41.7  41.8 64968000     41.8
##  9 AMZN   2016-10-12  41.7  41.9  41.5  41.7 47608000     41.7
## 10 AMZN   2016-10-04  42.0  42.1  41.5  41.7 59006000     41.7
## # ℹ 494 more rows

10.3 select columns

stocks |> 
  select(symbol, date, adjusted)
## # A tibble: 504 × 3
##    symbol date       adjusted
##    <chr>  <date>        <dbl>
##  1 TSLA   2016-01-04     14.9
##  2 TSLA   2016-01-05     14.9
##  3 TSLA   2016-01-06     14.6
##  4 TSLA   2016-01-07     14.4
##  5 TSLA   2016-01-08     14.1
##  6 TSLA   2016-01-11     13.9
##  7 TSLA   2016-01-12     14.0
##  8 TSLA   2016-01-13     13.4
##  9 TSLA   2016-01-14     13.7
## 10 TSLA   2016-01-15     13.7
## # ℹ 494 more rows

##mutate columns

stocks |> 
  mutate(adjusted_log = log(adjusted))
## # A tibble: 504 × 9
##    symbol date        open  high   low close    volume adjusted adjusted_log
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>     <dbl>    <dbl>        <dbl>
##  1 TSLA   2016-01-04  15.4  15.4  14.6  14.9 102406500     14.9         2.70
##  2 TSLA   2016-01-05  15.1  15.1  14.7  14.9  47802000     14.9         2.70
##  3 TSLA   2016-01-06  14.7  14.7  14.4  14.6  56686500     14.6         2.68
##  4 TSLA   2016-01-07  14.3  14.6  14.2  14.4  53314500     14.4         2.67
##  5 TSLA   2016-01-08  14.5  14.7  14.1  14.1  54421500     14.1         2.64
##  6 TSLA   2016-01-11  14.3  14.3  13.5  13.9  61371000     13.9         2.63
##  7 TSLA   2016-01-12  14.1  14.2  13.7  14.0  46378500     14.0         2.64
##  8 TSLA   2016-01-13  14.1  14.2  13.3  13.4  61896000     13.4         2.59
##  9 TSLA   2016-01-14  13.5  14    12.9  13.7  97360500     13.7         2.62
## 10 TSLA   2016-01-15  13.3  13.7  13.1  13.7  83679000     13.7         2.61
## # ℹ 494 more rows

##summarize

stocks |>
  group_by(symbol) |>
  summarize(
    avg_price = mean(adjusted, na.rm = TRUE),
    .groups = "drop"
  )
## # A tibble: 2 × 2
##   symbol avg_price
##   <chr>      <dbl>
## 1 AMZN        35.0
## 2 TSLA        14.0

  1. Citation for Footnote 1↩︎

  2. Citation for Footnote 2↩︎