class: title-slide <br> <br> .right-panel[ # Visaluzing Data: <br>Part II ## Dr. Mine Dogucu ] --- class: middle ## Review --- class: middle ## Outline - More ggplot - ggplot extensions - Accessible Data Visualization --- class: middle .panelset[ .panel[ .panel-name[Plot] <img src="02b-data-viz2_files/figure-html/unnamed-chunk-3-1.png" style="display: block; margin: auto;" /> ] .panel[ .panel-name[English] - Using the `penguins` data, - Map `bill depth` to x-axis, `bill length` to y-axis, `species` to shape and color. - Add a layer of points and set the size of the points to 4. ] .panel[ .panel-name[R] ```r ggplot(penguins, aes(x = bill_depth_mm, y = bill_length_mm, shape = species, color = species)) + geom_point(size = 4) ``` ] ] --- class: middle ## Labs .panelset[ .panel[ .panel-name[Plot] <img src="02b-data-viz2_files/figure-html/unnamed-chunk-5-1.png" style="display: block; margin: auto;" /> ] .panel[ .panel-name[English] - Using the `penguins` data, - Map `bill depth` to x-axis, `bill length` to y-axis, `species` to shape. - Add a layer of points and set the size of the points to 4. - .highlight-text[Add labels to x-axis (Bill Depth(mm)), y-axis (Bill Length(mm)), and the title of the plot (Palmer Penguins).] .panel[ .panel-name[R] ```r ggplot(penguins, aes(x = bill_depth_mm, y = bill_length_mm, shape = species, color = species)) + geom_point(size = 4) + * labs(x = "Bill Depth (mm)", * y = "Bill Length (mm)", * title = "Palmer Penguins") ``` ] ] ] --- class: middle .left-panel[ ```r ggplot(penguins, aes(x = bill_depth_mm, y = bill_length_mm, shape = species, color = species)) + geom_point() + labs(x = "Bill Depth (mm)", y = "Bill Length (mm)", title = "Palmer Penguins") + * theme_bw() ``` ] .right-panel[ <img src="02b-data-viz2_files/figure-html/unnamed-chunk-8-1.png" style="display: block; margin: auto;" /> ] --- class: middle ## Themes .panelset[ .panel[ .panel-name[`theme_gray()`] <img src="02b-data-viz2_files/figure-html/unnamed-chunk-9-1.png" style="display: block; margin: auto;" /> ] .panel[ .panel-name[`theme_bw()`] <img src="02b-data-viz2_files/figure-html/unnamed-chunk-10-1.png" style="display: block; margin: auto;" /> ] .panel[ .panel-name[`theme_classic()`] <img src="02b-data-viz2_files/figure-html/unnamed-chunk-11-1.png" style="display: block; margin: auto;" /> ] .panel[ .panel-name[`theme_dark()`] <img src="02b-data-viz2_files/figure-html/unnamed-chunk-12-1.png" style="display: block; margin: auto;" /> ] .panel[ .panel-name[`theme_minimal()`] <img src="02b-data-viz2_files/figure-html/unnamed-chunk-13-1.png" style="display: block; margin: auto;" /> ] .panel[ .panel-name[`theme_void()`] <img src="02b-data-viz2_files/figure-html/unnamed-chunk-14-1.png" style="display: block; margin: auto;" /> ] ] --- class: middle .left-panel[ ```r ggplot(penguins, aes(x = bill_depth_mm, y = bill_length_mm, shape = species, color = species)) + geom_point() + labs(x = "Bill Depth (mm)", y = "Bill Length (mm)", title = "Palmer Penguins") + theme_bw() + * theme(text = element_text(size=20)) ``` ] .right-panel[ <img src="02b-data-viz2_files/figure-html/unnamed-chunk-16-1.png" style="display: block; margin: auto;" /> ] --- class: middle ```r ?theme ``` [A blog post on theme elements](https://cmdlinetips.com/2021/05/tips-to-customize-text-color-font-size-in-ggplot2-with-element_text/) --- class: middle .left-panel[ ```r ggplot(penguins, aes(x = bill_depth_mm, y = bill_length_mm, shape = species, color = species)) + geom_point(size = 4) + * facet_grid(.~species) ``` ] .right-panel[ <img src="02b-data-viz2_files/figure-html/unnamed-chunk-19-1.png" style="display: block; margin: auto;" /> ] --- class: middle .left-panel[ ```r ggplot(penguins, aes(x = bill_depth_mm, y = bill_length_mm, shape = species, color = species)) + geom_point(size = 4) + * facet_grid(species~.) ``` ] .right-panel[ <img src="02b-data-viz2_files/figure-html/unnamed-chunk-21-1.png" style="display: block; margin: auto;" /> ] --- class: middle .left-panel[ ```r ggplot(penguins, aes(x = bill_depth_mm, y = bill_length_mm)) + geom_point() + * xlim(0, 30) + * ylim(0, 70) ``` ] .right-panel[ <img src="02b-data-viz2_files/figure-html/unnamed-chunk-23-1.png" style="display: block; margin: auto;" /> ] --- class: middle [Top Ten Dos and Don't for Charts and Graphs](https://guides.lib.uci.edu/datavis/do) --- class: middle Check out [the ggplot flipbook](https://evamaerey.github.io/ggplot_flipbook/ggplot_flipbook_xaringan.html#1) for some inspiration. Find your favorite new function/feature. Share it with your neighbor. --- class: middle ```r library(maps) ``` ``` Warning: package 'maps' was built under R version 4.1.1 ``` ```r usa <- map_data("usa") glimpse(usa) ``` ``` Rows: 7,243 Columns: 6 $ long <dbl> -101.4078, -101.3906, -101.3620, -101.3505, -101.3219, -101.~ $ lat <dbl> 29.74224, 29.74224, 29.65056, 29.63911, 29.63338, 29.64484, ~ $ group <dbl> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, ~ $ order <int> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 1~ $ region <chr> "main", "main", "main", "main", "main", "main", "main", "mai~ $ subregion <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, ~ ``` --- .pull-left[ <br> <br> <br> ```r ggplot(data = usa, aes(x = long, y = lat)) ``` ] .pull-right[ <img src="02b-data-viz2_files/figure-html/unnamed-chunk-27-1.png" style="display: block; margin: auto;" /> ] --- .pull-left[ <br> <br> <br> ```r ggplot(data = usa, aes(x = long, y = lat)) + geom_polygon() ``` ] .pull-right[ <img src="02b-data-viz2_files/figure-html/unnamed-chunk-29-1.png" style="display: block; margin: auto;" /> ] --- class: middle .pull-left[ <br> <br> <br> ```r ggplot(data = usa, aes(x = long, y = lat)) + geom_polygon() + coord_fixed(1.4) ``` ] .pull-right[ <img src="02b-data-viz2_files/figure-html/unnamed-chunk-31-1.png" style="display: block; margin: auto;" /> ] --- .pull-left[ <br> <br> <br> ```r ggplot(data = usa, aes(x = long, y = lat)) + geom_polygon(fill = NA, color = "steelblue") + coord_fixed(1.4) ``` ] .pull-right[ <img src="02b-data-viz2_files/figure-html/unnamed-chunk-33-1.png" style="display: block; margin: auto;" /> ] --- class: middle .pull-left[ <br> <br> <br> ```r ggplot(data = usa, aes(x = long, y = lat)) + geom_polygon(fill = NA, color = "steelblue", aes(group = group)) + coord_fixed(1.4) ``` ] .pull-right[ <img src="02b-data-viz2_files/figure-html/unnamed-chunk-35-1.png" style="display: block; margin: auto;" /> ] --- class: middle ```r location <- c("Northampton", "Columbus", "Sarasota","Irvine" ) long <- c(-72.6412, -82.998795, -82.538602, -117.824719) lat <- c(42.3251, 39.961178, 27.336483, 33.685908) pop <- c(28726, 892533, 57738, 282572) home <- tibble::tibble(location = location, lat = lat, long = long, pop = pop) ``` --- class: middle ```r home ``` ``` # A tibble: 4 x 4 location lat long pop <chr> <dbl> <dbl> <dbl> 1 Northampton 42.3 -72.6 28726 2 Columbus 40.0 -83.0 892533 3 Sarasota 27.3 -82.5 57738 4 Irvine 33.7 -118. 282572 ``` --- .pull-left[ <br> <br> <br> ```r usa_map <- ggplot(data = usa, aes(x = long, y = lat)) + geom_polygon(fill = NA, color = "steelblue", aes(group = group)) + coord_fixed(1.4) ``` ] .pull-right[ ] --- .pull-left[ <br> <br> <br> ```r usa_map + geom_point(data = home, aes(x = long, y = lat, color = pop)) ``` ] .pull-right[ <img src="02b-data-viz2_files/figure-html/unnamed-chunk-41-1.png" style="display: block; margin: auto;" /> ] --- .pull-left[ <br> <br> <br> ```r usa_map + geom_point(data = home, aes(x = long, y = lat, size = pop)) ``` ] .pull-right[ <img src="02b-data-viz2_files/figure-html/unnamed-chunk-43-1.png" style="display: block; margin: auto;" /> ] --- class: middle .pull-left[ <br> <br> <br> ```r states <- map_data("state") ggplot(data = states, aes(x = long, y = lat, fill = region)) + geom_polygon(aes(group = group)) + coord_fixed(1.4) + guides(fill = "none") ``` ] .pull-right[ <img src="02b-data-viz2_files/figure-html/unnamed-chunk-45-1.png" style="display: block; margin: auto;" /> ] --- class: middle `library(sf)` [Maps Chapter from ggplot2 book](https://ggplot2-book.org/maps.html) --- class: middle center inverse .font75[ggplot extensions] --- class: middle - [`patchwork`](https://patchwork.data-imaginist.com/) combining plots into a single plot - [`gganimate`](https://gganimate.com/) animated graphics - [`ggthemes`](https://rafalab.github.io/dsbook/ggplot2.html#add-on-packages) additional set of themes - [`ggtext`](https://wilkelab.org/ggtext/) improved text rendering support for ggplot2 - [`ggdag`](https://ggdag.malco.io/) Causal directed acyclic graphs (DAGs) - [`survminer`](https://rpkgs.datanovia.com/survminer/index.html) survival analysis and visualization - [`gggenes`](https://wilkox.org/gggenes/) gene arrow maps There are [more extensions](https://exts.ggplot2.tidyverse.org/gallery/). --- class: middle center inverse .font75[Making Accessible Visualizations] --- class: middle [Color blindness simulation](https://www.color-blindness.com/coblis-color-blindness-simulator/): red-blind/protanopia <img src="img/red-blind.png" width="60%" style="display: block; margin: auto;" /> --- class: middle [Color blindness simulation](https://www.color-blindness.com/coblis-color-blindness-simulator/): green-blind/deuteranopia <img src="img/green-blind.png" width="60%" style="display: block; margin: auto;" /> --- class: middle [Color blindness simulation](https://www.color-blindness.com/coblis-color-blindness-simulator/): blue-blind/tritanopia <img src="img/blue-blind.png" width="60%" style="display: block; margin: auto;" /> --- class: middle ```r palette.colors(palette = "Okabe-Ito") ``` ``` black orange skyblue bluishgreen yellow "#000000" "#E69F00" "#56B4E9" "#009E73" "#F0E442" blue vermillion reddishpurple gray "#0072B2" "#D55E00" "#CC79A7" "#999999" ``` [Okabe-Ito color palette](https://jfly.uni-koeln.de/color/) allows us to make figures that are colorblind friendly. You can use the HEX code printed for selecting colors. --- class: middle .left-panel[ ```r ggplot(penguins, aes(x = bill_depth_mm, y = bill_length_mm, shape = species, color = species)) + geom_point(size = 4) + facet_grid(species~.) + * scale_color_manual(values = * c("#E69F00", * "#009E73", * "#56B4E9")) ``` ] .right-panel[ <img src="02b-data-viz2_files/figure-html/unnamed-chunk-51-1.png" style="display: block; margin: auto;" /> ] --- class: middle ## Detour: R Markdown chunk options ```{r, echo=TRUE, message=FALSE} ``` --- class: center middle ## (some) Chunk Options in R Markdown <table align = "center"> <tr> <td>echo = FALSE</td> <td>hides the code</td> </tr> <tr> <td>message = FALSE</td> <td>hides messages</td> </tr> <tr> <td>warning = FALSE</td> <td>hides warning</td> </tr> <tr> <td>error = TRUE</td> <td>renders despite errors and displays the error</td> </tr> <tr> <td>fig.cap = "Some figure caption"</td> <td>creates a figure caption</td> </tr> <tr> <td>fig.alt = "Some alternate text for figure"</td> <td>creates alternate text for figures</td> </tr> </table> --- class: middle ## Alternate Text "Alt text" describes contents of an image. It is used in HTML pages such as the R Markdown outputs we have been writing. Screen-readers cannot read images but can read alt text. Alt text has to be provided and cannot magically appear. --- --- class: middle ## Potential Content of Alternate Text for Data Visualization 1. Data source/frame 2. Variables and axes information including units 3. The plot type 4. The message that the plot conveys. --- class: middle Please complete the in-class activity provided to you in lecture notes.