class: middle, inverse .leftcol30[ <center> <img src="https://madd.seas.gwu.edu/images/logo.png" width=250> </center> ] .rightcol70[ # Week 3: .fancy[Quarto & Plotting] ###
EMSE 6035: Marketing Analytics for Design Decisions ###
John Paul Helveston ###
September 13, 2023 ] --- class: inverse, middle # Week 3: .fancy[Quarto & Plotting] ### 1. Intro to Quarto ### QUIZ 1 ### 2. Intro to ggplot2 ### 3. Project attributes & levels --- class: inverse, middle # Week 3: .fancy[Quarto & Plotting] ### 1. .orange[Intro to Quarto] ### 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) ]] --- class: middle, inverse # .center[Quick demo] <br> # 1. Open `quarto_demo.qmd` # 2. Click "Render" <center> <img src="images/how-qmd-works.png" width=100%> </center> --- # .center[Anatomy of a .qmd file] <br> # .red[Header] # Markdown text # R code --- # Define overall document options in header .leftcol[ Basic html page ``` --- title: Your title author: Author name format: html --- ``` ] .rightcol[ Add table of contents, change theme ``` --- title: Your title author: Author name toc: true format: html: theme: united --- ``` More on themes at https://quarto.org/docs/output-formats/html-themes.html ] --- # Render to multiple outputs .leftcol[ ### PDF uses LaTeX ``` --- title: Your title author: Author name format: pdf --- ``` If you don't have LaTeX on your computer, install tinytex in R: ```r tinytex::install_tinytex() ``` ] .rightcol[ ### Microsoft Word ``` --- title: Your title author: Author name format: docx --- ``` ] --- # .center[Anatomy of a .qmd file] <br> # ~~Header~~ # .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: middle, center # Don't want to use Markdown? # .red[Use Visual Mode!] <center> <img src="images/visual-mode.png" width=700> </center> --- # .center[Anatomy of a .qmd file] <br> # ~~Header (think of this as the "settings")~~ # ~~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} library(palmerpenguins) head(penguins) ``` ```` ] -- .rightcol[ ...will produce this when compiled: ```r library(palmerpenguins) head(penguins) ``` ``` #> # A tibble: 6 × 8 #> species island bill_length_mm bill_depth_mm flipper_length_mm body_mass_g sex year #> <fct> <fct> <dbl> <dbl> <int> <int> <fct> <int> #> 1 Adelie Torgersen 39.1 18.7 181 3750 male 2007 #> 2 Adelie Torgersen 39.5 17.4 186 3800 female 2007 #> 3 Adelie Torgersen 40.3 18 195 3250 female 2007 #> 4 Adelie Torgersen NA NA NA NA <NA> 2007 #> 5 Adelie Torgersen 36.7 19.3 193 3450 female 2007 #> 6 Adelie Torgersen 39.3 20.6 190 3650 male 2007 ``` ] --- # Chunk options Control what chunks output using options All options [here](https://quarto.org/docs/reference/cells/cells-knitr.html) <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 🌍 .leftcol[ ````markdown ```{r} #| label: setup #| include: false knitr::opts_chunk$set( warning = FALSE, message = FALSE, fig.path = "figs/", fig.width = 7.252, fig.height = 4, comment = "#>", fig.retina = 3 ) ``` ```` ] .rightcol[ - Typically the first chunk - All following chunks will use these options (i.e., sets global chunk options) - You can (and should) use individual chunk options too - Often where I load libraries, etc. ] --- class: inverse
15
:
00
# Your turn .font90[ 1) Open the `bears.qmd` 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 `quarto_demo.qmd` 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[ ### Download the template from the #class channel ### Make sure you unzip it! ### When done, submit your `quiz1.qmd` on Blackboard ] .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[Quarto & Plotting] ### 1. Intro to Quarto ### 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-19-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-20-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-21-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-22-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-23-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-24-1.png" width="432" /> ] .rightcol[ `theme_minimal()` ```r mpg %>% ggplot(aes(x = displ, y = hwy)) + geom_point() + * theme_minimal() ``` <img src="figs/unnamed-chunk-25-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-26-1.png" width="432" /> ] .rightcol[ `theme_void()` ```r mpg %>% ggplot(aes(x = displ, y = hwy)) + geom_point() + * theme_void() ``` <img src="figs/unnamed-chunk-27-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-29-1.png" width="432" /> ] .rightcol[ ```r mpg %>% ggplot(aes(x = displ, y = hwy)) + geom_point() + * hrbrthemes::theme_ft_rc() ``` <img src="figs/unnamed-chunk-30-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-32-1.png" width="432" /> ] .rightcol[ ```r mpg %>% ggplot(aes(x = displ, y = hwy)) + geom_point() + * ggthemes::theme_economist_white() ``` <img src="figs/unnamed-chunk-33-1.png" width="432" /> ] --- class: center, middle, inverse # More practice # Open `ggplot2.qmd` --- class: middle, inverse .leftcol[ <img src="figs/unnamed-chunk-34-1.png" width="522.144" /> <img src="figs/unnamed-chunk-35-1.png" width="522.144" /> ] .rightcol[
15
:
00
## Your turn Open `practice.qmd` Use the `mpg` data frame and ggplot to create these charts <img src="figs/unnamed-chunk-37-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-38-1.png" width="432" /> ] --- class: inverse, middle # Week 3: .fancy[Quarto & Plotting] ### 1. Intro to Quarto ### QUIZ 1 ### 2. Intro to ggplot2 ### 3. .orange[Project attributes & levels] --- class: center # Model Relationships Table ([example](https://docs.google.com/spreadsheets/d/1Hmxfav_l1bubnaPkIiiMW0tZrFA-xblP9_ndN_6TB1I/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) ]