class: center, middle, inverse, title-slide .title[ # An Open-source Odyssey: Crafting Data Narratives with R ] .author[ ### Data Visualization Camp - UoM Research Week 2023
Dr. Priyanga D. Talagala
] .date[ ###
01/12/2023
] --- class: center, middle, inverse ## Welcome to the Data Visualization Camp - UoM Research Week 2023 ### An Open-source Odyssey: Crafting Data Narratives with R -- <span style="font-size:8mm; font-weight:bold"> Dr. Priyanga D. Talagala</span> <span style="font-size:7mm; font-weight:bold"> Senior Lecturer, Faculty of Information Technology, UoM</span> <span style="font-size:7mm; font-weight:bold">PhD in Statistics, Monash University, Australia</span> <span style="font-size:7mm; font-weight:bold">Email: priyangad@uom.lk</span> -- <span style="font-size:7mm; font-weight:bold">Web: prital.netlify.app</span> <span style="font-size:7mm; font-weight:bold">My research focuses on statistical machine learning, data mining and time series and forecasting</span> --- ## Workshop Website: datavizcamp.netlify.app <img src="fig/QR.png" width="40%" style="display: block; margin: auto;" /> --- 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 --- <!--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[ ## The Datasaurus Dozen ```r library(datasauRus) library(ggplot2) datasaurus_dozen |> ggplot(aes(x, y, color = dataset)) + geom_point(show.legend = FALSE) + facet_wrap(~dataset, ncol = 4) ``` ```r head(datasaurus_dozen) ``` ``` ## # A tibble: 6 × 3 ## dataset x y ## <chr> <dbl> <dbl> ## 1 dino 55.4 97.2 ## 2 dino 51.5 96.0 ## 3 dino 46.2 94.5 ## 4 dino 42.8 91.4 ## 5 dino 40.8 88.3 ## 6 dino 38.7 84.9 ``` ] .pull-right[ <img src="Data_visualization_files/figure-html/unnamed-chunk-4-1.png" width="150%" style="display: block; margin: auto;" /> ] --- .pull-left[ ## The Datasaurus Dozen ```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="Data_visualization_files/figure-html/unnamed-chunk-7-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/--> #### R Base Graphics .pull-left[ <img src="Data_visualization_files/figure-html/unnamed-chunk-9-1.png" style="display: block; margin: auto;" /> <img src="Data_visualization_files/figure-html/unnamed-chunk-10-1.png" style="display: block; margin: auto;" /> ] .pull-right[ <img src="Data_visualization_files/figure-html/unnamed-chunk-11-1.png" style="display: block; margin: auto;" /> <img src="Data_visualization_files/figure-html/unnamed-chunk-12-1.png" style="display: block; margin: auto;" /> ] --- class: middle, center ## The Grammar of Graphics .pull-left[ .pull-left[ Pie Chart <img src="Data_visualization_files/figure-html/unnamed-chunk-13-1.png" style="display: block; margin: auto;" /> Line Chart <img src="Data_visualization_files/figure-html/unnamed-chunk-14-1.png" style="display: block; margin: auto;" /> ] .pull-right[ Bar Chart <img src="Data_visualization_files/figure-html/unnamed-chunk-15-1.png" style="display: block; margin: auto;" /> Scatterplot <img src="Data_visualization_files/figure-html/unnamed-chunk-16-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_mm body_mass_g <fct> <fct> <dbl> <dbl> <int> <int> 1 Adelie Torgersen 39.1 18.7 181 3750 2 Adelie Torgersen 39.5 17.4 186 3800 3 Adelie Torgersen 40.3 18 195 3250 4 Adelie Torgersen NA NA NA NA 5 Adelie Torgersen 36.7 19.3 193 3450 6 Adelie Torgersen 39.3 20.6 190 3650 # ℹ 2 more variables: sex <fct>, 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="Data_visualization_files/figure-html/unnamed-chunk-25-1.png" style="display: block; margin: auto;" /> --- ## Which dataset to plot? ```r *ggplot(data = penguins) ``` <img src="Data_visualization_files/figure-html/unnamed-chunk-27-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="Data_visualization_files/figure-html/unnamed-chunk-30-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="Data_visualization_files/figure-html/unnamed-chunk-33-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="Data_visualization_files/figure-html/unnamed-chunk-36-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="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, * color = species, * shape = species)) ``` <img src="Data_visualization_files/figure-html/unnamed-chunk-40-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="Data_visualization_files/figure-html/unnamed-chunk-42-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="Data_visualization_files/figure-html/unnamed-chunk-44-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="Data_visualization_files/figure-html/unnamed-chunk-46-1.png" style="display: block; margin: auto;" /> ] -- .pull-left[ ```r ggplot(penguins) + geom_histogram( * aes(x = flipper_length_mm)) ``` ].pull-right[ <img src="Data_visualization_files/figure-html/unnamed-chunk-48-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;" /> --- The statistic layer is responsible for calculating summary statistics for your data. ```r ggplot(penguins, aes(x = species, fill = species)) + * geom_bar() + * geom_text(stat = "count", aes(label = ..count..), vjust = -0.5) ``` <img src="Data_visualization_files/figure-html/unnamed-chunk-51-1.png" style="display: block; margin: auto;" /> --- The statistic layer is responsible for calculating summary statistics for your data. ```r ggplot(penguins, aes(x = species, fill = species)) + * geom_bar(stat = "count") + # Default Setting geom_text(stat = "count", aes(label = ..count..), vjust = -0.5) ``` <img src="Data_visualization_files/figure-html/unnamed-chunk-52-1.png" style="display: block; margin: auto;" /> --- Statistics | Geometries ------------|---------- `stat_count` | `geom_bar` `stat_boxplot` | `geom_boxplot` `stat_identity` | `geom_col` `stat_identity` | `geom_point` `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 The scale layer customizes how the values of the mapped variables are represented visually. ```r ggplot(penguins) + geom_point( aes(x = flipper_length_mm, y = body_mass_g, * color = species, * shape = species)) ``` <img src="Data_visualization_files/figure-html/unnamed-chunk-57-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="Data_visualization_files/figure-html/unnamed-chunk-59-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="Data_visualization_files/figure-html/unnamed-chunk-61-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="Data_visualization_files/figure-html/unnamed-chunk-63-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="Data_visualization_files/figure-html/unnamed-chunk-65-1.png" style="display: block; margin: auto;" /> -- - `scale_<aesthetic>_<type>` --- `RColorBrewer::display.brewer.all()` <!-- <img src="Data_visualization_files/figure-html/unnamed-chunk-66-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="Data_visualization_files/figure-html/unnamed-chunk-69-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="Data_visualization_files/figure-html/unnamed-chunk-71-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 layer allows you to create multiple plots, each showing a subset of your data. ## `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="Data_visualization_files/figure-html/unnamed-chunk-74-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="Data_visualization_files/figure-html/unnamed-chunk-76-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="Data_visualization_files/figure-html/unnamed-chunk-78-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 How the plot's coordinate system is structured. ```r ggplot(penguins) + geom_bar(aes(x= species, fill = species)) ``` <img src="Data_visualization_files/figure-html/unnamed-chunk-81-1.png" style="display: block; margin: auto;" /> --- ```r ggplot(penguins) + geom_bar(aes(x= species, fill = species)) + * coord_flip() ``` <img src="Data_visualization_files/figure-html/unnamed-chunk-83-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()` --- 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="Data_visualization_files/figure-html/unnamed-chunk-86-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="Data_visualization_files/figure-html/unnamed-chunk-88-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="Data_visualization_files/figure-html/unnamed-chunk-90-1.png" style="display: block; margin: auto;" /> ].pull-right[ <img src="Data_visualization_files/figure-html/unnamed-chunk-91-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="Data_visualization_files/figure-html/unnamed-chunk-94-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="Data_visualization_files/figure-html/unnamed-chunk-95-1.png" style="display: block; margin: auto;" /> --- ```r library(patchwork) p <- (p1 | p2) / p3 *p + plot_layout(guide = 'collect') ``` <img src="Data_visualization_files/figure-html/unnamed-chunk-96-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="Data_visualization_files/figure-html/unnamed-chunk-97-1.png" style="display: block; margin: auto;" /> --- ```r library(patchwork) p <- (p1 | p2) / p3 p & * theme(legend.position = 'none') ``` <img src="Data_visualization_files/figure-html/unnamed-chunk-98-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="Data_visualization_files/figure-html/unnamed-chunk-99-1.png" style="display: block; margin: auto;" /> --- ```r plotly::ggplotly(p1) ```
--- class: middle, center # 3. `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="Data_visualization_files/figure-html/unnamed-chunk-102-1.png" style="display: block; margin: auto;" /> --- ```r library(gganimate) p + * transition_reveal(along = end) ``` <img src="Data_visualization_files/figure-html/unnamed-chunk-103-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.--> --- ```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 # 4. `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="Data_visualization_files/figure-html/unnamed-chunk-106-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="Data_visualization_files/figure-html/unnamed-chunk-107-1.png" style="display: block; margin: auto;" /> --- class: middle, center # 5. `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="Data_visualization_files/figure-html/unnamed-chunk-109-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="Data_visualization_files/figure-html/unnamed-chunk-110-1.png" style="display: block; margin: auto;" /> --- class: middle, center # 6. `esquisse` <img src="fig/42_esquisse.png" width="50%" style="display: block; margin: auto;" /> --- ## Reverse Engineer Your Visualizations: From Plots to Code ```r library("esquisse") esquisse::esquisser() # or with your data: esquisse::esquisser(palmerpenguins::penguins) ``` --- class: middle, center ## 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 Priyanga D. Talagala