class: center, middle, inverse, title-slide # Data visualization with R ## IASSL Workshop ### Dr. Priyanga D. Talagala, University of Moratuwa ###
21-25, February, 2022
--- background-image:url('fig/tidyworkflow1.png') background-position: 50% 80% background-size: 85% class: top, center # Tidy Workflow --- background-image:url('fig/tidyworkflow2.png') background-position: 50% 80% background-size: 85% class: top, center # Tidy Workflow --- background-image:url('fig/tidyworkflow3.png') background-position: 50% 80% background-size: 85% class: top, center # Tidy Workflow --- ## The Datasaurus Dozen <!--Data visualization and storytelling has always been one of the most important phases of any data science pipeline involving extracting meaningful insights from data, regardless of the complexity of the data or the project. Take a simple example of ‘The Datasaurus Dozen’ — twelve different datasets depicted in the following figure. https://towardsdatascience.com/a-comprehensive-guide-to-the-grammar-of-graphics-for-effective-visualization-of-multi-dimensional-1f92b4ed4149--> .pull-left[ ```r library(datasauRus) library(ggplot2) datasaurus_dozen %>% ggplot(aes(x, y, color = dataset)) + geom_point(show.legend = FALSE) + facet_wrap(~dataset, ncol = 4) ``` ] .pull-right[ <img src="9_Data_visualization_files/figure-html/unnamed-chunk-2-1.png" width="100%" style="display: block; margin: auto;" /> ] --- ## The Datasaurus Dozen .pull-left[ ```r library(datasauRus) library(ggplot2) datasaurus_dozen %>% ggplot(aes(x, y, color = dataset)) + geom_point(show.legend = FALSE) + facet_wrap(~dataset, ncol = 4) ``` </br> Summary statistics | -------------------|---- X Mean | 54.263 Y Mean | 47.832 X SD | 16.765 Y SD | 26.935 Corr. | -0.064 `The Datasaurus was created by Alberto Cairo` ] .pull-right[ <img src="9_Data_visualization_files/figure-html/unnamed-chunk-5-1.png" width="100%" style="display: block; margin: auto;" /> ] <!-- https://r-mageddon.netlify.app/post/reanimating-the-datasaurus/ Can you guess what is common among these very different looking sets of data? Essentially the Datasaurus Dozen is a playful twist on the classic statistical dataset; Anscombe’s Quartet. The Datasaurus Dozen is a group of twelve datasets, with nigh-identical summary statistics, but when plotted on a graph they prove to be distinctly dissimilar. Imagine my delight when I found the Datasaurus Dozen was available on CRAN in the datasauRus package. https://towardsdatascience.com/a-comprehensive-guide-to-the-grammar-of-graphics-for-effective-visualization-of-multi-dimensional-1f92b4ed4149 It turned out that creating an animated version of the Datasaurus Dozen was absurdly simple, taking only 4 lines of code. This is a fun variant of the well known Anscombe’s quartet. It seems crazy to me that all these datasets have the same means, standard deviations and correlations! This is a fun variant of the well known Anscombe’s quartet, which many of you might be very familiar with, as depicted in the following figure. The key takeaway from these demonstrations would be, “Do not trust your data blindly, and start modeling on your data”. Summary statistics can always be deceptive. Always visualize and understand your data attributes before moving on to feature engineering and building statistical, machine learning and deep learning models. --> --- class: inverse, middle, center # Never trust summary statistics <span style="color:#ff00b3">ALONE </span> -- ## Always visualize your data <!-- http://www.thefunctionalart.com/2016/08/download-datasaurus-never-trust-summary.html https://cran.r-project.org/web/packages/datasauRus/vignettes/Datasaurus.html https://cran.r-project.org/web/packages/datasauRus/vignettes/Datasaurus.html --> --- 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="25%" style="display: block; margin: auto;" /> <!-- The original grammar of graphics framework was proposed by Leland Wilkinson, which covers all major aspects pertaining to effective data visualization in detail. I would definitely recommend interested readers to check out the book on it, whenever they get a chance! --> --- <!--https://flowingdata.com/2016/03/22/comparing-ggplot2-and-r-base-graphics/--> .pull-left[ R Base Graphics <img src="9_Data_visualization_files/figure-html/unnamed-chunk-7-1.png" style="display: block; margin: auto;" /> <img src="9_Data_visualization_files/figure-html/unnamed-chunk-8-1.png" style="display: block; margin: auto;" /> ] .pull-right[ <img src="9_Data_visualization_files/figure-html/unnamed-chunk-9-1.png" style="display: block; margin: auto;" /> <img src="9_Data_visualization_files/figure-html/unnamed-chunk-10-1.png" style="display: block; margin: auto;" /> ] --- class: middle, center ## The Grammar of Graphics .pull-left[ .pull-left[ Pie Chart <img src="9_Data_visualization_files/figure-html/unnamed-chunk-11-1.png" style="display: block; margin: auto;" /> Line Chart <img src="9_Data_visualization_files/figure-html/unnamed-chunk-12-1.png" style="display: block; margin: auto;" /> ] .pull-right[ Bar Chart <img src="9_Data_visualization_files/figure-html/unnamed-chunk-13-1.png" style="display: block; margin: auto;" /> Scatterplot <img src="9_Data_visualization_files/figure-html/unnamed-chunk-14-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;" /> ] --- class: inverse, middle, center <!--https://medium.com/@zoldin/introduction-to-ggplot2-the-grammar-81d10e11d908--> # 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 palmerpenguins data contains size measurements for three penguin species observed on three islands in the Palmer Archipelago, Antarctica. https://allisonhorst.github.io/palmerpenguins/articles/intro.html --> 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 × 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, Adel… $ island <fct> Torgersen, Torgersen, Torgersen, Torgersen, Torgerse… $ 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, 186… $ body_mass_g <int> 3750, 3800, 3250, NA, 3450, 3650, 3625, 4675, 3475, … $ sex <fct> male, female, female, NA, female, male, female, male… $ year <int> 2007, 2007, 2007, 2007, 2007, 2007, 2007, 2007, 2007… ``` ] --- ## Which dataset to plot? ```r ggplot() ``` <img src="9_Data_visualization_files/figure-html/unnamed-chunk-23-1.png" style="display: block; margin: auto;" /> --- ## Which dataset to plot? ```r *ggplot(data = penguins) ``` <img src="9_Data_visualization_files/figure-html/unnamed-chunk-25-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="9_Data_visualization_files/figure-html/unnamed-chunk-28-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="9_Data_visualization_files/figure-html/unnamed-chunk-31-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="9_Data_visualization_files/figure-html/unnamed-chunk-34-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="9_Data_visualization_files/figure-html/unnamed-chunk-36-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="9_Data_visualization_files/figure-html/unnamed-chunk-38-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="9_Data_visualization_files/figure-html/unnamed-chunk-40-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="9_Data_visualization_files/figure-html/unnamed-chunk-42-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="9_Data_visualization_files/figure-html/unnamed-chunk-44-1.png" style="display: block; margin: auto;" /> ] -- .pull-left[ ```r ggplot(penguins) + geom_histogram( * aes(x = flipper_length_mm)) ``` ].pull-right[ <img src="9_Data_visualization_files/figure-html/unnamed-chunk-46-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;" /> --- ### Global Data vs Layer Specific Mapping .pull-left[ ```r *ggplot(data = penguins, * aes(x = flipper_length_mm, * y = body_mass_g)) + geom_point() + geom_density_2d() ``` <img src="9_Data_visualization_files/figure-html/unnamed-chunk-48-1.png" style="display: block; margin: auto;" /> ].pull-right[ ```r ggplot() + * geom_point(data = penguins, * aes(x = flipper_length_mm, * y = body_mass_g)) + geom_density_2d() ``` <img src="9_Data_visualization_files/figure-html/unnamed-chunk-49-1.png" style="display: block; margin: auto;" /> ] --- ```r ggplot() + geom_point() ``` <img src="9_Data_visualization_files/figure-html/unnamed-chunk-50-1.png" style="display: block; margin: auto;" /> --- ### Global Data vs Layer Specific Mapping .pull-left[ ```r ggplot(data = penguins, aes(x = flipper_length_mm, y = body_mass_g)) + geom_point() + geom_density_2d() ``` <img src="9_Data_visualization_files/figure-html/unnamed-chunk-51-1.png" style="display: block; margin: auto;" /> ].pull-right[ ```r ggplot() + geom_point(data = penguins, aes(x = flipper_length_mm, y = body_mass_g)) + * geom_density_2d(data = penguins, * aes(x = flipper_length_mm, * y = body_mass_g)) ``` <img src="9_Data_visualization_files/figure-html/unnamed-chunk-52-1.png" style="display: block; margin: auto;" /> ] --- class: middle, center ## Statistics <img src="fig/21_statistics.png" width="40%" style="display: block; margin: auto;" /> --- - There are two ways to use statistical functions. <!--https://medium.com/@zoldin/introduction-to-ggplot2-the-grammar-81d10e11d908--> .pull-left[ **define `stat_*()` function and `geom` argument inside that function** ```r ggplot(penguins, aes(x = flipper_length_mm, y = body_mass_g)) + geom_point() + * stat_summary( * geom ="point", fun.y ="mean", colour ="red") ``` <img src="9_Data_visualization_files/figure-html/unnamed-chunk-54-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() + * geom_point( * stat ="summary", fun.y ="mean", colour ="red") ``` <img src="9_Data_visualization_files/figure-html/unnamed-chunk-55-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="9_Data_visualization_files/figure-html/unnamed-chunk-60-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="9_Data_visualization_files/figure-html/unnamed-chunk-62-1.png" style="display: block; margin: auto;" /> --- ## Scales manual - It's recommended to use a named vector ```r cols <- c("Adelie" = "red", "Chinstrap" = "blue", "Gentoo" = "darkgreen") ggplot(penguins) + geom_point( aes(x = flipper_length_mm, y = body_mass_g, * color = species)) + * scale_colour_manual(values = cols) ``` <img src="9_Data_visualization_files/figure-html/unnamed-chunk-64-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="9_Data_visualization_files/figure-html/unnamed-chunk-66-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="9_Data_visualization_files/figure-html/unnamed-chunk-68-1.png" style="display: block; margin: auto;" /> -- - `scale_<aesthetic>_<type>` --- `RColorBrewer::display.brewer.all()` <!-- <img src="9_Data_visualization_files/figure-html/unnamed-chunk-69-1.png" width="100%" style="display: block; margin: auto;" /> --> <img src="fig/26_brewer.png" width="70%" 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="9_Data_visualization_files/figure-html/unnamed-chunk-72-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 <!--These functions makes it possible to set a custom color palette for each group level. Use a list of colors that are color-blind friendly. R packages such as viridis and RColorBrewer provide different color scales that are robust to color-blindness.--> --- ```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="9_Data_visualization_files/figure-html/unnamed-chunk-74-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)) ``` <!-- help page # Use vars() to supply faceting variables:--> <img src="9_Data_visualization_files/figure-html/unnamed-chunk-77-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") ``` <!-- help page # Use vars() to supply faceting variables:--> <img src="9_Data_visualization_files/figure-html/unnamed-chunk-79-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)) ``` <!-- help page # Use vars() to supply faceting variables:--> <img src="9_Data_visualization_files/figure-html/unnamed-chunk-81-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="9_Data_visualization_files/figure-html/unnamed-chunk-84-1.png" style="display: block; margin: auto;" /> --- ```r ggplot(penguins) + geom_bar(aes(x= species, fill = species)) + * coord_flip() ``` <img src="9_Data_visualization_files/figure-html/unnamed-chunk-86-1.png" style="display: block; margin: auto;" /> -- <!--https://ggplot2-book.org/coord.html--> - 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()` --- ### Accommodating Human Limitations - Pie charts are one of the most overused graphs in the world and in most cases are not the best way to present data. -- - You Shouldn’t Use Pie Charts In Your Dashboards -- - Many visualization software vendors no longer include them in their catalogs. -- - Pie charts are prone to misinterpretation and can easily be turned into disinformation. -- - Humans are not great at judging angles, which is exactly what a pie chart uses to represent size. -- - Lengths are much easier to compare, and length happens to be exactly what a bar chart uses to represent size. -- - Bar charts allows the viewer to make comparisons based on the the length of the bars along a common scale (the y-axis). -- - Humans tend to be more accurate when decoding differences based on these perceptual elements than based on area or color --- 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="9_Data_visualization_files/figure-html/unnamed-chunk-89-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="9_Data_visualization_files/figure-html/unnamed-chunk-91-1.png" style="display: block; margin: auto;" /> ] --- #### Create custom themes in ggplot. ```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-left[ <img src="9_Data_visualization_files/figure-html/unnamed-chunk-93-1.png" style="display: block; margin: auto;" /> ].pull-right[ <img src="9_Data_visualization_files/figure-html/unnamed-chunk-94-1.png" style="display: block; margin: auto;" /> ] <!--Why use custom themes? ggplot2, a popular R package for visualization (used here interchangeably with ggplot), produces graphics with a recognizable default “style”. While the “baked-in” ggplot theme settings can work well from a design standpoint, there is also massive flexibility and potential for customization within the theme() function of ggplot. Almost any non-data component of a plot - axes, titles subtitles, gridlines, and more - can be specified within this function. This customization can help you establish a personal or professional brand for your plots. Many organizations leverage ggplot themes in their publications to create a consistent style across graphics that they produce. https://rpubs.com/mclaire19/ggplot2-custom-themes--> --- 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/` <!--https://mode.com/blog/r-ggplot-extension-packages/--> --- 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="9_Data_visualization_files/figure-html/unnamed-chunk-97-1.png" style="display: block; margin: auto;" /> --- <!-- The + operator simply combines plots without telling patchwork anything about the desired layout. The layout, unless changed with plot_layout() (See the Controlling Layout guide), will simply be a grid with enough rows and columns to contain the number of plots, while being as square as possible. For the special case of putting plots besides each other or on top of each other patchwork provides 2 shortcut operators. | will place plots next to each other while / will place them on top of each other. https://patchwork.data-imaginist.com/articles/guides/assembly.html--> ```r library(patchwork) (p1 | p2) / p3 ``` <img src="9_Data_visualization_files/figure-html/unnamed-chunk-98-1.png" style="display: block; margin: auto;" /> --- ```r library(patchwork) p <- (p1 | p2) / p3 *p + plot_layout(guide = 'collect') ``` <img src="9_Data_visualization_files/figure-html/unnamed-chunk-99-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="9_Data_visualization_files/figure-html/unnamed-chunk-100-1.png" style="display: block; margin: auto;" /> --- ```r library(patchwork) p <- (p1 | p2) / p3 p & * theme(legend.position = 'none') ``` <img src="9_Data_visualization_files/figure-html/unnamed-chunk-101-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="9_Data_visualization_files/figure-html/unnamed-chunk-102-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="9_Data_visualization_files/figure-html/unnamed-chunk-104-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="9_Data_visualization_files/figure-html/unnamed-chunk-106-1.png" style="display: block; margin: auto;" /> --- ```r library(gganimate) p + * transition_reveal(along = end) ``` <img src="9_Data_visualization_files/figure-html/unnamed-chunk-107-1.gif" style="display: block; margin: auto;" /> - Sometimes you might need to install the `png` and `gifski` packages and restart the R-Studio. <!-- https://stackoverflow.com/questions/59664377/gganimate-plot-not-showing-and-saving-bunch-of-pngs And then, I installed the png and gifski packages and restart the R-Studio. It's working. I think, you can try this.--> --- .pull-left[ ```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 ``` ].pull-right[ ![](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="9_Data_visualization_files/figure-html/unnamed-chunk-110-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="9_Data_visualization_files/figure-html/unnamed-chunk-111-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="9_Data_visualization_files/figure-html/unnamed-chunk-113-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="9_Data_visualization_files/figure-html/unnamed-chunk-114-1.png" style="display: block; margin: auto;" /> --- class: inverse, middle, center
pridiltal and thiyangt # Acknowledgements: Hadley Wickham, Thomas Lin Pedersen and ggplot development team This work was supported in part by RETINA research lab funded by the OWSD, a program unit of United Nations Educational, Scientific and Cultural Organization (UNESCO). ## 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 All rights reserved by Thiyanga S. Talagala and Priyanga D Talagala