class: center, middle, inverse, title-slide # Visualization with
ggplot
## Distributions ### SuffolkEcon --- # Painting with data You can make a `ggplot` in four steps: -- _Step 1._ Choose your **data** -- _Step 2._ Make a **blank canvas** with the `ggplot()` and `aes()` functions -- _Step 3._ Choose your **paint** to visualization the data with a `geom_()` function (for "geometry") -- _Step 4._ **Customize** the axes, colors, and so on. --- class: inverse, center, middle # 1. Histogram `geom_histogram()` --- count: false ### 1. Histogram .panel1-hist1-auto[ ```r # 1. Data *gapminder ``` ] .panel2-hist1-auto[ ``` # A tibble: 1,704 x 6 country continent year lifeExp pop gdpPercap <fct> <fct> <int> <dbl> <int> <dbl> 1 Afghanistan Asia 1952 28.8 8425333 779. 2 Afghanistan Asia 1957 30.3 9240934 821. 3 Afghanistan Asia 1962 32.0 10267083 853. 4 Afghanistan Asia 1967 34.0 11537966 836. 5 Afghanistan Asia 1972 36.1 13079460 740. 6 Afghanistan Asia 1977 38.4 14880372 786. 7 Afghanistan Asia 1982 39.9 12881816 978. 8 Afghanistan Asia 1987 40.8 13867957 852. 9 Afghanistan Asia 1992 41.7 16317921 649. 10 Afghanistan Asia 1997 41.8 22227415 635. # … with 1,694 more rows ``` ] --- count: false ### 1. Histogram .panel1-hist1-auto[ ```r # 1. Data gapminder %>% # 2. blank canvas * ggplot(aes(x = lifeExp)) ``` ] .panel2-hist1-auto[ ![](ggplot_distributions_files/figure-html/hist1_auto_02_output-1.png)<!-- --> ] --- count: false ### 1. Histogram .panel1-hist1-auto[ ```r # 1. Data gapminder %>% # 2. blank canvas ggplot(aes(x = lifeExp)) + # 3. paint * geom_histogram() ``` ] .panel2-hist1-auto[ ![](ggplot_distributions_files/figure-html/hist1_auto_03_output-1.png)<!-- --> ] --- count: false ### 1. Histogram .panel1-hist1-auto[ ```r # 1. Data gapminder %>% # 2. blank canvas ggplot(aes(x = lifeExp)) + # 3. paint geom_histogram() + # 4. customize: titles * labs(x = "Average Life Expectancy", * y = "Frequency", * title = "How long will you live?", * subtitle = "1952 to 2007") ``` ] .panel2-hist1-auto[ ![](ggplot_distributions_files/figure-html/hist1_auto_04_output-1.png)<!-- --> ] --- count: false ### 1. Histogram .panel1-hist1-auto[ ```r # 1. Data gapminder %>% # 2. blank canvas ggplot(aes(x = lifeExp)) + # 3. paint geom_histogram() + # 4. customize: titles labs(x = "Average Life Expectancy", y = "Frequency", title = "How long will you live?", subtitle = "1952 to 2007") + # 4. customize: one panel per continent * facet_wrap(~continent) ``` ] .panel2-hist1-auto[ ![](ggplot_distributions_files/figure-html/hist1_auto_05_output-1.png)<!-- --> ] <style> .panel1-hist1-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-hist1-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-hist1-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- class: inverse, center, middle # 2. Kernel Density `geom_density()` --- count: false ### 2. Kernel Density .panel1-hist2-auto[ ```r # 1. Data *gapminder ``` ] .panel2-hist2-auto[ ``` # A tibble: 1,704 x 6 country continent year lifeExp pop gdpPercap <fct> <fct> <int> <dbl> <int> <dbl> 1 Afghanistan Asia 1952 28.8 8425333 779. 2 Afghanistan Asia 1957 30.3 9240934 821. 3 Afghanistan Asia 1962 32.0 10267083 853. 4 Afghanistan Asia 1967 34.0 11537966 836. 5 Afghanistan Asia 1972 36.1 13079460 740. 6 Afghanistan Asia 1977 38.4 14880372 786. 7 Afghanistan Asia 1982 39.9 12881816 978. 8 Afghanistan Asia 1987 40.8 13867957 852. 9 Afghanistan Asia 1992 41.7 16317921 649. 10 Afghanistan Asia 1997 41.8 22227415 635. # … with 1,694 more rows ``` ] --- count: false ### 2. Kernel Density .panel1-hist2-auto[ ```r # 1. Data gapminder %>% # 2. blank canvas * ggplot(aes(x = lifeExp)) ``` ] .panel2-hist2-auto[ ![](ggplot_distributions_files/figure-html/hist2_auto_02_output-1.png)<!-- --> ] --- count: false ### 2. Kernel Density .panel1-hist2-auto[ ```r # 1. Data gapminder %>% # 2. blank canvas ggplot(aes(x = lifeExp)) + # 3. paint * geom_density() ``` ] .panel2-hist2-auto[ ![](ggplot_distributions_files/figure-html/hist2_auto_03_output-1.png)<!-- --> ] --- count: false ### 2. Kernel Density .panel1-hist2-auto[ ```r # 1. Data gapminder %>% # 2. blank canvas ggplot(aes(x = lifeExp)) + # 3. paint geom_density() + # 4. customize: titles * labs(x = "Average Life Expectancy", * y = "Density", * title = "How long will you live?", * subtitle = "1952 to 2007") ``` ] .panel2-hist2-auto[ ![](ggplot_distributions_files/figure-html/hist2_auto_04_output-1.png)<!-- --> ] --- count: false ### 2. Kernel Density .panel1-hist2-auto[ ```r # 1. Data gapminder %>% # 2. blank canvas ggplot(aes(x = lifeExp)) + # 3. paint geom_density() + # 4. customize: titles labs(x = "Average Life Expectancy", y = "Density", title = "How long will you live?", subtitle = "1952 to 2007") + # 4. customize: one panel per continent * facet_wrap(~continent) ``` ] .panel2-hist2-auto[ ![](ggplot_distributions_files/figure-html/hist2_auto_05_output-1.png)<!-- --> ] <style> .panel1-hist2-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-hist2-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-hist2-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- class: inverse, center, middle # 3. Cumulative Distributions `stat_ecdf()` --- count: false ### 3. Cumulative Distribution .panel1-hist3-auto[ ```r # 1. Data *gapminder ``` ] .panel2-hist3-auto[ ``` # A tibble: 1,704 x 6 country continent year lifeExp pop gdpPercap <fct> <fct> <int> <dbl> <int> <dbl> 1 Afghanistan Asia 1952 28.8 8425333 779. 2 Afghanistan Asia 1957 30.3 9240934 821. 3 Afghanistan Asia 1962 32.0 10267083 853. 4 Afghanistan Asia 1967 34.0 11537966 836. 5 Afghanistan Asia 1972 36.1 13079460 740. 6 Afghanistan Asia 1977 38.4 14880372 786. 7 Afghanistan Asia 1982 39.9 12881816 978. 8 Afghanistan Asia 1987 40.8 13867957 852. 9 Afghanistan Asia 1992 41.7 16317921 649. 10 Afghanistan Asia 1997 41.8 22227415 635. # … with 1,694 more rows ``` ] --- count: false ### 3. Cumulative Distribution .panel1-hist3-auto[ ```r # 1. Data gapminder %>% # 2. blank canvas * ggplot(aes(x = lifeExp)) ``` ] .panel2-hist3-auto[ ![](ggplot_distributions_files/figure-html/hist3_auto_02_output-1.png)<!-- --> ] --- count: false ### 3. Cumulative Distribution .panel1-hist3-auto[ ```r # 1. Data gapminder %>% # 2. blank canvas ggplot(aes(x = lifeExp)) + # 3. paint * stat_ecdf() ``` ] .panel2-hist3-auto[ ![](ggplot_distributions_files/figure-html/hist3_auto_03_output-1.png)<!-- --> ] --- count: false ### 3. Cumulative Distribution .panel1-hist3-auto[ ```r # 1. Data gapminder %>% # 2. blank canvas ggplot(aes(x = lifeExp)) + # 3. paint stat_ecdf() + # 4. customize: titles * labs(x = "Average Life Expectancy", * y = "Proportion", * title = "How long will you live?", * subtitle = "1952 to 2007") ``` ] .panel2-hist3-auto[ ![](ggplot_distributions_files/figure-html/hist3_auto_04_output-1.png)<!-- --> ] --- count: false ### 3. Cumulative Distribution .panel1-hist3-auto[ ```r # 1. Data gapminder %>% # 2. blank canvas ggplot(aes(x = lifeExp)) + # 3. paint stat_ecdf() + # 4. customize: titles labs(x = "Average Life Expectancy", y = "Proportion", title = "How long will you live?", subtitle = "1952 to 2007") + # 4. customize: one panel per continent * facet_wrap(~continent) ``` ] .panel2-hist3-auto[ ![](ggplot_distributions_files/figure-html/hist3_auto_05_output-1.png)<!-- --> ] <style> .panel1-hist3-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-hist3-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-hist3-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- class: inverse, center, middle # 4. Boxplot `geom_boxplot()` --- count: false ### 4. Boxplot .panel1-hist4-auto[ ```r # 1. Data *gapminder ``` ] .panel2-hist4-auto[ ``` # A tibble: 1,704 x 6 country continent year lifeExp pop gdpPercap <fct> <fct> <int> <dbl> <int> <dbl> 1 Afghanistan Asia 1952 28.8 8425333 779. 2 Afghanistan Asia 1957 30.3 9240934 821. 3 Afghanistan Asia 1962 32.0 10267083 853. 4 Afghanistan Asia 1967 34.0 11537966 836. 5 Afghanistan Asia 1972 36.1 13079460 740. 6 Afghanistan Asia 1977 38.4 14880372 786. 7 Afghanistan Asia 1982 39.9 12881816 978. 8 Afghanistan Asia 1987 40.8 13867957 852. 9 Afghanistan Asia 1992 41.7 16317921 649. 10 Afghanistan Asia 1997 41.8 22227415 635. # … with 1,694 more rows ``` ] --- count: false ### 4. Boxplot .panel1-hist4-auto[ ```r # 1. Data gapminder %>% # 2. blank canvas * ggplot(aes(x = continent, y = lifeExp)) ``` ] .panel2-hist4-auto[ ![](ggplot_distributions_files/figure-html/hist4_auto_02_output-1.png)<!-- --> ] --- count: false ### 4. Boxplot .panel1-hist4-auto[ ```r # 1. Data gapminder %>% # 2. blank canvas ggplot(aes(x = continent, y = lifeExp)) + # 3. paint * geom_boxplot() ``` ] .panel2-hist4-auto[ ![](ggplot_distributions_files/figure-html/hist4_auto_03_output-1.png)<!-- --> ] --- count: false ### 4. Boxplot .panel1-hist4-auto[ ```r # 1. Data gapminder %>% # 2. blank canvas ggplot(aes(x = continent, y = lifeExp)) + # 3. paint geom_boxplot() + # 4. customize: titles * labs(x = "Continent", * y = "Life Expectancy", * title = "How long will you live?", * subtitle = "1952 to 2007") ``` ] .panel2-hist4-auto[ ![](ggplot_distributions_files/figure-html/hist4_auto_04_output-1.png)<!-- --> ] <style> .panel1-hist4-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-hist4-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-hist4-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- class: inverse, center, middle # 5. Themes `theme_` --- count: false ### 5. Themes .panel1-theme-rotate[ ```r # 1. Data gapminder %>% # 2. blank canvas ggplot(aes(x = continent, y = lifeExp)) + # 3. paint geom_boxplot() + # 4. customize: titles labs(x = "Continent", y = "Life Expectancy", title = "How long will you live?", subtitle = "1952 to 2007") + # customize: theme * theme_gray() ``` ] .panel2-theme-rotate[ ![](ggplot_distributions_files/figure-html/theme_rotate_01_output-1.png)<!-- --> ] --- count: false ### 5. Themes .panel1-theme-rotate[ ```r # 1. Data gapminder %>% # 2. blank canvas ggplot(aes(x = continent, y = lifeExp)) + # 3. paint geom_boxplot() + # 4. customize: titles labs(x = "Continent", y = "Life Expectancy", title = "How long will you live?", subtitle = "1952 to 2007") + # customize: theme * theme_minimal() ``` ] .panel2-theme-rotate[ ![](ggplot_distributions_files/figure-html/theme_rotate_02_output-1.png)<!-- --> ] --- count: false ### 5. Themes .panel1-theme-rotate[ ```r # 1. Data gapminder %>% # 2. blank canvas ggplot(aes(x = continent, y = lifeExp)) + # 3. paint geom_boxplot() + # 4. customize: titles labs(x = "Continent", y = "Life Expectancy", title = "How long will you live?", subtitle = "1952 to 2007") + # customize: theme * theme_bw() ``` ] .panel2-theme-rotate[ ![](ggplot_distributions_files/figure-html/theme_rotate_03_output-1.png)<!-- --> ] --- count: false ### 5. Themes .panel1-theme-rotate[ ```r # 1. Data gapminder %>% # 2. blank canvas ggplot(aes(x = continent, y = lifeExp)) + # 3. paint geom_boxplot() + # 4. customize: titles labs(x = "Continent", y = "Life Expectancy", title = "How long will you live?", subtitle = "1952 to 2007") + # customize: theme * theme_dark() ``` ] .panel2-theme-rotate[ ![](ggplot_distributions_files/figure-html/theme_rotate_04_output-1.png)<!-- --> ] --- count: false ### 5. Themes .panel1-theme-rotate[ ```r # 1. Data gapminder %>% # 2. blank canvas ggplot(aes(x = continent, y = lifeExp)) + # 3. paint geom_boxplot() + # 4. customize: titles labs(x = "Continent", y = "Life Expectancy", title = "How long will you live?", subtitle = "1952 to 2007") + # customize: theme * theme_light() ``` ] .panel2-theme-rotate[ ![](ggplot_distributions_files/figure-html/theme_rotate_05_output-1.png)<!-- --> ] --- count: false ### 5. Themes .panel1-theme-rotate[ ```r # 1. Data gapminder %>% # 2. blank canvas ggplot(aes(x = continent, y = lifeExp)) + # 3. paint geom_boxplot() + # 4. customize: titles labs(x = "Continent", y = "Life Expectancy", title = "How long will you live?", subtitle = "1952 to 2007") + # customize: theme * theme_classic() ``` ] .panel2-theme-rotate[ ![](ggplot_distributions_files/figure-html/theme_rotate_06_output-1.png)<!-- --> ] <style> .panel1-theme-rotate { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-theme-rotate { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-theme-rotate { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- class: inverse, center, middle # 6. Color --- count: false ### 6. Color .panel1-color-rotate[ ```r # 1. Data gapminder %>% # 2. blank canvas ggplot(aes(x = lifeExp)) + # 3. paint * geom_histogram() + # 4. customize: titles labs(x = "Continent", y = "Life Expectancy", title = "How long will you live?", subtitle = "1952 to 2007") ``` ] .panel2-color-rotate[ ![](ggplot_distributions_files/figure-html/color_rotate_01_output-1.png)<!-- --> ] --- count: false ### 6. Color .panel1-color-rotate[ ```r # 1. Data gapminder %>% # 2. blank canvas ggplot(aes(x = lifeExp)) + # 3. paint * geom_histogram(fill = "blue") + # 4. customize: titles labs(x = "Continent", y = "Life Expectancy", title = "How long will you live?", subtitle = "1952 to 2007") ``` ] .panel2-color-rotate[ ![](ggplot_distributions_files/figure-html/color_rotate_02_output-1.png)<!-- --> ] --- count: false ### 6. Color .panel1-color-rotate[ ```r # 1. Data gapminder %>% # 2. blank canvas ggplot(aes(x = lifeExp)) + # 3. paint * geom_histogram(aes(fill = continent)) + # 4. customize: titles labs(x = "Continent", y = "Life Expectancy", title = "How long will you live?", subtitle = "1952 to 2007") ``` ] .panel2-color-rotate[ ![](ggplot_distributions_files/figure-html/color_rotate_03_output-1.png)<!-- --> ] --- count: false ### 6. Color .panel1-color-rotate[ ```r # 1. Data gapminder %>% # 2. blank canvas ggplot(aes(x = lifeExp)) + # 3. paint * geom_histogram(aes(fill = continent), alpha = 0.75) + # 4. customize: titles labs(x = "Continent", y = "Life Expectancy", title = "How long will you live?", subtitle = "1952 to 2007") ``` ] .panel2-color-rotate[ ![](ggplot_distributions_files/figure-html/color_rotate_04_output-1.png)<!-- --> ] --- count: false ### 6. Color .panel1-color-rotate[ ```r # 1. Data gapminder %>% # 2. blank canvas ggplot(aes(x = lifeExp)) + # 3. paint * geom_density() + # 4. customize: titles labs(x = "Continent", y = "Life Expectancy", title = "How long will you live?", subtitle = "1952 to 2007") ``` ] .panel2-color-rotate[ ![](ggplot_distributions_files/figure-html/color_rotate_05_output-1.png)<!-- --> ] --- count: false ### 6. Color .panel1-color-rotate[ ```r # 1. Data gapminder %>% # 2. blank canvas ggplot(aes(x = lifeExp)) + # 3. paint * geom_density(fill = "purple") + # 4. customize: titles labs(x = "Continent", y = "Life Expectancy", title = "How long will you live?", subtitle = "1952 to 2007") ``` ] .panel2-color-rotate[ ![](ggplot_distributions_files/figure-html/color_rotate_06_output-1.png)<!-- --> ] --- count: false ### 6. Color .panel1-color-rotate[ ```r # 1. Data gapminder %>% # 2. blank canvas ggplot(aes(x = lifeExp)) + # 3. paint * geom_density(aes(fill = continent)) + # 4. customize: titles labs(x = "Continent", y = "Life Expectancy", title = "How long will you live?", subtitle = "1952 to 2007") ``` ] .panel2-color-rotate[ ![](ggplot_distributions_files/figure-html/color_rotate_07_output-1.png)<!-- --> ] --- count: false ### 6. Color .panel1-color-rotate[ ```r # 1. Data gapminder %>% # 2. blank canvas ggplot(aes(x = lifeExp)) + # 3. paint * geom_density(aes(fill = continent), alpha = 0.75) + # 4. customize: titles labs(x = "Continent", y = "Life Expectancy", title = "How long will you live?", subtitle = "1952 to 2007") ``` ] .panel2-color-rotate[ ![](ggplot_distributions_files/figure-html/color_rotate_08_output-1.png)<!-- --> ] --- count: false ### 6. Color .panel1-color-rotate[ ```r # 1. Data gapminder %>% # 2. blank canvas ggplot(aes(x = lifeExp)) + # 3. paint * stat_ecdf() + # 4. customize: titles labs(x = "Continent", y = "Life Expectancy", title = "How long will you live?", subtitle = "1952 to 2007") ``` ] .panel2-color-rotate[ ![](ggplot_distributions_files/figure-html/color_rotate_09_output-1.png)<!-- --> ] --- count: false ### 6. Color .panel1-color-rotate[ ```r # 1. Data gapminder %>% # 2. blank canvas ggplot(aes(x = lifeExp)) + # 3. paint * stat_ecdf(color = "red") + # 4. customize: titles labs(x = "Continent", y = "Life Expectancy", title = "How long will you live?", subtitle = "1952 to 2007") ``` ] .panel2-color-rotate[ ![](ggplot_distributions_files/figure-html/color_rotate_10_output-1.png)<!-- --> ] --- count: false ### 6. Color .panel1-color-rotate[ ```r # 1. Data gapminder %>% # 2. blank canvas ggplot(aes(x = lifeExp)) + # 3. paint * stat_ecdf(aes(color = continent)) + # 4. customize: titles labs(x = "Continent", y = "Life Expectancy", title = "How long will you live?", subtitle = "1952 to 2007") ``` ] .panel2-color-rotate[ ![](ggplot_distributions_files/figure-html/color_rotate_11_output-1.png)<!-- --> ] <style> .panel1-color-rotate { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-color-rotate { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-color-rotate { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- # Acknowledgements These slides were made with the `flipbookr` package by [Gina Reynolds](https://github.com/EvaMaeRey/flipbookr). Last update: June 2021 <style type="text/css"> .remark-code{line-height: 1.5; font-size: 80%} @media print { .has-continuation { display: block; } } code.r.hljs.remark-code{ position: relative; overflow-x: hidden; } code.r.hljs.remark-code:hover{ overflow-x:visible; width: 500px; border-style: solid; } </style>