class: center, middle, inverse, title-slide # Plot a lot with ggplot2 to find plots ## R-Ladies Colombo Meetup ### Priyanga Dilini Talagala ### 2021/01/27 --- background-image:url('fig/tidyworkflow1.png') background-position: 60% 80% background-size: 100% class: top, center # Tidy Workflow --- background-image:url('fig/tidyworkflow2.png') background-position: 60% 80% background-size: 100% class: top, center # Tidy Workflow --- background-image:url('fig/tidyworkflow3.png') background-position: 60% 80% background-size: 100% class: top, center # Tidy Workflow --- class: inverse ## The Datasaurus Dozen ```r library(datasauRus) library(ggplot2) library(gganimate) ggplot(datasaurus_dozen, aes(x=x, y=y))+ geom_point()+ theme_minimal() + transition_states(dataset, 3, 1) + theme(aspect.ratio = 1) ``` <img src="RLadies-ggplot2_files/figure-html/unnamed-chunk-1-1.gif" style="display: block; margin: auto;" /> --- ## The Datasaurus Dozen ```r library(datasauRus) library(ggplot2) library(gganimate) ggplot(datasaurus_dozen, aes(x=x, y=y))+ geom_point()+ theme_minimal() + transition_states(dataset, 3, 1) + theme(aspect.ratio = 1) ``` .pull-left[ <img src="RLadies-ggplot2_files/figure-html/unnamed-chunk-3-1.gif" style="display: block; margin: auto;" /> ] .pull-right[ Summary statistics | -------------------|---- X Mean | 54.26 Y Mean | 47.83 X SD | 16.77 Y SD | 26.94 Corr. | -0.06 ] --- class: inverse, middle, center # The Grammar of Graphics --- class: inverse, middle, center ### The Book # The Grammar of Graphics <img src="fig/6_ggbook.jpg" width="35%" style="display: block; margin: auto;" /> --- #### R Base Graphics .pull-left[ <img src="RLadies-ggplot2_files/figure-html/unnamed-chunk-6-1.png" style="display: block; margin: auto;" /> <img src="RLadies-ggplot2_files/figure-html/unnamed-chunk-7-1.png" style="display: block; margin: auto;" /> ] .pull-right[ <img src="RLadies-ggplot2_files/figure-html/unnamed-chunk-8-1.png" style="display: block; margin: auto;" /> <img src="RLadies-ggplot2_files/figure-html/unnamed-chunk-9-1.png" style="display: block; margin: auto;" /> ] --- class: middle, center ## The Grammar of Graphics .pull-left[ .pull-left[ Pie Chart <img src="RLadies-ggplot2_files/figure-html/unnamed-chunk-10-1.png" style="display: block; margin: auto;" /> Line Chart <img src="RLadies-ggplot2_files/figure-html/unnamed-chunk-11-1.png" style="display: block; margin: auto;" /> ] .pull-right[ Bar Chart <img src="RLadies-ggplot2_files/figure-html/unnamed-chunk-12-1.png" style="display: block; margin: auto;" /> Scatterplot <img src="RLadies-ggplot2_files/figure-html/unnamed-chunk-13-1.png" style="display: block; margin: auto;" /> ] ].pull-right[ <img src="fig/7_GG_a.png" width="1223" height="100%" style="display: block; margin: auto;" /> ] --- ## The Grammar of Graphics .pull-left[ **`penguins` dataset** Size measurements for adult foraging penguins near Palmer Station, Antarctica ``` Rows: 344 Columns: 8 $ species <fct> Adelie, Adelie, Adelie, Adelie, Adelie, Adelie, Ade… $ island <fct> Torgersen, Torgersen, Torgersen, Torgersen, Torgers… $ bill_length_mm <dbl> 39.1, 39.5, 40.3, NA, 36.7, 39.3, 38.9, 39.2, 34.1,… $ bill_depth_mm <dbl> 18.7, 17.4, 18.0, NA, 19.3, 20.6, 17.8, 19.6, 18.1,… $ flipper_length_mm <int> 181, 186, 195, NA, 193, 190, 181, 195, 193, 190, 18… $ body_mass_g <int> 3750, 3800, 3250, NA, 3450, 3650, 3625, 4675, 3475,… $ sex <fct> male, female, female, NA, female, male, female, mal… $ year <int> 2007, 2007, 2007, 2007, 2007, 2007, 2007, 2007, 200… ``` <img src="fig/15_palmerpenguins.png" width="35%" style="display: block; margin: auto;" /> ].pull-right[ <img src="fig/8_GG_1.png" width="100%" style="display: block; margin: auto;" /> ] --- ## The Grammar of Graphics .pull-left[ ``` Rows: 344 Columns: 8 $ species <fct> Adelie, Adelie, Adelie, Adelie, Adelie, Adelie, Ade… $ island <fct> Torgersen, Torgersen, Torgersen, Torgersen, Torgers… $ bill_length_mm <dbl> 39.1, 39.5, 40.3, NA, 36.7, 39.3, 38.9, 39.2, 34.1,… $ bill_depth_mm <dbl> 18.7, 17.4, 18.0, NA, 19.3, 20.6, 17.8, 19.6, 18.1,… $ flipper_length_mm <int> 181, 186, 195, NA, 193, 190, 181, 195, 193, 190, 18… $ body_mass_g <int> 3750, 3800, 3250, NA, 3450, 3650, 3625, 4675, 3475,… $ sex <fct> male, female, female, NA, female, male, female, mal… $ year <int> 2007, 2007, 2007, 2007, 2007, 2007, 2007, 2007, 200… ``` <img src="RLadies-ggplot2_files/figure-html/unnamed-chunk-18-1.png" style="display: block; margin: auto;" /> ].pull-right[ <img src="fig/9_GG_2.png" width="100%" style="display: block; margin: auto;" /> ] --- ## The Grammar of Graphics .pull-left[ ``` Rows: 344 Columns: 8 $ species <fct> Adelie, Adelie, Adelie, Adelie, Adelie, Adelie, Ade… $ island <fct> Torgersen, Torgersen, Torgersen, Torgersen, Torgers… $ bill_length_mm <dbl> 39.1, 39.5, 40.3, NA, 36.7, 39.3, 38.9, 39.2, 34.1,… $ bill_depth_mm <dbl> 18.7, 17.4, 18.0, NA, 19.3, 20.6, 17.8, 19.6, 18.1,… $ flipper_length_mm <int> 181, 186, 195, NA, 193, 190, 181, 195, 193, 190, 18… $ body_mass_g <int> 3750, 3800, 3250, NA, 3450, 3650, 3625, 4675, 3475,… $ sex <fct> male, female, female, NA, female, male, female, mal… $ year <int> 2007, 2007, 2007, 2007, 2007, 2007, 2007, 2007, 200… ``` **Counts** ``` # A tibble: 3 x 2 species n <fct> <int> 1 Adelie 152 2 Chinstrap 68 3 Gentoo 124 ``` ].pull-right[ <img src="fig/10_GG_3.png" width="100%" style="display: block; margin: auto;" /> ] --- ## The Grammar of Graphics .pull-left[ <img src="fig/16_scale.png" width="100%" style="display: block; margin: auto;" /> ].pull-right[ <img src="fig/11_GG_4.png" width="100%" style="display: block; margin: auto;" /> ] --- ## The Grammar of Graphics .pull-left[ <img src="fig/16_scale.png" width="100%" style="display: block; margin: auto;" /> **Bar Plot** <img src="RLadies-ggplot2_files/figure-html/unnamed-chunk-26-1.png" style="display: block; margin: auto;" /> ].pull-right[ <img src="fig/12_GG_5.png" width="100%" style="display: block; margin: auto;" /> ] --- ## The Grammar of Graphics .pull-left[ ``` Rows: 344 Columns: 8 $ species <fct> Adelie, Adelie, Adelie, Adelie, Adelie, Adelie, Ade… $ island <fct> Torgersen, Torgersen, Torgersen, Torgersen, Torgers… $ bill_length_mm <dbl> 39.1, 39.5, 40.3, NA, 36.7, 39.3, 38.9, 39.2, 34.1,… $ bill_depth_mm <dbl> 18.7, 17.4, 18.0, NA, 19.3, 20.6, 17.8, 19.6, 18.1,… $ flipper_length_mm <int> 181, 186, 195, NA, 193, 190, 181, 195, 193, 190, 18… $ body_mass_g <int> 3750, 3800, 3250, NA, 3450, 3650, 3625, 4675, 3475,… $ sex <fct> male, female, female, NA, female, male, female, mal… $ year <int> 2007, 2007, 2007, 2007, 2007, 2007, 2007, 2007, 200… ``` <img src="RLadies-ggplot2_files/figure-html/unnamed-chunk-29-1.png" style="display: block; margin: auto;" /> ].pull-right[ <img src="fig/13_GG_6.png" width="100%" style="display: block; margin: auto;" /> ] --- ## The Grammar of Graphics .pull-left[ <img src="RLadies-ggplot2_files/figure-html/unnamed-chunk-31-1.png" style="display: block; margin: auto;" /> ].pull-right[ <img src="fig/14_GG_7.png" width="100%" style="display: block; margin: auto;" /> ] --- ## The Grammar of Graphics .pull-left[ <img src="RLadies-ggplot2_files/figure-html/unnamed-chunk-33-1.png" style="display: block; margin: auto;" /> <img src="RLadies-ggplot2_files/figure-html/unnamed-chunk-34-1.png" style="display: block; margin: auto;" /> ].pull-right[ <img src="fig/14_GG_7.png" width="100%" style="display: block; margin: auto;" /> ] --- ## The Grammar of Graphics .pull-left[ <img src="RLadies-ggplot2_files/figure-html/unnamed-chunk-36-1.png" style="display: block; margin: auto;" /> **Excel Theme** <img src="RLadies-ggplot2_files/figure-html/unnamed-chunk-37-1.png" style="display: block; margin: auto;" /> ].pull-right[ <img src="fig/7_GG.png" width="100%" style="display: block; margin: auto;" /> ] --- class: inverse, middle, center # The ggplot2 API <img src="fig/ggplot.png" width="40%" style="display: block; margin: auto;" /> --- class: middle, center ## Which dataset to plot? <img src="fig/8_GG_1.png" width="40%" style="display: block; margin: auto;" /> --- .pull-left[ ## `palmerpenguins` data The Palmer Archipelago penguins. Artwork by `@allison_horst`. <img src="fig/17_lter_penguins.png" width="100%" style="display: block; margin: auto;" /> <img src="fig/18_culmen_depth.png" width="70%" style="display: block; margin: auto;" /> ].pull-right[ <img src="fig/15_palmerpenguins.png" width="35%" style="display: block; margin: auto;" /> ``` # A tibble: 6 x 8 species island bill_length_mm bill_depth_mm flipper_length_… body_mass_g sex <fct> <fct> <dbl> <dbl> <int> <int> <fct> 1 Adelie Torge… 39.1 18.7 181 3750 male 2 Adelie Torge… 39.5 17.4 186 3800 fema… 3 Adelie Torge… 40.3 18 195 3250 fema… 4 Adelie Torge… NA NA NA NA <NA> 5 Adelie Torge… 36.7 19.3 193 3450 fema… 6 Adelie Torge… 39.3 20.6 190 3650 male # … with 1 more variable: year <int> ``` ``` Rows: 344 Columns: 8 $ species <fct> Adelie, Adelie, Adelie, Adelie, Adelie, Adelie, Ade… $ island <fct> Torgersen, Torgersen, Torgersen, Torgersen, Torgers… $ bill_length_mm <dbl> 39.1, 39.5, 40.3, NA, 36.7, 39.3, 38.9, 39.2, 34.1,… $ bill_depth_mm <dbl> 18.7, 17.4, 18.0, NA, 19.3, 20.6, 17.8, 19.6, 18.1,… $ flipper_length_mm <int> 181, 186, 195, NA, 193, 190, 181, 195, 193, 190, 18… $ body_mass_g <int> 3750, 3800, 3250, NA, 3450, 3650, 3625, 4675, 3475,… $ sex <fct> male, female, female, NA, female, male, female, mal… $ year <int> 2007, 2007, 2007, 2007, 2007, 2007, 2007, 2007, 200… ``` ] --- ## Which dataset to plot? ```r ggplot() ``` <img src="RLadies-ggplot2_files/figure-html/unnamed-chunk-46-1.png" style="display: block; margin: auto;" /> --- ## Which dataset to plot? ```r *ggplot(data = penguins) ``` <img src="RLadies-ggplot2_files/figure-html/unnamed-chunk-48-1.png" style="display: block; margin: auto;" /> --- class: middle, center ## Mapping <img src="fig/40_mapping.png" width="40%" style="display: block; margin: auto;" /> --- ## Which columns to use for x and y? ```r ggplot(data = penguins, * mapping = aes(x = flipper_length_mm, * y = body_mass_g)) ``` <img src="RLadies-ggplot2_files/figure-html/unnamed-chunk-51-1.png" style="display: block; margin: auto;" /> --- class: middle, center ## Geometries <img src="fig/41_geom.png" width="40%" style="display: block; margin: auto;" /> --- ## How to draw the plot? ```r ggplot(data = penguins, mapping = aes(x = flipper_length_mm, y = body_mass_g)) + * geom_point() ``` <img src="RLadies-ggplot2_files/figure-html/unnamed-chunk-54-1.png" style="display: block; margin: auto;" /> --- class: middle, center ## Data, Mapping and Geometries <img src="fig/20_maingg.png" width="40%" style="display: block; margin: auto;" /> --- ## How to draw the plot? ```r ggplot(data = penguins) + * geom_point(mapping = aes(x = flipper_length_mm, * y = body_mass_g)) ``` <img src="RLadies-ggplot2_files/figure-html/unnamed-chunk-57-1.png" style="display: block; margin: auto;" /> --- ## How to draw the plot? ```r ggplot() + geom_point(mapping = aes(x = flipper_length_mm, y = body_mass_g), * data = penguins) ``` <img src="RLadies-ggplot2_files/figure-html/unnamed-chunk-59-1.png" style="display: block; margin: auto;" /> --- ## Mapping Colours ```r ggplot(penguins) + geom_point( aes(x = flipper_length_mm, y = body_mass_g, * color = species, * shape = species)) ``` <img src="RLadies-ggplot2_files/figure-html/unnamed-chunk-61-1.png" style="display: block; margin: auto;" /> --- ## Mapping Colours ```r ggplot(penguins) + geom_point( aes(x = flipper_length_mm, y = body_mass_g, * colour = flipper_length_mm < 205)) ``` <img src="RLadies-ggplot2_files/figure-html/unnamed-chunk-63-1.png" style="display: block; margin: auto;" /> --- ## Setting Colours ```r ggplot(penguins) + geom_point( aes(x = flipper_length_mm, y = body_mass_g), * colour = 'purple') ``` <img src="RLadies-ggplot2_files/figure-html/unnamed-chunk-65-1.png" style="display: block; margin: auto;" /> --- .pull-left[ ```r ggplot(penguins, aes(x = flipper_length_mm, y = body_mass_g, color = species, shape = species)) + geom_point() + * geom_density_2d() ``` - Syntax starts with `geom_*`. - eg: geom_histogram(), geom_bar(), geom_boxplot(). - Each shape has its own specific aesthetics arguments. ].pull-right[ <img src="RLadies-ggplot2_files/figure-html/unnamed-chunk-67-1.png" style="display: block; margin: auto;" /> ] -- .pull-left[ ```r ggplot(penguins) + geom_histogram( * aes(x = flipper_length_mm)) ``` ].pull-right[ <img src="RLadies-ggplot2_files/figure-html/unnamed-chunk-69-1.png" style="display: block; margin: auto;" /> ] --- Each shape has its own specific aesthetics arguments. `?geom_point` <img src="fig/22_help.png" width="90%" style="display: block; margin: auto;" /> --- class: middle, center ## Statistics <img src="fig/21_statistics.png" width="40%" style="display: block; margin: auto;" /> --- ## `geom_bar()` uses `stat_count()` by default ```r ggplot(penguins) + geom_bar(aes(x = species)) ``` <img src="RLadies-ggplot2_files/figure-html/unnamed-chunk-73-1.png" style="display: block; margin: auto;" /> --- ## `after_stat()` ```r ggplot(penguins) + geom_bar(aes(x = species, * y = after_stat(100*count/ sum (count)) )) ``` <img src="RLadies-ggplot2_files/figure-html/unnamed-chunk-75-1.png" style="display: block; margin: auto;" /> --- ## Old version of ggplot2 ```r ggplot(penguins) + geom_bar(aes(x = species, * y = 100*(..count..)/sum(..count..) )) ``` <img src="RLadies-ggplot2_files/figure-html/unnamed-chunk-77-1.png" style="display: block; margin: auto;" /> --- ## `after_stat()` (ggplot2 3.0.0) ```r ggplot(penguins) + geom_bar(aes(x = species, * y = after_stat(100*count/ sum (count)) )) ``` <img src="RLadies-ggplot2_files/figure-html/unnamed-chunk-79-1.png" style="display: block; margin: auto;" /> --- - There are two ways to use statistical functions. .pull-left[ **define `stat_*()` function and `geom` argument inside that function** ```r ggplot(penguins, aes(x = flipper_length_mm, y = body_mass_g)) + * stat_summary( * geom ="point", fun.y ="mean", colour ="red") ``` <img src="RLadies-ggplot2_files/figure-html/unnamed-chunk-80-1.png" style="display: block; margin: auto;" /> ].pull-right[ **define `geom_*()` function and `stat` argument inside that function** ```r ggplot(penguins, aes(x = flipper_length_mm, y = body_mass_g)) + * geom_point( * stat ="summary", fun.y ="mean", colour ="red") ``` <img src="RLadies-ggplot2_files/figure-html/unnamed-chunk-81-1.png" style="display: block; margin: auto;" /> ] --- Statistics | Geometries ------------|---------- `stat_count` | `geom_bar` `stat_boxplot` | `geom_boxplot` `stat_identity` | `geom_col` `stat_bin` | `geom_bar`, `geom_histogram` `stat_density` | `geom_density` -- .pull-left[ <div class="figure" style="text-align: center"> <img src="fig/23_help.png" alt="?geom_boxplot" width="100%" /> <p class="caption">?geom_boxplot</p> </div> ].pull-right[ <div class="figure" style="text-align: center"> <img src="fig/24_help.png" alt="?geom_bar" width="120%" /> <p class="caption">?geom_bar</p> </div> ] --- class: middle, center ## Scales <img src="fig/25_scale.png" width="40%" style="display: block; margin: auto;" /> --- ## Scales ```r ggplot(penguins) + geom_point( aes(x = flipper_length_mm, y = body_mass_g, * color = species, * shape = species)) ``` <img src="RLadies-ggplot2_files/figure-html/unnamed-chunk-86-1.png" style="display: block; margin: auto;" /> --- ## Scales ```r ggplot(penguins) + geom_point( aes(x = flipper_length_mm, y = body_mass_g, * color = species, * shape = island)) ``` <img src="RLadies-ggplot2_files/figure-html/unnamed-chunk-88-1.png" style="display: block; margin: auto;" /> --- ## Scales ```r ggplot(penguins) + geom_point( aes(x = flipper_length_mm, y = body_mass_g, * color = bill_length_mm, * shape = island)) ``` <img src="RLadies-ggplot2_files/figure-html/unnamed-chunk-90-1.png" style="display: block; margin: auto;" /> --- ## Scales ```r ggplot(penguins) + geom_point(aes(x = flipper_length_mm, y = body_mass_g, color = species)) + * scale_color_brewer(type = 'qual', * palette = 'Dark2') ``` <img src="RLadies-ggplot2_files/figure-html/unnamed-chunk-92-1.png" style="display: block; margin: auto;" /> -- - `scale_<aesthetic>_<type>` --- `RColorBrewer::display.brewer.all()` <img src="fig/26_brewer.png" width="120%" style="display: block; margin: auto;" /> --- ```r ggplot(penguins) + geom_point(aes(x = flipper_length_mm, y = body_mass_g, * color = species)) + * scale_color_viridis_d() ``` <img src="RLadies-ggplot2_files/figure-html/unnamed-chunk-95-1.png" style="display: block; margin: auto;" /> -- - `viridis` and `RColorBrewer` provide different color scales that are robust to color-blindness. -- - For details and an interactive palette selection tools see http://colorbrewer.org --- ```r ggplot(penguins) + geom_point(aes(x = flipper_length_mm, y = body_mass_g, color = species, shape = species, alpha = species)) + * scale_x_continuous( breaks = c(170,200,230)) + * scale_y_log10() + * scale_colour_viridis_d(direction = -1, option= 'plasma') + * scale_shape_manual( values = c(17,18,19)) + * scale_alpha_manual( values = c( "Adelie" = 0.6, "Gentoo" = 0.5, # * "Chinstrap" = 0.7)) ``` <img src="RLadies-ggplot2_files/figure-html/unnamed-chunk-97-1.png" style="display: block; margin: auto;" /> --- class: middle, center ## Facets <img src="fig/28_facets.png" width="40%" style="display: block; margin: auto;" /> --- ## `facet_wrap()` ```r ggplot(penguins) + geom_point(aes( x = flipper_length_mm, y = body_mass_g)) + * facet_wrap(vars(species)) ``` <img src="RLadies-ggplot2_files/figure-html/unnamed-chunk-100-1.png" style="display: block; margin: auto;" /> --- ## `facet_wrap()` ```r ggplot(penguins) + geom_point(aes( x = flipper_length_mm, y = body_mass_g)) + facet_wrap(vars(species), * scales = "free_x") ``` <img src="RLadies-ggplot2_files/figure-html/unnamed-chunk-102-1.png" style="display: block; margin: auto;" /> --- ## `facet_grid()` ```r ggplot(penguins) + geom_point(aes( x = flipper_length_mm, y = body_mass_g)) + * facet_grid( vars(species), vars(sex)) ``` <img src="RLadies-ggplot2_files/figure-html/unnamed-chunk-104-1.png" style="display: block; margin: auto;" /> --- class: middle, center ## Coordinates <img src="fig/29_coord.png" width="40%" style="display: block; margin: auto;" /> --- ## Coordinates ```r ggplot(penguins) + geom_bar(aes(x= species, fill = species)) ``` <img src="RLadies-ggplot2_files/figure-html/unnamed-chunk-107-1.png" style="display: block; margin: auto;" /> --- ```r ggplot(penguins) + geom_bar(aes(x= species, fill = species)) + * coord_flip() ``` <img src="RLadies-ggplot2_files/figure-html/unnamed-chunk-109-1.png" style="display: block; margin: auto;" /> -- - There are two types of coordinate systems: - Linear coordinate systems - Non-linear coordinate systems -- - Linear coordinate systems : `coord_cartesian()`, `coord_flip()`, `coord_fixed()` -- - Non-linear coordinate systems : eg : `coord_map()`, `coord_quickmap()`, `coord_sf()`, `coord_polar()`, `coord_trans()` --- ## Coordinates ```r ggplot(penguins) + geom_bar(aes(x= species, fill = species)) + * coord_polar() ``` <img src="RLadies-ggplot2_files/figure-html/unnamed-chunk-111-1.png" style="display: block; margin: auto;" /> --- ## Coordinates ```r ggplot(penguins) + geom_bar(aes(x= species, fill = species)) + * coord_polar(theta = "y") ``` <img src="RLadies-ggplot2_files/figure-html/unnamed-chunk-113-1.png" style="display: block; margin: auto;" /> --- #### Zooming into a plot ```r ggplot(penguins) + geom_bar(aes(x= year, fill = species)) ``` <img src="RLadies-ggplot2_files/figure-html/unnamed-chunk-115-1.png" style="display: block; margin: auto;" /> --- #### Zooming into a plot with `scale` ```r ggplot(penguins) + geom_bar(aes(x= year, fill = species)) + * scale_y_continuous(limits = c(0,115)) ``` <img src="RLadies-ggplot2_files/figure-html/unnamed-chunk-117-1.png" style="display: block; margin: auto;" /> -- <img src="fig/30_caution.png" width="15%" style="display: block; margin: auto;" /> **<span style="color:red">When zooming with `scale`, any data outside the limits is thrown away</span>** --- #### Proper zoom with `coord_cartesian()` ```r ggplot(penguins) + geom_bar(aes(x= year, fill = species)) + * coord_cartesian(ylim = c(0,115)) ``` <img src="RLadies-ggplot2_files/figure-html/unnamed-chunk-120-1.png" style="display: block; margin: auto;" /> -- <img src="fig/31_mag.jpg" width="15%" style="display: block; margin: auto;" /> **<span style="color:green">Zooming with `coord` is like looking at the plot under a magnifying glass.</span>** --- class: middle, center ## Themes <img src="fig/32_themes.png" width="40%" style="display: block; margin: auto;" /> --- These are complete themes which control all **non-data** display. .pull-left[ ```r ggplot(data = penguins, aes(x = flipper_length_mm, y = body_mass_g)) + geom_point(aes( color = species, shape = species), size = 3, alpha = 0.8) + * theme_minimal() ``` ].pull-right[ <img src="RLadies-ggplot2_files/figure-html/unnamed-chunk-124-1.png" style="display: block; margin: auto;" /> ] -- .pull-left[ ```r ggplot(data = penguins, aes(x = flipper_length_mm, y = body_mass_g)) + geom_point(aes( color = species, shape = species), size = 3, alpha = 0.8) + * theme_dark() ``` ].pull-right[ <img src="RLadies-ggplot2_files/figure-html/unnamed-chunk-126-1.png" style="display: block; margin: auto;" /> ] --- Create custom themes in ggplot. .pull-left[ ```r ggplot(penguins, aes(x = flipper_length_mm, y = body_mass_g)) + geom_point(aes(color = species, shape = species), size = 3, alpha = 0.8) + scale_color_viridis_d() + * theme_minimal() + * labs( * title = "Penguin size, Palmer Station LTER", * subtitle = "Flipper length and body mass for Adelie, Chinstrap and Gentoo Penguins", * x = "Flipper length (mm)", * y = "Body mass (g)", * color = "Penguin species", * shape = "Penguin species") + * theme( * aspect.ratio = 1, * legend.position = c(0.2, 0.7), * legend.background = * element_rect( * fill = "white", * color = NA), * plot.title.position = "plot", * plot.caption = * element_text( * hjust = 0, * face= "italic"), * plot.caption.position = "plot") ``` ].pull-right[ <img src="RLadies-ggplot2_files/figure-html/unnamed-chunk-128-1.png" style="display: block; margin: auto;" /> <img src="RLadies-ggplot2_files/figure-html/unnamed-chunk-129-1.png" style="display: block; margin: auto;" /> ] --- background-image:url('fig/33_gg.png') background-position: 50% 50% background-size: 100% class: top, center --- class: inverse, middle, center # ggplot2 extensions --- background-image:url('fig/34_extension.png') background-position: 50% 90% background-size: 100% class: top, center ### ggplot2 extensions: `https://exts.ggplot2.tidyverse.org/` --- class: middle, center # 1. `patchwork` for plot composition <img src="fig/35_patchwork.png" width="40%" style="display: block; margin: auto;" /> --- ```r p1 <- ggplot(data = penguins, aes(x = flipper_length_mm, y = body_mass_g)) + geom_point(aes(color = species, shape = species), size = 2) + scale_color_manual(values = c("darkorange","darkorchid","cyan4")) + theme(aspect.ratio = 1) p2 <- ggplot(data = penguins, aes(x = bill_length_mm, y = bill_depth_mm)) + geom_point(aes(color = species, shape = species), size = 2) + scale_color_manual(values = c("darkorange","darkorchid","cyan4")) + theme(aspect.ratio = 1) p3 <- ggplot(data = penguins, aes(x = flipper_length_mm)) + geom_histogram(aes(fill = species), alpha = 0.5, position = "identity") + scale_fill_manual(values = c("darkorange","darkorchid","cyan4")) ``` --- ```r library(patchwork) p1 + p3 ``` <img src="RLadies-ggplot2_files/figure-html/unnamed-chunk-132-1.png" style="display: block; margin: auto;" /> --- ```r library(patchwork) (p1 | p2) / p3 ``` <img src="RLadies-ggplot2_files/figure-html/unnamed-chunk-133-1.png" style="display: block; margin: auto;" /> --- ```r library(patchwork) p <- (p1 | p2) / p3 *p + plot_layout(guide = 'collect') ``` <img src="RLadies-ggplot2_files/figure-html/unnamed-chunk-134-1.png" style="display: block; margin: auto;" /> --- ```r library(patchwork) p <- (p1 | p2) / p3 p + plot_layout(guide = 'collect') + plot_annotation( * title = 'Size measurements for adult foraging penguins near Palmer Station, Antarctica', * tag_levels = 'A') ``` <img src="RLadies-ggplot2_files/figure-html/unnamed-chunk-135-1.png" style="display: block; margin: auto;" /> --- ```r library(patchwork) p <- (p1 | p2) / p3 p & * theme(legend.position = 'none') ``` <img src="RLadies-ggplot2_files/figure-html/unnamed-chunk-136-1.png" style="display: block; margin: auto;" /> --- class: middle, center # 2. `plotly` An R package for creating **interactive web graphics** via the open source JavaScript graphing library plotly.js. --- ```r p1 ## a ggplot object ``` <img src="RLadies-ggplot2_files/figure-html/unnamed-chunk-137-1.png" style="display: block; margin: auto;" /> --- ```r plotly::ggplotly(p1) ```
--- class: middle, center # 3. `GGally` --- ```r GGally::ggpairs(penguins[, 1:5], aes(color = species, fill = species))+ scale_color_viridis_d() + scale_fill_viridis_d() ``` <img src="RLadies-ggplot2_files/figure-html/unnamed-chunk-139-1.png" style="display: block; margin: auto;" /> --- class: middle, center # 4. `gganimate` <img src="fig/36_gganimate.png" width="40%" style="display: block; margin: auto;" /> --- ```r library("ggplot2") library("dlstats") data <- cran_stats("ggplot2") p <- ggplot(data, aes(x= end, y = downloads)) + geom_line() + labs(title = "Download stats of ggplot2 package", x = "Time", y = "Downloads") p ``` <img src="RLadies-ggplot2_files/figure-html/unnamed-chunk-141-1.png" style="display: block; margin: auto;" /> --- ```r library(gganimate) p + * transition_reveal(along = end) ``` <img src="RLadies-ggplot2_files/figure-html/unnamed-chunk-142-1.gif" style="display: block; margin: auto;" /> --- ```r p <- ggplot(penguins, aes(flipper_length_mm, body_mass_g , color = species)) + geom_point() + scale_color_viridis_d() + labs(title = "Measurements of penguins {closest_state}")+ * transition_states(states = year) + enter_grow() + exit_fade() p ``` ![](fig/39_gganim.gif) --- class: middle, center # 5. `ggrepel` <img src="fig/37_ggrepel.png" width="40%" style="display: block; margin: auto;" /> --- ### Text annotation ```r df <- penguins %>% filter( flipper_length_mm > 225 ) ggplot(penguins, aes(x=flipper_length_mm, y= body_mass_g))+ geom_point()+ theme(aspect.ratio = 1) + * geom_text(data= df, * aes(x=flipper_length_mm, y= body_mass_g, label= island)) ``` <img src="RLadies-ggplot2_files/figure-html/unnamed-chunk-145-1.png" style="display: block; margin: auto;" /> --- ### Text annotation ```r ggplot(penguins, aes(x=flipper_length_mm, y= body_mass_g))+ geom_point()+ theme(aspect.ratio = 1) + * ggrepel::geom_text_repel(data= df, * aes(x=flipper_length_mm, y= body_mass_g, label= island)) ``` <img src="RLadies-ggplot2_files/figure-html/unnamed-chunk-146-1.png" style="display: block; margin: auto;" /> --- class: middle, center # 6. `ggforce` <img src="fig/38_ggforce.jpeg" width="40%" style="display: block; margin: auto;" /> --- ```r library(ggforce) penguins <- penguins %>% drop_na() p <- ggplot(penguins, aes(x=flipper_length_mm, y= body_mass_g))+ * geom_mark_ellipse(aes( * filter = species == "Gentoo", * label = 'Gentoo penguins'), * description = 'Palmer Station Antarctica LTER and K. Gorman. 2020.') + geom_point() p ``` <img src="RLadies-ggplot2_files/figure-html/unnamed-chunk-148-1.png" style="display: block; margin: auto;" /> --- ```r library(ggforce) ggplot(penguins, aes(x=flipper_length_mm, y= body_mass_g, color = species)) + geom_point() + scale_color_viridis_d() + facet_zoom(x = species == "Gentoo") ``` <img src="RLadies-ggplot2_files/figure-html/unnamed-chunk-149-1.png" style="display: block; margin: auto;" /> --- class: inverse, middle, center # Thank you Slides available at:
prital.netlify.app
pridiltal # Acknowledgements: Hadley Wickham, Thomas Lin Pedersen and ggplot2 development team ## Key References - ggplot2: Elegant Graphics for Data Analysis https://ggplot2-book.org/ - ggplot2 workshop by Thomas Lin Pedersen https://www.youtube.com/watch?v=h29g21z0a68 This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.