class: center, middle, inverse, title-slide # Visualization with
ggplot
## Relationships (Scatterplots and Lines) ### 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. Points `geom_point()` --- count: false ### 1. Scatterplot .panel1-p1-auto[ ```r # 1. Data *gapminder ``` ] .panel2-p1-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. Scatterplot .panel1-p1-auto[ ```r # 1. Data gapminder %>% # 2. blank canvas # notice we calculate log GDP per cap # this "linearizes" the relationship * ggplot(aes(x = lifeExp, y = log(gdpPercap))) ``` ] .panel2-p1-auto[ ![](ggplot_relationships_files/figure-html/p1_auto_02_output-1.png)<!-- --> ] --- count: false ### 1. Scatterplot .panel1-p1-auto[ ```r # 1. Data gapminder %>% # 2. blank canvas # notice we calculate log GDP per cap # this "linearizes" the relationship ggplot(aes(x = lifeExp, y = log(gdpPercap))) + # 3. paint * geom_point() ``` ] .panel2-p1-auto[ ![](ggplot_relationships_files/figure-html/p1_auto_03_output-1.png)<!-- --> ] --- count: false ### 1. Scatterplot .panel1-p1-auto[ ```r # 1. Data gapminder %>% # 2. blank canvas # notice we calculate log GDP per cap # this "linearizes" the relationship ggplot(aes(x = lifeExp, y = log(gdpPercap))) + # 3. paint geom_point() + # 4. customize: titles * labs(x = "GDP Per Capita", * y = "Life Expectancy", * title = "Live Long and Prosper", * subtitle = "1952 to 2007") ``` ] .panel2-p1-auto[ ![](ggplot_relationships_files/figure-html/p1_auto_04_output-1.png)<!-- --> ] --- count: false ### 1. Scatterplot .panel1-p1-auto[ ```r # 1. Data gapminder %>% # 2. blank canvas # notice we calculate log GDP per cap # this "linearizes" the relationship ggplot(aes(x = lifeExp, y = log(gdpPercap))) + # 3. paint geom_point() + # 4. customize: titles labs(x = "GDP Per Capita", y = "Life Expectancy", title = "Live Long and Prosper", subtitle = "1952 to 2007") + # 4. customize: one panel per continent * facet_wrap(~continent) ``` ] .panel2-p1-auto[ ![](ggplot_relationships_files/figure-html/p1_auto_05_output-1.png)<!-- --> ] <style> .panel1-p1-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-p1-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-p1-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- class: inverse, center, middle # 2. Sizes and Colors and Shapes `size`, `color`, `shape` --- count: false ### 2. Sizes and Colors .panel1-p2-rotate[ ```r # 1. Data gapminder %>% # 2. blank canvas # notice we calculate log GDP per cap # this "linearizes" the relationship ggplot(aes(x = lifeExp, y =log(gdpPercap))) + # 3. paint * geom_point() + # 4. customize: titles labs(x = "GDP Per Capita", y = "Life Expectancy", title = "Live Long and Prosper", subtitle = "1952 to 2007") ``` ] .panel2-p2-rotate[ ![](ggplot_relationships_files/figure-html/p2_rotate_01_output-1.png)<!-- --> ] --- count: false ### 2. Sizes and Colors .panel1-p2-rotate[ ```r # 1. Data gapminder %>% # 2. blank canvas # notice we calculate log GDP per cap # this "linearizes" the relationship ggplot(aes(x = lifeExp, y =log(gdpPercap))) + # 3. paint * geom_point(shape = 10) + # 4. customize: titles labs(x = "GDP Per Capita", y = "Life Expectancy", title = "Live Long and Prosper", subtitle = "1952 to 2007") ``` ] .panel2-p2-rotate[ ![](ggplot_relationships_files/figure-html/p2_rotate_02_output-1.png)<!-- --> ] --- count: false ### 2. Sizes and Colors .panel1-p2-rotate[ ```r # 1. Data gapminder %>% # 2. blank canvas # notice we calculate log GDP per cap # this "linearizes" the relationship ggplot(aes(x = lifeExp, y =log(gdpPercap))) + # 3. paint * geom_point(size = 3, color = "red") + # 4. customize: titles labs(x = "GDP Per Capita", y = "Life Expectancy", title = "Live Long and Prosper", subtitle = "1952 to 2007") ``` ] .panel2-p2-rotate[ ![](ggplot_relationships_files/figure-html/p2_rotate_03_output-1.png)<!-- --> ] --- count: false ### 2. Sizes and Colors .panel1-p2-rotate[ ```r # 1. Data gapminder %>% # 2. blank canvas # notice we calculate log GDP per cap # this "linearizes" the relationship ggplot(aes(x = lifeExp, y =log(gdpPercap))) + # 3. paint * geom_point(size = 3, color = "red", alpha = 0.75) + # 4. customize: titles labs(x = "GDP Per Capita", y = "Life Expectancy", title = "Live Long and Prosper", subtitle = "1952 to 2007") ``` ] .panel2-p2-rotate[ ![](ggplot_relationships_files/figure-html/p2_rotate_04_output-1.png)<!-- --> ] --- count: false ### 2. Sizes and Colors .panel1-p2-rotate[ ```r # 1. Data gapminder %>% # 2. blank canvas # notice we calculate log GDP per cap # this "linearizes" the relationship ggplot(aes(x = lifeExp, y =log(gdpPercap))) + # 3. paint * geom_point(aes(size = pop, shape = continent)) + # 4. customize: titles labs(x = "GDP Per Capita", y = "Life Expectancy", title = "Live Long and Prosper", subtitle = "1952 to 2007") ``` ] .panel2-p2-rotate[ ![](ggplot_relationships_files/figure-html/p2_rotate_05_output-1.png)<!-- --> ] --- count: false ### 2. Sizes and Colors .panel1-p2-rotate[ ```r # 1. Data gapminder %>% # 2. blank canvas # notice we calculate log GDP per cap # this "linearizes" the relationship ggplot(aes(x = lifeExp, y =log(gdpPercap))) + # 3. paint * geom_point(aes(size = pop, color = continent)) + # 4. customize: titles labs(x = "GDP Per Capita", y = "Life Expectancy", title = "Live Long and Prosper", subtitle = "1952 to 2007") ``` ] .panel2-p2-rotate[ ![](ggplot_relationships_files/figure-html/p2_rotate_06_output-1.png)<!-- --> ] --- count: false ### 2. Sizes and Colors .panel1-p2-rotate[ ```r # 1. Data gapminder %>% # 2. blank canvas # notice we calculate log GDP per cap # this "linearizes" the relationship ggplot(aes(x = lifeExp, y =log(gdpPercap))) + # 3. paint * geom_point(aes(size = pop, color = continent, alpha = 0.25)) + # 4. customize: titles labs(x = "GDP Per Capita", y = "Life Expectancy", title = "Live Long and Prosper", subtitle = "1952 to 2007") ``` ] .panel2-p2-rotate[ ![](ggplot_relationships_files/figure-html/p2_rotate_07_output-1.png)<!-- --> ] <style> .panel1-p2-rotate { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-p2-rotate { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-p2-rotate { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- class: inverse, center, middle # 3. Regression Lines `geom_smooth(method = "lm")` --- count: false ### 3. Regression Lines .panel1-p3-auto[ ```r # 1. Data *gapminder ``` ] .panel2-p3-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. Regression Lines .panel1-p3-auto[ ```r # 1. Data gapminder %>% # 2. blank canvas ## notice we calculate log GDP per cap * ggplot(aes(x = lifeExp, y = log(gdpPercap))) ``` ] .panel2-p3-auto[ ![](ggplot_relationships_files/figure-html/p3_auto_02_output-1.png)<!-- --> ] --- count: false ### 3. Regression Lines .panel1-p3-auto[ ```r # 1. Data gapminder %>% # 2. blank canvas ## notice we calculate log GDP per cap ggplot(aes(x = lifeExp, y = log(gdpPercap))) + # 3. paint: scatter plot * geom_point() ``` ] .panel2-p3-auto[ ![](ggplot_relationships_files/figure-html/p3_auto_03_output-1.png)<!-- --> ] --- count: false ### 3. Regression Lines .panel1-p3-auto[ ```r # 1. Data gapminder %>% # 2. blank canvas ## notice we calculate log GDP per cap ggplot(aes(x = lifeExp, y = log(gdpPercap))) + # 3. paint: scatter plot geom_point() + # 3. paint: regression line * geom_smooth(method = "lm") ``` ] .panel2-p3-auto[ ![](ggplot_relationships_files/figure-html/p3_auto_04_output-1.png)<!-- --> ] --- count: false ### 3. Regression Lines .panel1-p3-auto[ ```r # 1. Data gapminder %>% # 2. blank canvas ## notice we calculate log GDP per cap ggplot(aes(x = lifeExp, y = log(gdpPercap))) + # 3. paint: scatter plot geom_point() + # 3. paint: regression line geom_smooth(method = "lm") + # 4. customize: titles * labs(x = "GDP Per Capita", * y = "Life Expectancy", * title = "Live Long and Prosper", * subtitle = "1952 to 2007") ``` ] .panel2-p3-auto[ ![](ggplot_relationships_files/figure-html/p3_auto_05_output-1.png)<!-- --> ] --- count: false ### 3. Regression Lines .panel1-p3-auto[ ```r # 1. Data gapminder %>% # 2. blank canvas ## notice we calculate log GDP per cap ggplot(aes(x = lifeExp, y = log(gdpPercap))) + # 3. paint: scatter plot geom_point() + # 3. paint: regression line geom_smooth(method = "lm") + # 4. customize: titles labs(x = "GDP Per Capita", y = "Life Expectancy", title = "Live Long and Prosper", subtitle = "1952 to 2007") + # 4. customize: one panel per continent * facet_wrap(~continent) ``` ] .panel2-p3-auto[ ![](ggplot_relationships_files/figure-html/p3_auto_06_output-1.png)<!-- --> ] <style> .panel1-p3-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-p3-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-p3-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- class: inverse, center, middle # 4. Lines `geom_line()` --- count: false ### 4. Lines: One Group .panel1-p4-auto[ ```r # 1. Data *gapminder ``` ] .panel2-p4-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. Lines: One Group .panel1-p4-auto[ ```r # 1. Data gapminder %>% # one group: United States * filter(country == "United States") ``` ] .panel2-p4-auto[ ``` # A tibble: 12 x 6 country continent year lifeExp pop gdpPercap <fct> <fct> <int> <dbl> <int> <dbl> 1 United States Americas 1952 68.4 157553000 13990. 2 United States Americas 1957 69.5 171984000 14847. 3 United States Americas 1962 70.2 186538000 16173. 4 United States Americas 1967 70.8 198712000 19530. 5 United States Americas 1972 71.3 209896000 21806. 6 United States Americas 1977 73.4 220239000 24073. 7 United States Americas 1982 74.6 232187835 25010. 8 United States Americas 1987 75.0 242803533 29884. 9 United States Americas 1992 76.1 256894189 32004. 10 United States Americas 1997 76.8 272911760 35767. 11 United States Americas 2002 77.3 287675526 39097. 12 United States Americas 2007 78.2 301139947 42952. ``` ] --- count: false ### 4. Lines: One Group .panel1-p4-auto[ ```r # 1. Data gapminder %>% # one group: United States filter(country == "United States") %>% # 2. blank canvas * ggplot(aes(x = year, y = lifeExp)) ``` ] .panel2-p4-auto[ ![](ggplot_relationships_files/figure-html/p4_auto_03_output-1.png)<!-- --> ] --- count: false ### 4. Lines: One Group .panel1-p4-auto[ ```r # 1. Data gapminder %>% # one group: United States filter(country == "United States") %>% # 2. blank canvas ggplot(aes(x = year, y = lifeExp)) + # 3. paint: line * geom_line() ``` ] .panel2-p4-auto[ ![](ggplot_relationships_files/figure-html/p4_auto_04_output-1.png)<!-- --> ] --- count: false ### 4. Lines: One Group .panel1-p4-auto[ ```r # 1. Data gapminder %>% # one group: United States filter(country == "United States") %>% # 2. blank canvas ggplot(aes(x = year, y = lifeExp)) + # 3. paint: line geom_line() + # 4. customize: titles * labs(x = "Year", * y = "Average Life Expectancy", * title = "Live Long and Prosper", * subtitle = "1952 to 2007") ``` ] .panel2-p4-auto[ ![](ggplot_relationships_files/figure-html/p4_auto_05_output-1.png)<!-- --> ] <style> .panel1-p4-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-p4-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-p4-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- count: false ### 4. Lines: Multiple Groups .panel1-p6-rotate[ ```r # 1. Data gapminder %>% # multiple groups: Oceania filter(continent == "Oceania") %>% # 2. blank canvas ggplot(aes(x = year, y = lifeExp)) + # 3. paint: line * geom_line() + # 4. customize: titles labs(x = "Year", y = "Average Life Expectancy", title = "Live Long and Prosper", subtitle = "1952 to 2007") ``` ] .panel2-p6-rotate[ ![](ggplot_relationships_files/figure-html/p6_rotate_01_output-1.png)<!-- --> ] --- count: false ### 4. Lines: Multiple Groups .panel1-p6-rotate[ ```r # 1. Data gapminder %>% # multiple groups: Oceania filter(continent == "Oceania") %>% # 2. blank canvas ggplot(aes(x = year, y = lifeExp)) + # 3. paint: line * geom_line(aes(linetype = country)) + # 4. customize: titles labs(x = "Year", y = "Average Life Expectancy", title = "Live Long and Prosper", subtitle = "1952 to 2007") ``` ] .panel2-p6-rotate[ ![](ggplot_relationships_files/figure-html/p6_rotate_02_output-1.png)<!-- --> ] --- count: false ### 4. Lines: Multiple Groups .panel1-p6-rotate[ ```r # 1. Data gapminder %>% # multiple groups: Oceania filter(continent == "Oceania") %>% # 2. blank canvas ggplot(aes(x = year, y = lifeExp)) + # 3. paint: line * geom_line(aes(color = country)) + # 4. customize: titles labs(x = "Year", y = "Average Life Expectancy", title = "Live Long and Prosper", subtitle = "1952 to 2007") ``` ] .panel2-p6-rotate[ ![](ggplot_relationships_files/figure-html/p6_rotate_03_output-1.png)<!-- --> ] <style> .panel1-p6-rotate { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-p6-rotate { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-p6-rotate { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- # 5. Themes `theme_` --- count: false ### 5. Themes .panel1-theme-rotate[ ```r # 1. Data gapminder %>% # 2. blank canvas ggplot(aes(x = lifeExp, y = gdpPercap)) + # 3. paint geom_point() + # 4. customize: titles labs(x = "GDP Per Capita", y = "Life Expectancy", title = "Live Long and Prosper", subtitle = "1952 to 2007") + # customize: theme * theme_gray() ``` ] .panel2-theme-rotate[ ![](ggplot_relationships_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 = lifeExp, y = gdpPercap)) + # 3. paint geom_point() + # 4. customize: titles labs(x = "GDP Per Capita", y = "Life Expectancy", title = "Live Long and Prosper", subtitle = "1952 to 2007") + # customize: theme * theme_minimal() ``` ] .panel2-theme-rotate[ ![](ggplot_relationships_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 = lifeExp, y = gdpPercap)) + # 3. paint geom_point() + # 4. customize: titles labs(x = "GDP Per Capita", y = "Life Expectancy", title = "Live Long and Prosper", subtitle = "1952 to 2007") + # customize: theme * theme_bw() ``` ] .panel2-theme-rotate[ ![](ggplot_relationships_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 = lifeExp, y = gdpPercap)) + # 3. paint geom_point() + # 4. customize: titles labs(x = "GDP Per Capita", y = "Life Expectancy", title = "Live Long and Prosper", subtitle = "1952 to 2007") + # customize: theme * theme_dark() ``` ] .panel2-theme-rotate[ ![](ggplot_relationships_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 = lifeExp, y = gdpPercap)) + # 3. paint geom_point() + # 4. customize: titles labs(x = "GDP Per Capita", y = "Life Expectancy", title = "Live Long and Prosper", subtitle = "1952 to 2007") + # customize: theme * theme_light() ``` ] .panel2-theme-rotate[ ![](ggplot_relationships_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 = lifeExp, y = gdpPercap)) + # 3. paint geom_point() + # 4. customize: titles labs(x = "GDP Per Capita", y = "Life Expectancy", title = "Live Long and Prosper", subtitle = "1952 to 2007") + # customize: theme * theme_classic() ``` ] .panel2-theme-rotate[ ![](ggplot_relationships_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> --- # 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>