class: center, middle, inverse, title-slide .title[ # Session 7 ] .subtitle[ ## More Rmarkdown ] --- ## Typography Text can be **bold**, _italic_, ~~strikethrough~~, or `inline code`. ```markdown *italic* **bold** ~~strikethrough~~ `inline code` [link](https://example.com) ``` --- ### Code Blocks #### R Code ```r ggplot(gapminder) + aes(x = gdpPercap, y = lifeExp, size = pop, color = country) + geom_point() + facet_wrap(~year) ``` --- ## Tables ```r knitr::kable(head(mtcars), format = 'html') ``` <table> <thead> <tr> <th style="text-align:left;"> </th> <th style="text-align:right;"> mpg </th> <th style="text-align:right;"> cyl </th> <th style="text-align:right;"> disp </th> <th style="text-align:right;"> hp </th> <th style="text-align:right;"> drat </th> <th style="text-align:right;"> wt </th> <th style="text-align:right;"> qsec </th> <th style="text-align:right;"> vs </th> <th style="text-align:right;"> am </th> <th style="text-align:right;"> gear </th> <th style="text-align:right;"> carb </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> Mazda RX4 </td> <td style="text-align:right;"> 21.0 </td> <td style="text-align:right;"> 6 </td> <td style="text-align:right;"> 160 </td> <td style="text-align:right;"> 110 </td> <td style="text-align:right;"> 3.90 </td> <td style="text-align:right;"> 2.620 </td> <td style="text-align:right;"> 16.46 </td> <td style="text-align:right;"> 0 </td> <td style="text-align:right;"> 1 </td> <td style="text-align:right;"> 4 </td> <td style="text-align:right;"> 4 </td> </tr> <tr> <td style="text-align:left;"> Mazda RX4 Wag </td> <td style="text-align:right;"> 21.0 </td> <td style="text-align:right;"> 6 </td> <td style="text-align:right;"> 160 </td> <td style="text-align:right;"> 110 </td> <td style="text-align:right;"> 3.90 </td> <td style="text-align:right;"> 2.875 </td> <td style="text-align:right;"> 17.02 </td> <td style="text-align:right;"> 0 </td> <td style="text-align:right;"> 1 </td> <td style="text-align:right;"> 4 </td> <td style="text-align:right;"> 4 </td> </tr> <tr> <td style="text-align:left;"> Datsun 710 </td> <td style="text-align:right;"> 22.8 </td> <td style="text-align:right;"> 4 </td> <td style="text-align:right;"> 108 </td> <td style="text-align:right;"> 93 </td> <td style="text-align:right;"> 3.85 </td> <td style="text-align:right;"> 2.320 </td> <td style="text-align:right;"> 18.61 </td> <td style="text-align:right;"> 1 </td> <td style="text-align:right;"> 1 </td> <td style="text-align:right;"> 4 </td> <td style="text-align:right;"> 1 </td> </tr> <tr> <td style="text-align:left;"> Hornet 4 Drive </td> <td style="text-align:right;"> 21.4 </td> <td style="text-align:right;"> 6 </td> <td style="text-align:right;"> 258 </td> <td style="text-align:right;"> 110 </td> <td style="text-align:right;"> 3.08 </td> <td style="text-align:right;"> 3.215 </td> <td style="text-align:right;"> 19.44 </td> <td style="text-align:right;"> 1 </td> <td style="text-align:right;"> 0 </td> <td style="text-align:right;"> 3 </td> <td style="text-align:right;"> 1 </td> </tr> <tr> <td style="text-align:left;"> Hornet Sportabout </td> <td style="text-align:right;"> 18.7 </td> <td style="text-align:right;"> 8 </td> <td style="text-align:right;"> 360 </td> <td style="text-align:right;"> 175 </td> <td style="text-align:right;"> 3.15 </td> <td style="text-align:right;"> 3.440 </td> <td style="text-align:right;"> 17.02 </td> <td style="text-align:right;"> 0 </td> <td style="text-align:right;"> 0 </td> <td style="text-align:right;"> 3 </td> <td style="text-align:right;"> 2 </td> </tr> <tr> <td style="text-align:left;"> Valiant </td> <td style="text-align:right;"> 18.1 </td> <td style="text-align:right;"> 6 </td> <td style="text-align:right;"> 225 </td> <td style="text-align:right;"> 105 </td> <td style="text-align:right;"> 2.76 </td> <td style="text-align:right;"> 3.460 </td> <td style="text-align:right;"> 20.22 </td> <td style="text-align:right;"> 1 </td> <td style="text-align:right;"> 0 </td> <td style="text-align:right;"> 3 </td> <td style="text-align:right;"> 1 </td> </tr> </tbody> </table> --- exclude: false ```r DT::datatable(head(mtcars), fillContainer = FALSE, options = list(pageLength = 4)) ```
--- .pull-left[ #### Here is an unordered list: * Item foo * Item bar * Item baz * Item zip ] #### The markdown is: .pull-left[ ```markdown * Item foo * Item bar ``` ] --- .pull-left[ #### And an ordered list: ```markdown 1. Item one 1. Item two 1. Item three 1. Item four ``` ] .pull-right[ #### And an ordered list: 1. Item one 1. Item two 1. Item three 1. Item four ] --- ### And a nested list: - level 1 item - level 2 item - level 2 item - level 3 item - level 3 item - level 1 item - level 2 item - level 2 item - level 2 item - level 1 item - level 2 item - level 2 item - level 1 item --- ### Nesting an ol in ul in an ol - level 1 item (ul) 1. level 2 item (ol) 1. level 2 item (ol) - level 3 item (ul) - level 3 item (ul) - level 1 item (ul) 1. level 2 item (ol) 1. level 2 item (ol) - level 3 item (ul) - level 3 item (ul) 1. level 4 item (ol) 1. level 4 item (ol) - level 3 item (ul) - level 3 item (ul) - level 1 item (ul) --- # Images ![img](heart_rate_runs.png) --- ## Data Visualization - ggplot is great for fast exploratory plots. - It has great tools for creating plots for communicating your results. - In this scenario, the emphasis should be on the __story__ you are trying to tell. --- ## Themes - Branding [example from the BBC](https://bbc.github.io/rcookbook/#make_a_line_chart) --- ## Default theme - Is called theme_grey() ```r ggplot(diff_df, aes(x = pts_diff, y = win_pct)) + geom_point() + theme_grey() ``` <img src="index_files/figure-html/unnamed-chunk-5-1.png" width="100%" /> - Grey panel background - white grid lines --- ## GGthemes - package with some well known themes: - theme_fivethirtyeight() - theme_economist() --- ## Theme elements - theme elements can be lines, text, etc. - To specify an option, you use the corresponding element. --- ## Centering titles and subtitles - When styling text, use element_text. ```r theme( plot.title=element_text(hjust=.5) ) ``` --- ## Grids - When styling lines, use element_line ```r theme( panel.grid.major=element_line(color="red") ) ``` --- ## Labels ```r plot <- nfl_stand %>% mutate( color = case_when( season < 2020 & seed <= 6 ~ "blue", season == 2020 & seed <= 7 ~ "blue", TRUE ~ "red" ) ) %>% ggplot(aes(x = as.numeric(pts_diff), y = win_pct)) + geom_vline(xintercept = 0, size = 0.75, color = "#737373") + geom_point( aes(color = color), size = 3, alpha = 0.8 ) + scale_color_identity() + labs(x = "Points Differential", y = "Win Percent", title = "Playoff teams typically have a positive point differential", subtitle = "Data through week 15 of the 2020 NFL Season", caption = "Plot: @thomas_mock | Data: ESPN") ``` --- ```r plot ``` <img src="index_files/figure-html/unnamed-chunk-9-1.png" width="100%" /> --- ## Scales ```r plt_scales <- nfl_stand %>% mutate( color = case_when( season < 2020 & seed <= 6 ~ "blue", season == 2020 & seed <= 7 ~ "blue", TRUE ~ "red" ) ) %>% ggplot(aes(x = as.numeric(pts_diff), y = win_pct)) + geom_vline(xintercept = 0, size = 0.75, color = "#737373") + geom_point(aes(color=color)) + scale_color_identity()+ scale_y_continuous( labels = scales::percent_format(accuracy = 1), breaks = seq(.0, 1, by = .10) ) + scale_x_continuous( breaks = seq(-200, 250, by = 50) ) + labs(x = "Points Differential", y = "Win Percent", title = "Playoff teams typically have a positive point differential", subtitle = "Data through week 15 of the 2020 NFL Season", caption = "Plot: @thomas_mock | Data: ESPN") ``` --- ```r plt_scales ``` <img src="index_files/figure-html/unnamed-chunk-11-1.png" width="100%" /> --- ## Experiment with themes ```r plot + ggthemes::theme_economist() ``` <img src="index_files/figure-html/unnamed-chunk-12-1.png" width="100%" /> ## Final plot ```r library(ggtext) playoff_label_scatter <- tibble( pts_diff = c(25,-125), y = c(0.3, 0.8), label = c("Missed<br>Playoffs", "Made<br>Playoffs"), color = c("#D50A0A", "#013369") ) playoff_diff_plot <- nfl_stand %>% mutate( pts_diff=as.numeric(pts_diff), color = case_when( season < 2020 & seed <= 6 ~ "#013369", season == 2020 & seed <= 7 ~ "#013369", TRUE ~ "#D50A0A" ) ) %>% ggplot(aes(x = pts_diff, y = win_pct)) + geom_vline(xintercept = 0, size = 0.75, color = "#737373") + geom_hline(yintercept = 0, size = 0.75, color = "#737373") + geom_point( aes(color = color), size = 3, alpha = 0.8 ) + # ggtext::geom_richtext( # data = playoff_label_scatter, # aes(x = pts_diff, y = y, label = label, color = color), # fill = "#f0f0f0", label.color = NA, # remove background and outline # label.padding = grid::unit(rep(0, 4), "pt"), # remove padding # family = "Chivo", hjust = 0.1, fontface = "bold", # size = 8 # ) + scale_color_identity() + labs(x = "Points Differential", y = "Win Percent", title = "Playoff teams typically have a positive point differential", subtitle = "Data through week 15 of the 2020 NFL Season", caption = str_to_upper("Plot: @thomas_mock | Data: ESPN")) + scale_y_continuous( labels = scales::percent_format(accuracy = 1), breaks = seq(.0, 1, by = .10) ) + scale_x_continuous( breaks = seq(-200, 250, by = 50) ) + theme(plot.subtitle=element_text(hjust=.5)) ```