class: middle, inverse .leftcol30[ <center> <img src="https://github.com/emse-madd-gwu/emse-madd-gwu.github.io/raw/master/images/logo.png" width=250> </center> ] .rightcol70[ # Week 3: .fancy[RMarkdown & Plotting] ###
EMSE 6035: Marketing Analytics for Design Decisions ###
John Paul Helveston ###
September 14, 2022 ] --- class: inverse, middle # Week 3: .fancy[RMarkdown & Plotting] ### 1. Intro to RMarkdown ### QUIZ 1 ### 2. Intro to ggplot2 ### 3. Project attributes & levels --- class: inverse, middle # Week 3: .fancy[RMarkdown & Plotting] ### 1. .orange[Intro to RMarkdown] ### QUIZ 1 ### 2. Intro to ggplot2 ### 3. Project attributes & levels --- class: center # "Literate programming" .leftcol[.left[ > ### Treat programs as a "literature" understandable to **human beings** ]] .rightcol[.center[ <center> <img src="images/Knuth.jpg" width=350> </center> [Donald E. Knuth](https://en.wikipedia.org/wiki/Donald_Knuth) ]] --- background-image: url(images/horst_monsters_rmarkdown.png) background-size: contain background-color: #FFFFFF .footnote[Art by [Allison Horst](https://twitter.com/allison_horst?lang=en)] --- # .center[Anatomy of a .Rmd file] <br> # YAML (**Y**et **A**nother **M**arkdown **L**anguage) # Markdown text # R code --- class: center, middle, inverse # Quick demo # Open `rmd_demo.Rmd`, then click "knit" --- # Define overall document options in YAML .leftcol[ Basic html page ```yaml --- title: Your title here author: Your name here output: html_document --- ``` ] .rightcol[ Add table of contents, change theme ```yaml --- title: Your title here author: Your name here output: html_document: toc: true toc_float: true theme: flatly --- ``` Other themes at https://bootswatch.com/ ] --- # ๐งถ Knit to multiple outputs ```r rmarkdown::render("rmd_demo.Rmd", output_format = "all") ``` .leftcol[ <img src="images/knit-dropdown.png" width="60%" style="display: block; margin: auto;" /> ] .rightcol[ ```yaml --- title: Your title here author: Your name here output: html_document: toc: true toc_float: true theme: flatly word_document: default pdf_document: default --- ``` ] --- # .center[Anatomy of a .Rmd file] <br> # ~~YAML (**Y**et **A**nother **M**arkdown **L**anguage)~~ # .red[Markdown text] # R code --- class: center # Right now, bookmark this! ๐ # https://commonmark.org/help/ <br><hr><br> # (When you have 10 minutes, do this! ๐) # https://commonmark.org/help/tutorial/ --- # .center[Headers] -- .leftcol[ ```markdown # HEADER 1 ## HEADER 2 ### HEADER 3 #### HEADER 4 ##### HEADER 5 ###### HEADER 6 ``` ] -- .rightcol[ # HEADER 1 ## HEADER 2 ### HEADER 3 #### HEADER 4 ##### HEADER 5 ###### HEADER 6 ] --- # .center[Basic Text Formatting] .leftcol[ ## Type this... - `normal text` - `_italic text_` - `*italic text*` - `**bold text**` - `***bold italic text***` - `~~strikethrough~~` - `` `code text` `` ] .rightcol[ ## ..to get this - normal text - _italic text_ - *italic text* - **bold text** - ***bold italic text*** - ~~strikethrough~~ - `code text` ] --- class: top # .center[Lists] .leftcol[ Bullet list: ```r - first item - second item - third item ``` - first item - second item - third item ] .rightcol[ Numbered list: ```r 1. first item 2. second item 3. third item ``` 1. first item 2. second item 3. third item ] --- # .center[Links] Simple **url link** to another site: ```r [Download R](http://www.r-project.org/) ``` [Download R](http://www.r-project.org/) --- class: center, middle, inverse # Back to `rmd_demo.Rmd` --- # .center[Anatomy of a .Rmd file] <br> # ~~YAML (**Y**et **A**nother **M**arkdown **L**anguage)~~ # ~~Markdown text~~ # .red[R code] --- class: center # R Code -- .leftcol[ ## Inline code .left[ ```r `r insert code here` ``` ]] -- .rightcol[ ## Code chunks .left[ ````markdown ```{r} insert code here insert more code here ``` ```` ]] --- # Inline R code ```r The sum of 3 and 4 is `r 3 + 4` ``` -- Produces this: The sum of 3 and 4 is 7 --- # R Code chunks .leftcol[ This code chunk... ````markdown ```{r} bears %>% count(month) ``` ```` ] -- .rightcol[ ...will produce this when compiled: ```r bears %>% count(month) ``` ``` #> # A tibble: 12 ร 2 #> month n #> <dbl> <int> #> 1 1 3 #> 2 2 1 #> 3 3 1 #> 4 4 4 #> 5 5 18 #> 6 6 20 #> 7 7 27 #> 8 8 28 #> 9 9 25 #> 10 10 25 #> 11 11 12 #> 12 12 2 ``` ] --- # Chunk options Control what chunks output using options inside `{r}`: Example: `{r, echo=FALSE, message=FALSE}` <img src="images/chunks_options.png" width="60%" /> --- # .center[Chunk output options] .center[By default, code chunks print **code** + **output**] -- .cols3[ ````markdown ```{r, echo=FALSE} cat('hello world!') ``` ```` Prints only **output**<br>(doesn't show code) ``` #> hello world! ``` ] -- .cols3[ ````markdown ```{r, eval=FALSE} cat('hello world!') ``` ```` Prints only **code**<br>(doesn't run the code) ```r cat('hello world!') ``` ] -- .cols3[ ````markdown ```{r, include=FALSE} cat('hello world!') ``` ```` Runs, but doesn't print anything ] --- # message / warning ![](https://www.tidyverse.org/images/tidyverse_1.2.0/tidyverse_1-2-0_pkg_load.gif) --- # message / warning Drop messages and warnings in chunk settings .leftcol[ ````markdown ```{r, message=FALSE, warning=FALSE} library(tidyverse) ``` ```` ] --- # A global `setup` chunk ๐ One chunk to rule them all! .leftcol[ ````markdown ```{r setup, include = FALSE} knitr::opts_chunk$set( warning = FALSE, message = FALSE, comment = "#>", fig.retina = 3, fig.path = "figs/" ) ``` ```` ] .rightcol[ - A special chunk label: `setup` - Typically the first chunk - All following chunks will use these options (i.e., sets global chunk options) - **Tip**: set `include=FALSE` - You can (and should) use individual chunk options too ] --- class: inverse
15
:
00
# Your turn .font90[ 1) Open the `bears.Rmd` file, and title it _"Bears Analysis"_ 2) Create a "setup" code chunk to read in the `bear_killings.csv` data file<br>(HINT: You might want to look back at the `rmd_demo.Rmd` file!). 3) Use text and code to find answers each of the following questions - show your code and results to justify each answer: - Which months have the highest frequency of bear killings? - Who has been killed more often by bears: hunters or hikers? - How do the the number of bear attacks on men vs women compare? HINT: Use `bears %>% count(variable)` to count how many rows are in the data for each unique value of `variable` ] --- class: inverse # Quiz 1
10
:
00
.leftcol[ ### Link is in the #class channel ] .rightcol[ <center> <img src="https://github.com/emse-p4a-gwu/2022-Spring/raw/main/images/quiz_doge.png" width="400"> </center> ] --- class: inverse, middle # Week 3: .fancy[RMarkdown & Plotting] ### 1. Intro to RMarkdown ### QUIZ 1 ### 2. .orange[Intro to ggplot2] ### 3. Project attributes & levels --- .leftcol[ <img src="images/making_a_ggplot.jpeg" width=600> ] .rightcol[ # "Grammar of Graphics" Concept developed by Leland Wilkinson (1999) **ggplot2** package developed by Hadley Wickham (2005) ] --- # Making plot layers with ggplot2 <br> ### 1. The data ### 2. The aesthetic mapping (what goes on the axes?) ### 3. The geometries (points? bars? etc.) ### 4. The annotations / labels ### 5. The theme --- # Layer 1: The data ```r head(mpg) ``` ``` #> # A tibble: 6 ร 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(l5) f 18 29 p compact #> 2 audi a4 1.8 1999 4 manual(m5) f 21 29 p compact #> 3 audi a4 2 2008 4 manual(m6) f 20 31 p compact #> 4 audi a4 2 2008 4 auto(av) f 21 30 p compact #> 5 audi a4 2.8 1999 6 auto(l5) f 16 26 p compact #> 6 audi a4 2.8 1999 6 manual(m5) f 18 26 p compact ``` --- # Layer 1: The data The `ggplot()` function initializes the plot with whatever data you're using .leftcol[ ```r mpg %>% ggplot() ``` ] .rightcol[.blackborder[ <img src="figs/unnamed-chunk-22-1.png" width="504" /> ]] --- # Layer 2: The aesthetic mapping The `aes()` function determines which variables will be _mapped_ to the geometries<br>(e.g. the axes) .leftcol[ ```r mpg %>% * ggplot(aes(x = displ, y = hwy)) ``` ] .rightcol[.blackborder[ <img src="figs/unnamed-chunk-23-1.png" width="504" /> ]] --- # Layer 3: The geometries Use `+` to add geometries, e.g. `geom_points()` for points .leftcol[ ```r mpg %>% ggplot(aes(x = displ, y = hwy)) + * geom_point() ``` ] .rightcol[.blackborder[ <img src="figs/unnamed-chunk-24-1.png" width="504" /> ]] --- # Layer 4: The annotations / labels Use `labs()` to modify most labels .leftcol[ ```r mpg %>% ggplot(aes(x = displ, y = hwy)) + geom_point() + * labs( * x = "Engine displacement (liters)", * y = "Highway fuel economy (mpg)", * title = "Most larger engine vehicles are less fuel efficient" * ) ``` ] .rightcol[ <img src="figs/unnamed-chunk-25-1.png" width="504" /> ] --- # Layer 5: The theme .leftcol[ ```r mpg %>% ggplot(aes(x = displ, y = hwy)) + geom_point() + labs( x = "Engine displacement (liters)", y = "Highway fuel economy (mpg)", title = "Most larger engine vehicles are less fuel efficient" ) + * theme_bw() ``` ] .rightcol[ <img src="figs/unnamed-chunk-26-1.png" width="504" /> ] --- ### Common themes .leftcol[ `theme_bw()` ```r mpg %>% ggplot(aes(x = displ, y = hwy)) + geom_point() + * theme_bw() ``` <img src="figs/unnamed-chunk-27-1.png" width="432" /> ] .rightcol[ `theme_minimal()` ```r mpg %>% ggplot(aes(x = displ, y = hwy)) + geom_point() + * theme_minimal() ``` <img src="figs/unnamed-chunk-28-1.png" width="432" /> ] --- ### Common themes .leftcol[ `theme_classic()` ```r mpg %>% ggplot(aes(x = displ, y = hwy)) + geom_point() + * theme_classic() ``` <img src="figs/unnamed-chunk-29-1.png" width="432" /> ] .rightcol[ `theme_void()` ```r mpg %>% ggplot(aes(x = displ, y = hwy)) + geom_point() + * theme_void() ``` <img src="figs/unnamed-chunk-30-1.png" width="432" /> ] --- ### Other themes: [hrbrthemes](https://github.com/hrbrmstr/hrbrthemes) ```r remotes::install_github("hrbrmstr/hrbrthemes") ``` .leftcol[ ```r mpg %>% ggplot(aes(x = displ, y = hwy)) + geom_point() + * hrbrthemes::theme_ipsum() ``` <img src="figs/unnamed-chunk-32-1.png" width="432" /> ] .rightcol[ ```r mpg %>% ggplot(aes(x = displ, y = hwy)) + geom_point() + * hrbrthemes::theme_ft_rc() ``` <img src="figs/unnamed-chunk-33-1.png" width="432" /> ] --- ### Other themes: [ggthemes](https://jrnold.github.io/ggthemes/) ```r install.packages('ggthemes', dependencies = TRUE) ``` .leftcol[ ```r mpg %>% ggplot(aes(x = displ, y = hwy)) + geom_point() + * ggthemes::theme_economist() ``` <img src="figs/unnamed-chunk-35-1.png" width="432" /> ] .rightcol[ ```r mpg %>% ggplot(aes(x = displ, y = hwy)) + geom_point() + * ggthemes::theme_economist_white() ``` <img src="figs/unnamed-chunk-36-1.png" width="432" /> ] --- class: center, middle, inverse # More practice # Open `ggplot2.Rmd` --- class: inverse .leftcol[ <img src="figs/unnamed-chunk-37-1.png" width="522.144" /> <img src="figs/unnamed-chunk-38-1.png" width="522.144" /> ] .rightcol[
15
:
00
# Your turn Use the `mpg` data frame and ggplot to create these charts <img src="figs/unnamed-chunk-40-1.png" width="522.144" /> ] --- class: inverse # Extra practice .leftcol[ <img src="figs/ggbar_p1-1.png" width="504" /> ] .rightcol[ <img src="figs/unnamed-chunk-41-1.png" width="432" /> ] --- class: inverse, middle # Week 3: .fancy[RMarkdown & Plotting] ### 1. Intro to RMarkdown ### QUIZ 1 ### 2. Intro to ggplot2 ### 3. .orange[Project attributes & levels] --- class: center # Model Relationships Table ([example](https://docs.google.com/spreadsheets/d/1iwMI9cbJjB6J8wghZY6Y_fCOSt7MDsUTnSuuC58_xjU/edit?usp=sharing)) .border[ <center> <img src="images/solar2.png" width=1100> </center> ] ## Start defining attribute _levels_ --- # Defining attribute levels .leftcol[ ## Continuous - **Price**: 1, 2, 3, 4, 5 ($) - **Power Output**: 60, 80, 120 (Watts) ## Discrete - **Color**: Red, Blue, Yellow - **Material**: Plastic, Aluminum, Glass ] .rightcol[ - Look at competitors - Search web for values that cover the full set of values available today (and maybe some into the future) ]