class: center, middle, inverse, title-slide .title[ # ggplot flipbook ] .subtitle[ ## Use
⇦
and
⇨
to navigate. ] .author[ ### Andy Grogan-Kaylor ] .date[ ### 2024-07-28 ] --- <style type="text/css"> @import url('https://fonts.googleapis.com/css2?family=Montserrat&display=swap'); .title-slide { color: #ffcb05; background-color: #00274C; } .title-slide h1 { color: #ffcb05; } pre { white-space: pre-wrap; } h1, h2, h3 { font-family: 'Montserrat', sans-serif; } body { font-family: 'Montserrat', sans-serif; } .author, .date { font-family: 'Montserrat', sans-serif; } </style> # How To Navigate This Presentation * Use the <span style="font-size:100px">⇦</span> and <span style="font-size:100px">⇨</span> keys to move through the presentation. * Press *o* for *panel overview*. * This presentation plays a *tone* when each new item appears. Turn the volume down if you find these tones annoying. Based upon [2 Page ggplot](https://agrogan1.github.io/R/two-page-ggplot2/two-page-ggplot2.pdf) --- class: animated, slideInRight # The Essential Idea Of ggplot2 Is Simple There are 3 essential elements to any ggplot call: 1. An *aesthetic* that tells ggplot which variables are being mapped to the *x axis*, *y axis*, (and often other attributes of the graph, such as the *color fill*). Intuitively, the aesthetic can be thought of as **what you are graphing**. 2. A *geom* or *geometry* that tells ggplot about the basic structure of the graph. Intuitively, the geom can be thought of as **how you are graphing it**. 3. Other options, such as a *graph title*, *axis labels* and *overall theme* for the graph. --- class: animated, slideInRight # Get Started --- count: false .panel1-graph0-auto[ ```r *library(ggplot2) # beautiful graphs ``` ] .panel2-graph0-auto[ ] --- count: false .panel1-graph0-auto[ ```r library(ggplot2) # beautiful graphs *library(ggthemes) # nice themes ``` ] .panel2-graph0-auto[ ] --- count: false .panel1-graph0-auto[ ```r library(ggplot2) # beautiful graphs library(ggthemes) # nice themes ``` ] .panel2-graph0-auto[ ] <style> .panel1-graph0-auto { color: black; width: 65.3333333333333%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-graph0-auto { color: black; width: 32.6666666666667%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-graph0-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- class: animated, slideInRight # One Continuous Variable --- count: false .panel1-graph1-auto[ ```r *ggplot(mydata, # data * aes(x = outcome)) # variable ``` ] .panel2-graph1-auto[ <img src="ggplot-flipbook_files/figure-html/graph1_auto_01_output-1.png" width="432" /> ] --- count: false .panel1-graph1-auto[ ```r ggplot(mydata, # data aes(x = outcome)) + # variable * geom_histogram() ``` ] .panel2-graph1-auto[ <img src="ggplot-flipbook_files/figure-html/graph1_auto_02_output-1.png" width="432" /> ] --- count: false .panel1-graph1-auto[ ```r ggplot(mydata, # data aes(x = outcome)) + # variable geom_histogram() ``` ] .panel2-graph1-auto[ <img src="ggplot-flipbook_files/figure-html/graph1_auto_03_output-1.png" width="432" /> ] <style> .panel1-graph1-auto { color: black; width: 65.3333333333333%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-graph1-auto { color: black; width: 32.6666666666667%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-graph1-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- class: animated, slideInRight # Add Color --- count: false .panel1-graph1colorful-non_seq[ ```r ggplot(mydata, # data aes(x = outcome)) + # variable geom_histogram( ) ``` ] .panel2-graph1colorful-non_seq[ <img src="ggplot-flipbook_files/figure-html/graph1colorful_non_seq_01_output-1.png" width="432" /> ] --- count: false .panel1-graph1colorful-non_seq[ ```r ggplot(mydata, # data aes(x = outcome)) + # variable geom_histogram( * fill = "red" ) ``` ] .panel2-graph1colorful-non_seq[ <img src="ggplot-flipbook_files/figure-html/graph1colorful_non_seq_02_output-1.png" width="432" /> ] <style> .panel1-graph1colorful-non_seq { color: black; width: 65.3333333333333%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-graph1colorful-non_seq { color: black; width: 32.6666666666667%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-graph1colorful-non_seq { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- class: animated, slideInRight # An Alternative Geometry --- count: false .panel1-graph1A-non_seq[ ```r ggplot(mydata, # data aes(x = outcome)) + # variable geom_density( ) ``` ] .panel2-graph1A-non_seq[ <img src="ggplot-flipbook_files/figure-html/graph1A_non_seq_01_output-1.png" width="432" /> ] --- count: false .panel1-graph1A-non_seq[ ```r ggplot(mydata, # data aes(x = outcome)) + # variable geom_density( * fill = "blue" ) ``` ] .panel2-graph1A-non_seq[ <img src="ggplot-flipbook_files/figure-html/graph1A_non_seq_02_output-1.png" width="432" /> ] <style> .panel1-graph1A-non_seq { color: black; width: 65.3333333333333%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-graph1A-non_seq { color: black; width: 32.6666666666667%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-graph1A-non_seq { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- class: animated, slideInRight # One Categorical Variable --- count: false .panel1-graph2-non_seq[ ```r ggplot(mydata, # data aes( x = group, )) + geom_bar() # geometry ``` ] .panel2-graph2-non_seq[ <img src="ggplot-flipbook_files/figure-html/graph2_non_seq_01_output-1.png" width="432" /> ] --- count: false .panel1-graph2-non_seq[ ```r ggplot(mydata, # data aes( x = group, * fill = group )) + geom_bar() # geometry ``` ] .panel2-graph2-non_seq[ <img src="ggplot-flipbook_files/figure-html/graph2_non_seq_02_output-1.png" width="432" /> ] <style> .panel1-graph2-non_seq { color: black; width: 65.3333333333333%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-graph2-non_seq { color: black; width: 32.6666666666667%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-graph2-non_seq { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- class: animated, slideInRight # Continuous by Continuous --- count: false .panel1-graph3-non_seq[ ```r ggplot(mydata, # data aes( x = predictor, # variables y = outcome, )) + geom_point() # geometry ``` ] .panel2-graph3-non_seq[ <img src="ggplot-flipbook_files/figure-html/graph3_non_seq_01_output-1.png" width="432" /> ] --- count: false .panel1-graph3-non_seq[ ```r ggplot(mydata, # data aes( x = predictor, # variables y = outcome, * color = group )) + geom_point() # geometry ``` ] .panel2-graph3-non_seq[ <img src="ggplot-flipbook_files/figure-html/graph3_non_seq_02_output-1.png" width="432" /> ] <style> .panel1-graph3-non_seq { color: black; width: 65.3333333333333%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-graph3-non_seq { color: black; width: 32.6666666666667%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-graph3-non_seq { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- class: animated, slideInRight # Build A Graph With Better Design Elements --- count: false .panel1-graph4-auto[ ```r *ggplot(mydata, # data * aes(x = predictor, # variables * y = outcome, * color = group)) ``` ] .panel2-graph4-auto[ <img src="ggplot-flipbook_files/figure-html/graph4_auto_01_output-1.png" width="432" /> ] --- count: false .panel1-graph4-auto[ ```r ggplot(mydata, # data aes(x = predictor, # variables y = outcome, color = group)) + * geom_point() # 1st geometry ``` ] .panel2-graph4-auto[ <img src="ggplot-flipbook_files/figure-html/graph4_auto_02_output-1.png" width="432" /> ] --- count: false .panel1-graph4-auto[ ```r ggplot(mydata, # data aes(x = predictor, # variables y = outcome, color = group)) + geom_point() + # 1st geometry * geom_smooth(size = 3) # 2nd geometry ``` ] .panel2-graph4-auto[ <img src="ggplot-flipbook_files/figure-html/graph4_auto_03_output-1.png" width="432" /> ] --- count: false .panel1-graph4-auto[ ```r ggplot(mydata, # data aes(x = predictor, # variables y = outcome, color = group)) + geom_point() + # 1st geometry geom_smooth(size = 3) + # 2nd geometry * labs(title = "My Title", caption = "Colors Inspired by UNICEF", x = "Predictor", y = "Outcome") # labels ``` ] .panel2-graph4-auto[ <img src="ggplot-flipbook_files/figure-html/graph4_auto_04_output-1.png" width="432" /> ] --- count: false .panel1-graph4-auto[ ```r ggplot(mydata, # data aes(x = predictor, # variables y = outcome, color = group)) + geom_point() + # 1st geometry geom_smooth(size = 3) + # 2nd geometry labs(title = "My Title", caption = "Colors Inspired by UNICEF", x = "Predictor", y = "Outcome") + # labels * scale_colour_manual(name = "Group", values = c("#6A1E74", "#1CABE2"), labels = c("group 1", "group 2")) # manual colors ``` ] .panel2-graph4-auto[ <img src="ggplot-flipbook_files/figure-html/graph4_auto_05_output-1.png" width="432" /> ] --- count: false .panel1-graph4-auto[ ```r ggplot(mydata, # data aes(x = predictor, # variables y = outcome, color = group)) + geom_point() + # 1st geometry geom_smooth(size = 3) + # 2nd geometry labs(title = "My Title", caption = "Colors Inspired by UNICEF", x = "Predictor", y = "Outcome") + # labels scale_colour_manual(name = "Group", values = c("#6A1E74", "#1CABE2"), labels = c("group 1", "group 2")) + # manual colors * theme_minimal() # theme ``` ] .panel2-graph4-auto[ <img src="ggplot-flipbook_files/figure-html/graph4_auto_06_output-1.png" width="432" /> ] --- count: false .panel1-graph4-auto[ ```r ggplot(mydata, # data aes(x = predictor, # variables y = outcome, color = group)) + geom_point() + # 1st geometry geom_smooth(size = 3) + # 2nd geometry labs(title = "My Title", caption = "Colors Inspired by UNICEF", x = "Predictor", y = "Outcome") + # labels scale_colour_manual(name = "Group", values = c("#6A1E74", "#1CABE2"), labels = c("group 1", "group 2")) + # manual colors theme_minimal() + # theme * theme(plot.title = element_text(size = rel(5.0), color = "#1CABE2"), plot.caption = element_text(size=rel(2.0))) # add to theme ``` ] .panel2-graph4-auto[ <img src="ggplot-flipbook_files/figure-html/graph4_auto_07_output-1.png" width="432" /> ] --- count: false .panel1-graph4-auto[ ```r ggplot(mydata, # data aes(x = predictor, # variables y = outcome, color = group)) + geom_point() + # 1st geometry geom_smooth(size = 3) + # 2nd geometry labs(title = "My Title", caption = "Colors Inspired by UNICEF", x = "Predictor", y = "Outcome") + # labels scale_colour_manual(name = "Group", values = c("#6A1E74", "#1CABE2"), labels = c("group 1", "group 2")) + # manual colors theme_minimal() + # theme theme(plot.title = element_text(size = rel(5.0), color = "#1CABE2"), plot.caption = element_text(size=rel(2.0))) # add to theme ``` ] .panel2-graph4-auto[ <img src="ggplot-flipbook_files/figure-html/graph4_auto_08_output-1.png" width="432" /> ] <style> .panel1-graph4-auto { color: black; width: 65.3333333333333%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-graph4-auto { color: black; width: 32.6666666666667%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-graph4-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- class: animated, slideInRight # References Reynolds, E. (2020). _flipbookr: Parses code, creates partial code builds, delivers code movie_. R package version 0.1.0. Wickham, H. (2016). _ggplot2: Elegant Graphics for Data Analysis_. Springer-Verlag New York. ISBN: 978-3-319-24277-4. URL: [https://ggplot2.tidyverse.org](https://ggplot2.tidyverse.org).