class: center, middle, inverse, title-slide # Lec 10 - Visualization with ggplot2 ##
Statistical Programming ### Sta 323 | Spring 2022 ###
Dr. Colin Rundel --- exclude: true --- class: middle, center <img src="imgs/hex-ggplot2.png" width="45%" style="display: block; margin: auto;" /> --- ## The Grammar of Graphics - Visualization concept created by Leland Wilkinson (The Grammar of Graphics, 1999) - attempt to taxonimize the basic elements of statistical graphics - Adapted for R by Hadley Wickham (2009) - consistent and compact syntax to describe statistical graphics - highly modular as it breaks up graphs into semantic components - ggplot2 is not meant as a guide to which graph to use and how to best convey your data (more on that later), but it does have some strong opinions. --- ## Terminology A statistical graphic is a... - mapping of **data** - which may be **statistically transformed** (summarized, log-transformed, etc.) - to **aesthetic attributes** (color, size, xy-position, etc.) - using **geometric objects** (points, lines, bars, etc.) - and mapped onto a specific **facet** and **coordinate system** --- ## Anatomy of a ggplot call ```r ggplot( data = [dataframe], mapping = aes( x = [var x], y = [var y], color = [var color], shape = [var shape], ... ) ) + geom_[some geom]( mapping = aes( color = [var geom color], ... ) ) + ... # other geometries scale_[some axis]_[some scale]() + facet_[some facet]([formula]) + ... # other options ``` --- ## Data - Palmer Penguins Measurements for penguin species, island in Palmer Archipelago, size (flipper length, body mass, bill dimensions), and sex. .pull-left-narrow[ <img src="imgs/penguins.png" width="100%" style="display: block; margin: auto;" /> ] .pull-right-wide[ ```r library(palmerpenguins) penguins ``` ``` ## # A tibble: 344 × 8 ## species island bill_length_mm bill_depth_mm flipper_length_mm ## <fct> <fct> <dbl> <dbl> <int> ## 1 Adelie Torgersen 39.1 18.7 181 ## 2 Adelie Torgersen 39.5 17.4 186 ## 3 Adelie Torgersen 40.3 18 195 ## 4 Adelie Torgersen NA NA NA ## 5 Adelie Torgersen 36.7 19.3 193 ## 6 Adelie Torgersen 39.3 20.6 190 ## 7 Adelie Torgersen 38.9 17.8 181 ## 8 Adelie Torgersen 39.2 19.6 195 ## 9 Adelie Torgersen 34.1 18.1 193 ## 10 Adelie Torgersen 42 20.2 190 ## # … with 334 more rows, and 3 more variables: body_mass_g <int>, ## # sex <fct>, year <int> ``` ] --- ## A basic ggplot .pull-left[ ```r ggplot( data = penguins, mapping = aes( x = bill_depth_mm, y = bill_length_mm ) ) + geom_point() + labs( title = "Bill depth and length", subtitle = paste( "Dimensions for Adelie, Chinstrap,", "and Gentoo Penguins" ), x = "Bill depth (mm)", y = "Bill length (mm)", color = "Species" ) ``` ``` ## Warning: Removed 2 rows containing missing values (geom_point). ``` ] .pull-right[ <img src="Lec10_files/figure-html/unnamed-chunk-6-1.png" width="95%" style="display: block; margin: auto;" /> ] --- class: middle # Text <-> Plot --- .midi[ > **Start with the `penguins` data frame** ] .pull-left[ ```r *ggplot(data = penguins) ``` ] .pull-right[ <img src="Lec10_files/figure-html/unnamed-chunk-7-1.png" width="95%" style="display: block; margin: auto;" /> ] --- .midi[ > Start with the `penguins` data frame, > **map bill depth to the x-axis** ] .pull-left[ ```r ggplot( data = penguins, * mapping = aes(x = bill_depth_mm) ) ``` ] .pull-right[ <img src="Lec10_files/figure-html/unnamed-chunk-8-1.png" width="95%" style="display: block; margin: auto;" /> ] --- .midi[ > Start with the `penguins` data frame, > map bill depth to the x-axis > **and map bill length to the y-axis.** ] .pull-left[ ```r ggplot( data = penguins, mapping = aes( x = bill_depth_mm, * y = bill_length_mm ) ) ``` ] .pull-right[ <img src="Lec10_files/figure-html/unnamed-chunk-9-1.png" width="95%" style="display: block; margin: auto;" /> ] --- .midi[ > Start with the `penguins` data frame, > map bill depth to the x-axis > and map bill length to the y-axis. > **Represent each observation with a point** ] .pull-left[ ```r ggplot( data = penguins, mapping = aes( x = bill_depth_mm, y = bill_length_mm ) ) + * geom_point() ``` ] .pull-right[ <img src="Lec10_files/figure-html/unnamed-chunk-10-1.png" width="95%" style="display: block; margin: auto;" /> ] --- .midi[ > Start with the `penguins` data frame, > map bill depth to the x-axis > and map bill length to the y-axis. > Represent each observation with a point > **and map species to the color of each point.** ] .pull-left[ ```r ggplot( data = penguins, mapping = aes( x = bill_depth_mm, y = bill_length_mm ) ) + geom_point( * mapping = aes(color = species) ) ``` ] .pull-right[ <img src="Lec10_files/figure-html/unnamed-chunk-11-1.png" width="95%" style="display: block; margin: auto;" /> ] --- .midi[ > Start with the `penguins` data frame, > map bill depth to the x-axis > and map bill length to the y-axis. > Represent each observation with a point > and map species to the color of each point. > **Title the plot "Bill depth and length"** ] .pull-left[ ```r ggplot( data = penguins, mapping = aes( x = bill_depth_mm, y = bill_length_mm ) ) + geom_point( mapping = aes(color = species) ) + * labs(title = "Bill depth and length") ``` ] .pull-right[ <img src="Lec10_files/figure-html/unnamed-chunk-12-1.png" width="95%" style="display: block; margin: auto;" /> ] --- .midi[ > Start with the `penguins` data frame, > map bill depth to the x-axis > and map bill length to the y-axis. > Represent each observation with a point > and map species to the color of each point. > Title the plot "Bill depth and length", > **add the subtitle "Dimensions for Adelie, Chinstrap, and Gentoo Penguins"** ] .pull-left[ ```r ggplot( data = penguins, mapping = aes( x = bill_depth_mm, y = bill_length_mm ) ) + geom_point( mapping = aes(color = species) ) + labs( title = "Bill depth and length", * subtitle = paste("Dimensions for Adelie,", * "Chinstrap, and Gentoo", * "Penguins") ) ``` ] .pull-right[ <img src="Lec10_files/figure-html/unnamed-chunk-13-1.png" width="95%" style="display: block; margin: auto;" /> ] --- .midi[ > Start with the `penguins` data frame, > map bill depth to the x-axis > and map bill length to the y-axis. > Represent each observation with a point > and map species to the color of each point. > Title the plot "Bill depth and length", > add the subtitle "Dimensions for Adelie, Chinstrap, and Gentoo Penguins", > **label the x and y axes as "Bill depth (mm)" and "Bill length (mm)", respectively** ] .pull-left[ ```r ggplot( data = penguins, mapping = aes( x = bill_depth_mm, y = bill_length_mm ) ) + geom_point( mapping = aes(color = species) ) + labs( title = "Bill depth and length", subtitle = paste("Dimensions for Adelie,", "Chinstrap, and Gentoo", "Penguins"), * x = "Bill depth (mm)", * y = "Bill length (mm)" ) ``` ] .pull-right[ <img src="Lec10_files/figure-html/unnamed-chunk-14-1.png" width="95%" style="display: block; margin: auto;" /> ] --- .midi[ > Start with the `penguins` data frame, > map bill depth to the x-axis > and map bill length to the y-axis. > Represent each observation with a point > and map species to the color of each point. > Title the plot "Bill depth and length", > add the subtitle "Dimensions for Adelie, Chinstrap, and Gentoo Penguins", > label the x and y axes as "Bill depth (mm)" and "Bill length (mm)", respectively, > **label the legend "Species"** ] .pull-left[ ```r ggplot( data = penguins, mapping = aes( x = bill_depth_mm, y = bill_length_mm ) ) + geom_point( mapping = aes(color = species) ) + labs( title = "Bill depth and length", subtitle = paste("Dimensions for Adelie,", "Chinstrap, and Gentoo", "Penguins"), x = "Bill depth (mm)", y = "Bill length (mm)", * color = "Species" ) ``` ] .pull-right[ <img src="Lec10_files/figure-html/unnamed-chunk-15-1.png" width="95%" style="display: block; margin: auto;" /> ] --- .midi[ > Start with the `penguins` data frame, > map bill depth to the x-axis > and map bill length to the y-axis. > Represent each observation with a point > and map species to the color of each point. > Title the plot "Bill depth and length", > add the subtitle "Dimensions for Adelie, Chinstrap, and Gentoo Penguins", > label the x and y axes as "Bill depth (mm)" and "Bill length (mm)", respectively, > label the legend "Species", > **and add a caption for the data source.** ] .pull-left[ ```r ggplot( data = penguins, mapping = aes( x = bill_depth_mm, y = bill_length_mm ) ) + geom_point( mapping = aes(color = species) ) + labs( title = "Bill depth and length", subtitle = paste("Dimensions for Adelie,", "Chinstrap, and Gentoo", "Penguins"), x = "Bill depth (mm)", y = "Bill length (mm)", color = "Species", * caption = "Source: palmerpenguins package" ) ``` ] .pull-right[ <img src="Lec10_files/figure-html/unnamed-chunk-16-1.png" width="95%" style="display: block; margin: auto;" /> ] --- .midi[ > Start with the `penguins` data frame, > map bill depth to the x-axis > and map bill length to the y-axis. > Represent each observation with a point > and map species to the color of each point. > Title the plot "Bill depth and length", > add the subtitle "Dimensions for Adelie, Chinstrap, and Gentoo Penguins", > label the x and y axes as "Bill depth (mm)" and "Bill length (mm)", respectively, > label the legend "Species", > and add a caption for the data source. > **Finally, use the viridis color palete for all points.** ] .pull-left[ ```r ggplot( data = penguins, mapping = aes( x = bill_depth_mm, y = bill_length_mm ) ) + geom_point( mapping = aes(color = species) ) + labs( title = "Bill depth and length", subtitle = paste("Dimensions for Adelie,", "Chinstrap, and Gentoo", "Penguins"), x = "Bill depth (mm)", y = "Bill length (mm)", color = "Species", caption = "Source: palmerpenguins package" ) + * scale_color_viridis_d() ``` ] .pull-right[ <img src="Lec10_files/figure-html/unnamed-chunk-17-1.png" width="95%" style="display: block; margin: auto;" /> ] --- ## Argument names Often we omit the names of first two arguments when building plots with `ggplot()`. .pull-left[ ```r ggplot( data = penguins, mapping = aes( x = bill_depth_mm, y = bill_length_mm ) ) + geom_point( mapping = aes(color = species) ) + scale_color_viridis_d() ``` ] .pull-right[ ```r ggplot( penguins, aes( x = bill_depth_mm, y = bill_length_mm ) ) + geom_point( aes(color = species) ) + scale_color_viridis_d() ``` ] .footnote[**Note** that `ggplot` and `geom_*` swap the order of the `data` and `mapping` arguments.] --- class: middle # Aesthetics --- ## Aesthetics options Commonly used characteristics of plotting geometries that can be **mapped to a specific variable** in the data, examples include: - position (`x`, `y`) - `color` - `shape` - `size` - `alpha` (transparency) Different geometries have different aesthetics that can be used - see the [ggplot2 geoms](https://ggplot2.tidyverse.org/reference/index.html#Geoms) help files for listings. * Aesthetics given in `ggplot()` apply to all `geom`s. * Aesthetics for a specific `geom_*()` can be overridden via the `mapping` argument. --- ## color .pull-left[ ```r ggplot( penguins, aes( x = bill_depth_mm, y = bill_length_mm ) ) + geom_point( * aes(color = species) ) ``` ] .pull-right[ <img src="Lec10_files/figure-html/unnamed-chunk-18-1.png" width="95%" style="display: block; margin: auto;" /> ] --- ## Shape Mapped to a different variable than `color` .pull-left[ ```r ggplot( penguins, aes( x = bill_depth_mm, y = bill_length_mm ) ) + geom_point( aes( * color = species, * shape = island ) ) ``` ] .pull-right[ <img src="Lec10_files/figure-html/unnamed-chunk-19-1.png" width="95%" style="display: block; margin: auto;" /> ] --- ## Shape Mapped to same variable as `color` .pull-left[ ```r ggplot( penguins, aes( x = bill_depth_mm, y = bill_length_mm ) ) + geom_point( aes( * color = species, * shape = species ) ) ``` ] .pull-right[ <img src="Lec10_files/figure-html/unnamed-chunk-20-1.png" width="95%" style="display: block; margin: auto;" /> ] --- ## Size Using a fixed value (note this value is outside of the `aes` call) .pull-left[ ```r ggplot( penguins, aes( x = bill_depth_mm, y = bill_length_mm ) ) + geom_point( aes( color = species, shape = species ), * size = 3 ) ``` ] .pull-right[ <img src="Lec10_files/figure-html/unnamed-chunk-21-1.png" width="95%" style="display: block; margin: auto;" /> ] --- ## Size Mapped to a variable .pull-left[ ```r ggplot( penguins, aes( x = bill_depth_mm, y = bill_length_mm ) ) + geom_point( aes( color = species, shape = species, size = body_mass_g ), ) ``` ] .pull-right[ <img src="Lec10_files/figure-html/unnamed-chunk-22-1.png" width="95%" style="display: block; margin: auto;" /> ] --- ## Alpha .pull-left[ ```r ggplot( penguins, aes( x = bill_depth_mm, y = bill_length_mm ) ) + geom_point( aes( color = species, shape = species, alpha = body_mass_g ), size = 3 ) ``` ] .pull-right[ <img src="Lec10_files/figure-html/unnamed-chunk-23-1.png" width="95%" style="display: block; margin: auto;" /> ] --- ## Mapping vs settings - **Mapping:** Determine an aesthetic (the size, alpha, etc.) of a geom based on the values of a variable in the data - wrapped by `aes()` and pass as `mapping` argument to `ggplot()` or `geom_*()`. - **Setting:** Determine an aesthetic (the size, alpha, etc.) of a geom **not** based on the values of a variable in the data, usually a constant value. - passed directly into `geom_*()` as an argument. -- From the previous slide `color`, `shape`, and `alpha` are all **aesthetics** while `size` is a **setting**. --- class: middle # Faceting --- ## Faceting - Smaller plots that display different subsets of the data - Useful for exploring conditional relationships and large data - Also referred to as "small multiples" --- ## facet_grid .pull-left[ ```r ggplot( penguins, aes( x = bill_depth_mm, y = bill_length_mm ) ) + geom_point() + * facet_grid( species ~ island ) ``` ] .pull-right[ <img src="Lec10_files/figure-html/unnamed-chunk-24-1.png" width="95%" style="display: block; margin: auto;" /> ] --- ## Compare with ... .pull-left[ ```r ggplot( penguins, aes( x = bill_depth_mm, y = bill_length_mm ) ) + geom_point( aes( color = species, shape = island ), size = 3 ) ``` ] .pull-right[ <img src="Lec10_files/figure-html/unnamed-chunk-25-1.png" width="95%" style="display: block; margin: auto;" /> ] --- ## facet_grid (cols) .pull-left[ ```r ggplot( penguins, aes( x = bill_depth_mm, y = bill_length_mm ) ) + geom_point() + * facet_grid( ~ species ) ``` ] .pull-right[ <img src="Lec10_files/figure-html/unnamed-chunk-26-1.png" width="95%" style="display: block; margin: auto;" /> ] --- ## facet_grid (rows) .pull-left[ ```r ggplot( penguins, aes( x = bill_depth_mm, y = bill_length_mm ) ) + geom_point() + * facet_grid( species ~ . ) ``` ] .pull-right[ <img src="Lec10_files/figure-html/unnamed-chunk-27-1.png" width="95%" style="display: block; margin: auto;" /> ] --- ## facet_wrap .pull-left[ ```r ggplot( penguins, aes( x = bill_depth_mm, y = bill_length_mm ) ) + geom_point() + * facet_wrap(~ species) ``` ] .pull-right[ <img src="Lec10_files/figure-html/unnamed-chunk-28-1.png" width="95%" style="display: block; margin: auto;" /> ] --- ## facet_wrap .pull-left[ ```r ggplot( penguins, aes( x = bill_depth_mm, y = bill_length_mm ) ) + geom_point() + * facet_wrap(~ species, ncol = 2) ``` ] .pull-right[ <img src="Lec10_files/figure-html/unnamed-chunk-29-1.png" width="95%" style="display: block; margin: auto;" /> ] --- ## Faceting and color .pull-left[ ```r ggplot( penguins, aes( x = bill_depth_mm, y = bill_length_mm, color = species ) ) + geom_point() + facet_grid(species ~ sex) ``` ] .pull-right[ <img src="Lec10_files/figure-html/unnamed-chunk-30-1.png" width="95%" style="display: block; margin: auto;" /> ] --- ## Hiding redundancy .pull-left[ ```r ggplot( penguins, aes( x = bill_depth_mm, y = bill_length_mm, color = species ) ) + geom_point() + facet_grid(species ~ sex) + * guides(color = "none") ``` ``` ## Warning: Removed 2 rows containing missing values (geom_point). ``` ] .pull-right[ ``` ## Warning: Removed 2 rows containing missing values (geom_point). ``` <img src="Lec10_files/figure-html/unnamed-chunk-31-1.png" width="95%" style="display: block; margin: auto;" /> ] --- class: middle # A brief plot Tour of ggplot2 plots --- ## Histograms .pull-left[ ```r ggplot( penguins, aes( x = body_mass_g, ) ) + geom_histogram() ``` ``` ## `stat_bin()` using `bins = 30`. Pick better value with ## `binwidth`. ``` ] .pull-right[ <img src="Lec10_files/figure-html/unnamed-chunk-32-1.png" width="95%" style="display: block; margin: auto;" /> ] --- ## Histograms - bins .pull-left[ ```r ggplot( penguins, aes( x = body_mass_g, ) ) + geom_histogram(bins = 50) ``` ] .pull-right[ <img src="Lec10_files/figure-html/unnamed-chunk-33-1.png" width="95%" style="display: block; margin: auto;" /> ] --- ## Histograms - binwidth .pull-left[ ```r ggplot( penguins, aes( x = body_mass_g, ) ) + geom_histogram(binwidth = 250) ``` ] .pull-right[ <img src="Lec10_files/figure-html/unnamed-chunk-34-1.png" width="95%" style="display: block; margin: auto;" /> ] --- ## Histograms - color .pull-left[ ```r ggplot( penguins, aes( x = body_mass_g, color = species ) ) + geom_histogram(bins = 20) ``` ] .pull-right[ <img src="Lec10_files/figure-html/unnamed-chunk-35-1.png" width="95%" style="display: block; margin: auto;" /> ] --- ## Histograms - fill .pull-left[ ```r ggplot( penguins, aes( x = body_mass_g, fill = species ) ) + geom_histogram(bins = 20) ``` ] .pull-right[ <img src="Lec10_files/figure-html/unnamed-chunk-36-1.png" width="95%" style="display: block; margin: auto;" /> ] --- ## Histograms - position .pull-left[ ```r ggplot( penguins, aes( x = body_mass_g, fill = species ) ) + geom_histogram( bins = 20, position = "identity", alpha = 0.5 ) ``` ] .pull-right[ <img src="Lec10_files/figure-html/unnamed-chunk-37-1.png" width="95%" style="display: block; margin: auto;" /> ] --- ## Histograms - facets .pull-left[ ```r ggplot( penguins, aes( x = body_mass_g, fill = species ) ) + geom_histogram(bins = 20) + facet_grid(species ~ .) + guides(fill = "none") ``` ] .pull-right[ <img src="Lec10_files/figure-html/unnamed-chunk-38-1.png" width="95%" style="display: block; margin: auto;" /> ] --- ## Density plot .pull-left[ ```r ggplot( penguins, aes( x = body_mass_g ) ) + geom_density() ``` ] .pull-right[ <img src="Lec10_files/figure-html/unnamed-chunk-39-1.png" width="95%" style="display: block; margin: auto;" /> ] --- ## Density plot - fill .pull-left[ ```r ggplot( penguins, aes( x = body_mass_g, fill = species ) ) + geom_density(alpha = 0.25) ``` ] .pull-right[ <img src="Lec10_files/figure-html/unnamed-chunk-40-1.png" width="95%" style="display: block; margin: auto;" /> ] --- ## Density plot - adjust .pull-left[ ```r ggplot( penguins, aes( x = body_mass_g, fill = species ) ) + geom_density( adjust = 0.5, alpha = 0.25 ) ``` ] .pull-right[ <img src="Lec10_files/figure-html/unnamed-chunk-41-1.png" width="95%" style="display: block; margin: auto;" /> ] --- ## Violin plot .pull-left[ ```r ggplot( penguins, aes( x = species, y = body_mass_g, fill = species ) ) + geom_violin() ``` ] .pull-right[ <img src="Lec10_files/figure-html/unnamed-chunk-42-1.png" width="95%" style="display: block; margin: auto;" /> ] --- ## Ridge plot .pull-left[ ```r ggplot( penguins, aes( x = body_mass_g, y = species, fill = species ) ) + ggridges::geom_density_ridges(alpha = 0.5) ``` ``` ## Picking joint bandwidth of 153 ``` ] .pull-right[ <img src="Lec10_files/figure-html/unnamed-chunk-43-1.png" width="95%" style="display: block; margin: auto;" /> ] --- ## Ridge plot - more categories + dplyr .pull-left[ ```r penguins %>% mutate( species_sex = paste0(species, " (", sex, ")") ) %>% ggplot( aes( x = body_mass_g, y = species_sex, fill = species ) ) + ggridges::geom_density_ridges(alpha = 0.5) ``` ``` ## Picking joint bandwidth of 127 ``` ] .pull-right[ <img src="Lec10_files/figure-html/unnamed-chunk-44-1.png" width="95%" style="display: block; margin: auto;" /> ] --- ## Box plot .pull-left[ ```r ggplot( penguins, aes( x = body_mass_g, y = species ) ) + geom_boxplot() ``` ] .pull-right[ <img src="Lec10_files/figure-html/unnamed-chunk-45-1.png" width="95%" style="display: block; margin: auto;" /> ] --- ## Box plot - coord_flip .pull-left[ ```r ggplot( penguins, aes( x = body_mass_g, y = species ) ) + geom_boxplot() + coord_flip() ``` ] .pull-right[ <img src="Lec10_files/figure-html/unnamed-chunk-46-1.png" width="95%" style="display: block; margin: auto;" /> ] --- ## Box plot - swap coords .pull-left[ ```r ggplot( penguins, aes( x = species, y = body_mass_g ) ) + geom_boxplot() ``` ] .pull-right[ <img src="Lec10_files/figure-html/unnamed-chunk-47-1.png" width="95%" style="display: block; margin: auto;" /> ] --- ## Scatter plot .pull-left[ ```r ggplot( penguins, aes( x = bill_depth_mm, y = bill_length_mm, color = species ) ) + geom_point() ``` ] .pull-right[ <img src="Lec10_files/figure-html/unnamed-chunk-48-1.png" width="95%" style="display: block; margin: auto;" /> ] --- ## Scatter plot - geom_smooth .pull-left[ ```r ggplot( penguins, aes( x = bill_depth_mm, y = bill_length_mm, color = species ) ) + geom_point() + geom_smooth( fullrange = TRUE ) ``` ``` ## `geom_smooth()` using method = 'loess' and formula 'y ~ x' ``` ] .pull-right[ <img src="Lec10_files/figure-html/unnamed-chunk-49-1.png" width="95%" style="display: block; margin: auto;" /> ] --- ## Scatter plot - geom_smooth w/ lm .pull-left[ ```r ggplot( penguins, aes( x = bill_depth_mm, y = bill_length_mm, color = species ) ) + geom_point() + geom_smooth( method = "lm", se = FALSE, fullrange = TRUE ) ``` ``` ## `geom_smooth()` using formula 'y ~ x' ``` ] .pull-right[ <img src="Lec10_files/figure-html/unnamed-chunk-50-1.png" width="95%" style="display: block; margin: auto;" /> ] --- ## Line plot .pull-left[ ```r penguins %>% count(species, year) %>% ggplot( aes( x = year, y = n, color = species, group = species ) ) + geom_line() ``` ] .pull-right[ <img src="Lec10_files/figure-html/unnamed-chunk-51-1.png" width="95%" style="display: block; margin: auto;" /> ] --- ## Line plot - with points .pull-left[ ```r penguins %>% count(species, year) %>% ggplot( aes( x = year, y = n, color = species, group = species ) ) + geom_line() + geom_point() ``` ] .pull-right[ <img src="Lec10_files/figure-html/unnamed-chunk-52-1.png" width="95%" style="display: block; margin: auto;" /> ] --- ## Bar plot .pull-left[ ```r ggplot( penguins, aes( x = species ) ) + geom_bar() ``` ] .pull-right[ <img src="Lec10_files/figure-html/unnamed-chunk-53-1.png" width="95%" style="display: block; margin: auto;" /> ] --- ## Stacked bar plot .pull-left[ ```r ggplot( penguins, aes( x = species, fill = island ) ) + geom_bar() ``` ] .pull-right[ <img src="Lec10_files/figure-html/unnamed-chunk-54-1.png" width="95%" style="display: block; margin: auto;" /> ] --- ## Stacked relative frequency bar plot .pull-left[ ```r ggplot( penguins, aes( x = species, fill = island ) ) + geom_bar(position = "fill") ``` ] .pull-right[ <img src="Lec10_files/figure-html/unnamed-chunk-55-1.png" width="95%" style="display: block; margin: auto;" /> ] --- ## Dodged bar plot .pull-left[ ```r ggplot( penguins, aes( x = species, fill = sex ) ) + geom_bar(position = "dodge") ``` ] .pull-right[ <img src="Lec10_files/figure-html/unnamed-chunk-56-1.png" width="95%" style="display: block; margin: auto;" /> ] --- class: middle # Exercises --- ## Exercise 1 Recreate, as faithfully as possible, the following plot using ggplot2 and the `penguins` data. <img src="Lec10_files/figure-html/unnamed-chunk-57-1.png" width="60%" style="display: block; margin: auto;" /> --- ## Exercise 2 Recreate, as faithfully as possible, the following plot from the `palmerpenguin` package readme in ggplot2. <img src="imgs/palmer_plot_ex2.png" width="55%" style="display: block; margin: auto;" /> .footnote[ `palmerpenguins` pkgdown [site](https://allisonhorst.github.io/palmerpenguins/) ]