class: middle, center, inverse # .bigger[.bigger[.teal[GRAMMAR]]] .smaller[.smaller[of]] .bigger[.bigger[.teal[GRAPHICS]]] --- class: <img src="../images/ggplot_art_allison_horst.png" width="106%" style="margin-left: -52px; margin-top: -120px; max-width: 107%;" alt="A fuzzy monster in a beret and scarf, critiquing their own column graph on a canvas in front of them while other assistant monsters (also in berets) carry over boxes full of elements that can be used to customize a graph (like themes and geometric shapes). In the background is a wall with framed data visualizations. Stylized text reads “ggplot2: build a data masterpiece.”"> .smaller[.small[.bottom-right[.gray[Artwork by @allison_horst]]]] <style> .wider {width: 107%;} img {max-width: 120%; width: 100%;} .clear {clear:both;} h2, h3 {color: black;} .pull-right img {width: 139%; max-width: 139%; margin-left: -38px; margin-top: -20px;} .pull-left .remark-code-line {font-size: 1.49rem;} .pull-up {margin-top: -28px;} .remark-code-line {font-size: 1.92rem;} .small-font {font-size: 1.3rem;} .small-font .remark-code-line {font-size: 1.3rem;} .med-font .remark-code-line {font-size: 1.7rem;} .shift-up {z-index: 99; position: absolute; top: 168px; left: 52px; width: 602px;} .shift-left {margin-left: -80px;} .shift-right {margin-left: 180px;} .shift-down {padding-top: 12px;} .geom1 {padding-top: 1px;} .geom2 {margin-top: -16px;} .remark-slide table {width: 108%; font-size: 1.4rem;} .small-table table {width: 39%;} .missing {font-weight: 600;} </style> --- class: inverse, middle, center exclude: true # .bigger.big[[.blue[gg]]] # .gray[Grammar of graphics] --- class: middle, center exclude: true # .bigger[.darkpurple[ **Grammar of graphics** ]] --- exclude: true <br> .pinkquote[ > The .pink[**gg**] in `ggplot` stands for Grammar of graphics ] ??? And this grammar is a powerful tool that gives you a set of building blocks you can piece together in any way you choose to build the specific chart of your dreams. The first building block is the plot canvas, and it is the foundation for all the other plot pieces. --- .shift-left[ # .teal[ggplot canvas] ```r library(tidyverse) ggplot() ``` <img src="03-1-Plots-graphics-grammar_files/figure-html/unnamed-chunk-1-1.png" height="330px" /> ] --- class: .shift-left[ # Lost penguins ] <br> <div style="overflow: hidden; width: 93%;"> <img src="../images/penguins_torn.png" style=""> </div> --- class: inverse <div style="overflow: hidden; width: 100%; position: absolute; top: 0; left:0; float: left;"> <img src="../images/penguins_stranded_up_ice3.png" style="width: 101.3%; margin-left: -8px; margin-top: -45px;"> </div> .shift-left[ # Lost penguins ] .small-font[.shift-up[.whitecode[ ```r lost_penguins <- read_csv("torn_penguins.csv") ``` ]]] .bottom-right[.off-white[Penguins illustrated by @allison_horst]] --- class: inverse exclude: true .shift-left[ # Lost # penguins ] <div style="overflow: hidden; margin-left: 350px; margin-top: -305px; width: 100%;"> <img src="../images/penguins_stranded_up_ice3.png" style="width: 91%; margin-left: -108px;"> </div> .small-font[.shift-left[.shift-up[.whitecode[ ```r lost_penguins <- read_csv("torn_penguins.csv") ``` ]]]] --- exclude: true .shift-left[ # Lost penguins <br> <style> @import url('https://fonts.googleapis.com/css2?family=Kalam:wght@300&family=Source+Sans+3:wght@300;500;600;700&display=swap'); @import url('https://fonts.googleapis.com/css2?family=Gluten:wght@100&family=Kalam:wght@300&family=Source+Sans+3:wght@300;500;600;700&display=swap'); .hand {font-family: 'Kalam', cursive; font-weight: 300;} .hand table {font-family: 'Kalam', cursive;} .hand {font-family: 'Gluten', cursive; font-weight: 100;} .hand table {font-family: 'Gluten', cursive;} </style> .hand[ | species | island | bill_length_mm | bill_depth_mm | flipper_length_mm | body_mass_g | sex | year | |:-----------------------------------------:|:-----------------------------------------:|:--------------:|:-------------:|:-----------------:|:-----------:|:------:|:----:| | <span class="missing blue">missing</span> | <span class="missing blue">missing</span> | 42.6 | 13.7 | 213 | 4950 | female | 2008 | | <span class="missing blue">missing</span> | <span class="missing blue">missing</span> | 50.4 | 15.7 | 222 | 5750 | male | 2009 | | <span class="missing blue">missing</span> | <span class="missing blue">missing</span> | 46.7 | 15.3 | 219 | 5200 | male | 2007 | ] ] --- .shift-left[ # .teal[ggplot canvas] .smaller[(with penguins)] ```r library(tidyverse) ggplot(lost_penguins) ``` <img src="03-1-Plots-graphics-grammar_files/figure-html/unnamed-chunk-6-1.png" height="330px" /> ] --- class: inverse, middle, center # .bigger[.big[.blue[aes( )]]] # aesthetics --- .shift-left[ # .blue[aes( )] Assign the .blue[**X**] and .blue[**Y**] axes inside the aesthetics. ```r ggplot(lost_penguins, * aes(x = bill_length_mm, * y = flipper_length_mm)) ``` ] --- .shift-left[ # .blue[aes( )] ] .pull-left[ .shift-left[ ```r ggplot(lost_penguins, * aes(x = bill_length_mm, * y = flipper_length_mm)) ``` ]] .pull-right[ <img src="03-1-Plots-graphics-grammar_files/figure-html/unnamed-chunk-9-1.png" height="500px" /> ] --- class: inverse, middle, center # .bigger[.big[.blue[geoms]]] --- exclude: true .shift-left[ # Geoms <br> > Geoms add visualization of your data to the your canvas. They are the really juicy fillings in your ggplot sandwhich. You can add one or multiple geoms. ] --- .shift-left[ # .blue[geom_point( )] ```r ggplot(lost_penguins, aes(x = bill_length_mm, y = flipper_length_mm)) + * geom_point() ``` ] --- .shift-left[ # .blue[geom_point( )] ] .pull-left[ .shift-left[ ```r ggplot(lost_penguins, aes(x = bill_length_mm, y = flipper_length_mm)) + * geom_point() ``` ]] .pull-right[ <img src="03-1-Plots-graphics-grammar_files/figure-html/unnamed-chunk-12-1.png" height="500px" /> ] --- .shift-left[ # .blue[geom_point(] size = HUGE .blue[)] ] .pull-left[ .shift-left[ ```r ggplot(lost_penguins, aes(x = bill_length_mm, y = flipper_length_mm)) + * geom_point(size = 10) ``` ]] .pull-right[ <img src="03-1-Plots-graphics-grammar_files/figure-html/unnamed-chunk-14-1.png" height="500px" /> ] --- .shift-left[ # All together now ![](03-1-Plots-graphics-grammar_files/figure-html/unnamed-chunk-15-1.png)<!-- --> ] --- .shift-left[ # All together now ```r good_penguins <- read_csv("untorn_penguins.csv") all_penguins <- bind_rows(good_penguins, lost_penguins) ``` ] --- .shift-left[ # All together now ```r *ggplot(all_penguins, aes(x = bill_length_mm, y = flipper_length_mm)) + geom_point(size = 10) ``` ] --- .shift-left[ # All together now ] .pull-left[ .shift-left[ ```r *ggplot(all_penguins, aes(x = bill_length_mm, y = flipper_length_mm)) + geom_point(size = 10) ``` ]] .pull-right[ <img src="03-1-Plots-graphics-grammar_files/figure-html/unnamed-chunk-20-1.png" height="500px" /> ] --- .shift-left[ # All together now ] .pull-left[ .shift-left[ ```r ggplot(all_penguins, aes(x = bill_length_mm, y = flipper_length_mm)) + geom_point(size = 10, * alpha = 0.25) ``` ]] .pull-right[ <img src="03-1-Plots-graphics-grammar_files/figure-html/unnamed-chunk-22-1.png" height="500px" /> ] --- .shift-left[ # Colors ```r ggplot(all_penguins, aes(x = bill_length_mm, y = flipper_length_mm)) + * geom_point(aes(color = species), size = 10, alpha = 0.25) ``` ] --- .shift-left[ # Colors ] .pull-left[ .shift-left[ ```r ggplot(all_penguins, aes(x = bill_length_mm, y = flipper_length_mm)) + * geom_point(aes(color = species), size = 10, alpha = 0.25) ``` ]] .pull-right[ <img src="03-1-Plots-graphics-grammar_files/figure-html/unnamed-chunk-25-1.png" height="500px" /> ] --- .shift-left[ # Colors ```r ggplot(all_penguins, * aes(x = species, y = flipper_length_mm)) + geom_point(aes(color = species), size = 10, alpha = 0.25) ``` ] --- .shift-left[ # Colors ] .pull-left[ .shift-left[ ```r ggplot(all_penguins, * aes(x = species, y = flipper_length_mm)) + geom_point(aes(color = species), size = 10, alpha = 0.25) ``` ]] .pull-right[.pull-up[ ![](03-1-Plots-graphics-grammar_files/figure-html/unnamed-chunk-28-1.png)<!-- --> ] ] --- .shift-left[ # .blue[geom_`*`] .pull-left[ <img src="../images/geom_point.PNG" style="float:left; width: 90px; margin-right: 14px; padding-top: 6px;"> .geom1[.geom2[ geom_point( ) ]] .clear[ <img src="../images/geom_line.PNG" style="float:left; width: 91px; margin-right: 14px; padding-top: 6px;"> .geom1[.geom2[ geom_line( ) ]]] .clear[ <img src="../images/geom_area.PNG" style="float:left; width: 89px; margin-right: 14px; padding-top: 6px;"> .geom1[.geom2[ geom_area( ) ]]] .clear[ <img src="../images/geom_col.PNG" style="float:left; width: 90px; margin-right: 14px; padding-top: 6px;"> .geom1[.geom2[ geom_col( ) ]]] <div style="clear: both;"></div> ] .pull-right[ <div style="min-height: 18px"></div> <img src="../images/geom_boxplot.PNG" style="float:left; width: 92px; margin-right: 14px; padding-top: 6px;"> .geom1[.geom2[ geom_boxplot( ) ]] .clear[ <img src="../images/geom_histogram.PNG" style="float:left; width: 89px; margin-right: 14px; padding-top: 6px;"> .geom1[.geom2[ geom_histogram( ) ]]] .clear[ <img src="../images/geom_text.PNG" style="float:left; width: 92px; margin-right: 14px; padding-top: 6px;"> .geom1[.geom2[ geom_text( ) ]]] ] ] --- .shift-left[ # .blue[geom_text( )] .wider[ ```r ggplot(all_penguins, aes(x = bill_length_mm, y = flipper_length_mm)) + geom_point(aes(color = species), size = 10, alpha = 0.25) + * geom_text(aes(label = species)) ``` ] ] --- .shift-left[ # .blue[geom_text( )] ![](03-1-Plots-graphics-grammar_files/figure-html/unnamed-chunk-31-1.png)<!-- --> ] --- class: inverse, middle, center # .bigger[.big[.blue[lims]]] --- .shift-left[ # .blue[lims( )] .wider[ ```r ggplot(all_penguins, aes(x = bill_length_mm, y = flipper_length_mm)) + geom_point(aes(color = species), size = 10, alpha = 0.25) + geom_text(aes(label = species)) + * lims(x = c(40, 55), * y = c(200, 225)) ``` ] ] --- .shift-left[ # .blue[lims( )] ![](03-1-Plots-graphics-grammar_files/figure-html/unnamed-chunk-33-1.png)<!-- --> ] --- class: middle, center, inverse # .bigger[.bigger[❤️]] # .bigger[.teal[Code] .teal[magic]] --- .shift-left[ # Compare ~~species~~ .teal[islands] ] -- .shift-left[.wider[ ```r ggplot(all_penguins, aes(x = bill_length_mm, y = flipper_length_mm)) + geom_point(aes(`color = island`), size = 10, alpha = 0.25) + geom_text(aes(`label = island`)) + lims(x = c(40, 55), y = c(200, 225)) ``` ] ] --- .shift-left[ # Compare .teal[islands] ![](03-1-Plots-graphics-grammar_files/figure-html/unnamed-chunk-35-1.png)<!-- --> ] --- class: inverse, center, middle # <i class="fas fa-carrot" aria-hidden="true"></i> [Back to Videos](https://tidy-mn.github.io/R-camp-penguins/index.html)