class: center, middle, inverse, title-slide .title[ # Advanced Visualizations ] .subtitle[ ## Advances in Computer Language ] .author[ ### Dr. Zulfiqar Ali (Assistant Professor) ] .institute[ ### College of Statistcal Sciences, University of the Punjab, Lahore ] .date[ ### 15 June 2023 ] --- ## Supplementary materials Additional resources - [Extend ggplot2](https://ggplot2.tidyverse.org/articles/extending-ggplot2.html) by creating your own stat, geom, and theme - [Network visualization with `ggraph`](https://ggraph.data-imaginist.com) - [Plotly ggplot2 library](https://plotly.com/ggplot2/) - [Template themes with `ggthemes`](https://github.com/jrnold/ggthemes) --- class: inverse, center, middle # `ggplot2` extensions --- ## Packages For these slides we will use the following packages. .normal[ ```r library(tidyverse) library(gapminder) # some data library(ggcorrplot) # correlogram plots library(ggpol) # parliament plots and more library(patchwork) # combining plots library(gganimate) # animations library(ggiraph) # interactive plots ``` ] <br><br><br><br> **Note: You have to run the code by yourself for seeing maps in this lecture.** --- ## Download the data Flint water crisis from the following weblink ```r flint <- read_csv("http://www2.stat.duke.edu/~sms185/data/health/flint.csv") flint ``` ``` #> # A tibble: 271 × 6 #> id zip ward draw1 draw2 draw3 #> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> #> 1 1 48504 6 0.344 0.226 0.145 #> 2 2 48507 9 8.13 10.8 2.76 #> 3 4 48504 1 1.11 0.11 0.123 #> 4 5 48507 8 8.01 7.45 3.38 #> 5 6 48505 3 1.95 0.048 0.035 #> 6 7 48507 9 7.2 1.4 0.2 #> 7 8 48507 9 40.6 9.73 6.13 #> 8 9 48503 5 1.1 2.5 0.1 #> 9 12 48507 9 10.6 1.04 1.29 #> 10 13 48505 3 6.2 4.2 2.3 #> # ℹ 261 more rows ``` --- class: inverse, center, middle # Correlation Matrix of multivariate data using `ggcorrplot` --- ### Display of Correlation matrix 1 .tiny[ ```r corr_mat <- round(cor(flint[, c("draw1", "draw2", "draw3")]), 2) ggcorrplot(corr = corr_mat, lab = TRUE) ``` <img src="lec_09_files/figure-html/unnamed-chunk-4-1.png" style="display: block; margin: auto;" /> ] --- ### Display of Correlation matrix 2 .tiny[ ```r corr_mat <- round(cor(flint[, c("draw1", "draw2", "draw3")]), 2) ggcorrplot(corr = corr_mat, hc.order = TRUE, type = "lower", outline.color = "white") ``` <img src="lec_09_files/figure-html/unnamed-chunk-5-1.png" style="display: block; margin: auto;" /> ] --- ### Display of Correlation matrix 3 .tiny[ ```r corr_mat <- round(cor(flint[, c("draw1", "draw2", "draw3")]), 2) ggcorrplot(corr = corr_mat,hc.order = TRUE, type = "lower", outline.col = "black",ggtheme = ggplot2::theme_classic(), colors = c("chartreuse1","aquamarine","cornflowerblue","darkblue","lightgoldenrodyellow", "lightgoldenrod1","tomato","firebrick","darkred")) ``` <img src="lec_09_files/figure-html/unnamed-chunk-6-1.png" style="display: block; margin: auto;" /> ] --- ## Let consider another popular data of mtcars .tiny[ ```r data(mtcars) corr <- round(cor(mtcars), 1) corr ``` ``` #> mpg cyl disp hp drat wt qsec vs am gear carb #> mpg 1.0 -0.9 -0.8 -0.8 0.7 -0.9 0.4 0.7 0.6 0.5 -0.6 #> cyl -0.9 1.0 0.9 0.8 -0.7 0.8 -0.6 -0.8 -0.5 -0.5 0.5 #> disp -0.8 0.9 1.0 0.8 -0.7 0.9 -0.4 -0.7 -0.6 -0.6 0.4 #> hp -0.8 0.8 0.8 1.0 -0.4 0.7 -0.7 -0.7 -0.2 -0.1 0.7 #> drat 0.7 -0.7 -0.7 -0.4 1.0 -0.7 0.1 0.4 0.7 0.7 -0.1 #> wt -0.9 0.8 0.9 0.7 -0.7 1.0 -0.2 -0.6 -0.7 -0.6 0.4 #> qsec 0.4 -0.6 -0.4 -0.7 0.1 -0.2 1.0 0.7 -0.2 -0.2 -0.7 #> vs 0.7 -0.8 -0.7 -0.7 0.4 -0.6 0.7 1.0 0.2 0.2 -0.6 #> am 0.6 -0.5 -0.6 -0.2 0.7 -0.7 -0.2 0.2 1.0 0.8 0.1 #> gear 0.5 -0.5 -0.6 -0.1 0.7 -0.6 -0.2 0.2 0.8 1.0 0.3 #> carb -0.6 0.5 0.4 0.7 -0.1 0.4 -0.7 -0.6 0.1 0.3 1.0 ``` ] --- ## Compute p values .tiny[ ```r p.mat <- cor_pmat(mtcars) p.mat ``` ``` #> mpg cyl disp hp drat #> mpg 0.000000e+00 6.112687e-10 9.380327e-10 1.787835e-07 1.776240e-05 #> cyl 6.112687e-10 0.000000e+00 1.802838e-12 3.477861e-09 8.244636e-06 #> disp 9.380327e-10 1.802838e-12 0.000000e+00 7.142679e-08 5.282022e-06 #> hp 1.787835e-07 3.477861e-09 7.142679e-08 0.000000e+00 9.988772e-03 #> drat 1.776240e-05 8.244636e-06 5.282022e-06 9.988772e-03 0.000000e+00 #> wt 1.293959e-10 1.217567e-07 1.222320e-11 4.145827e-05 4.784260e-06 #> qsec 1.708199e-02 3.660533e-04 1.314404e-02 5.766253e-06 6.195826e-01 #> vs 3.415937e-05 1.843018e-08 5.235012e-06 2.940896e-06 1.167553e-02 #> am 2.850207e-04 2.151207e-03 3.662114e-04 1.798309e-01 4.726790e-06 #> gear 5.400948e-03 4.173297e-03 9.635921e-04 4.930119e-01 8.360110e-06 #> carb 1.084446e-03 1.942340e-03 2.526789e-02 7.827810e-07 6.211834e-01 #> wt qsec vs am gear #> mpg 1.293959e-10 1.708199e-02 3.415937e-05 2.850207e-04 5.400948e-03 #> cyl 1.217567e-07 3.660533e-04 1.843018e-08 2.151207e-03 4.173297e-03 #> disp 1.222320e-11 1.314404e-02 5.235012e-06 3.662114e-04 9.635921e-04 #> hp 4.145827e-05 5.766253e-06 2.940896e-06 1.798309e-01 4.930119e-01 #> drat 4.784260e-06 6.195826e-01 1.167553e-02 4.726790e-06 8.360110e-06 #> wt 0.000000e+00 3.388683e-01 9.798492e-04 1.125440e-05 4.586601e-04 #> qsec 3.388683e-01 0.000000e+00 1.029669e-06 2.056621e-01 2.425344e-01 #> vs 9.798492e-04 1.029669e-06 0.000000e+00 3.570439e-01 2.579439e-01 #> am 1.125440e-05 2.056621e-01 3.570439e-01 0.000000e+00 5.834043e-08 #> gear 4.586601e-04 2.425344e-01 2.579439e-01 5.834043e-08 0.000000e+00 #> carb 1.463861e-02 4.536949e-05 6.670496e-04 7.544526e-01 1.290291e-01 #> carb #> mpg 1.084446e-03 #> cyl 1.942340e-03 #> disp 2.526789e-02 #> hp 7.827810e-07 #> drat 6.211834e-01 #> wt 1.463861e-02 #> qsec 4.536949e-05 #> vs 6.670496e-04 #> am 7.544526e-01 #> gear 1.290291e-01 #> carb 0.000000e+00 ``` ] --- ### Visualize the correlation matrix (method = "square" or "circle") .tiny[ ```r ggcorrplot(corr) ``` <img src="lec_09_files/figure-html/unnamed-chunk-9-1.png" style="display: block; margin: auto;" /> ] --- ### Add circle .tiny[ ```r ggcorrplot(corr, method = "circle") ``` <img src="lec_09_files/figure-html/unnamed-chunk-10-1.png" style="display: block; margin: auto;" /> --- ### Reordering the correlation matrix .tiny[ ```r ggcorrplot(corr, hc.order = TRUE, outline.color = "white") ``` <img src="lec_09_files/figure-html/unnamed-chunk-11-1.png" style="display: block; margin: auto;" /> --- ### Types of correlogram layout .tiny[ ```r ggcorrplot(corr, hc.order = TRUE, type = "lower", outline.color = "white" ) ``` <img src="lec_09_files/figure-html/unnamed-chunk-12-1.png" style="display: block; margin: auto;" /> --- ### Get the upeper triangle .tiny[ ```r ggcorrplot(corr, hc.order = TRUE, type = "lower", outline.color = "white" ) ``` <img src="lec_09_files/figure-html/unnamed-chunk-13-1.png" style="display: block; margin: auto;" /> --- ### Change colors and theme .tiny[ ```r ggcorrplot(corr, hc.order = TRUE, type = "lower", outline.color = "white", ggtheme = ggplot2::theme_gray, colors = c("#6D9EC1", "white", "#E46726") ) ``` <img src="lec_09_files/figure-html/unnamed-chunk-14-1.png" style="display: block; margin: auto;" /> --- ### Add correlation coefficients .tiny[ ```r ggcorrplot(corr, hc.order = TRUE, type = "lower", lab = TRUE, ggtheme = ggplot2::theme_linedraw(), ) ``` <img src="lec_09_files/figure-html/unnamed-chunk-15-1.png" style="display: block; margin: auto;" /> --- ### Add correlation significance level .tiny[ ```r ggcorrplot(corr, hc.order = TRUE, type = "lower", p.mat = p.mat ) ``` <img src="lec_09_files/figure-html/unnamed-chunk-16-1.png" style="display: block; margin: auto;" /> --- ### Leave blank on no significant coefficient .tiny[ ```r ggcorrplot(corr, p.mat = p.mat, hc.order = TRUE, type = "lower", insig = "blank" ) ``` <img src="lec_09_files/figure-html/unnamed-chunk-17-1.png" style="display: block; margin: auto;" /> --- ### Changing number of digits for correlation coeffcient .tiny[ ```r ggcorrplot(cor(mtcars), type = "lower", insig = "blank", lab = TRUE, digits = 3 ) ``` <img src="lec_09_files/figure-html/unnamed-chunk-18-1.png" style="display: block; margin: auto;" /> --- class: inverse, center, middle # Interactive plots: `ggiraph` --- ## Interactive Africa map with ggigraph ### Set of R packages ```r library(sp) library(sf) library(ggplot2) library(ggiraph) library(ggthemes) library("maptools") library("mapproj") library("maps") library(rnaturalearth) library(tidyverse) library(dplyr) library(janitor) ``` --- ## map data - Now, let’s load the necessary maps. We’ll begin with the World map and then filter it to obtain the map of Africa. - For this purpose, we’ll utilize the rnaturalearth package, which offers map data that can be seamlessly used with other R packages. --- ## map data2 ```r world <- sf::st_as_sf(countries110) world <- world %>%clean_names() africa <- dplyr::filter(world, region_un=='Africa')%>% st_transform(crs="+proj=laea +lon_0=18.984375") ``` --- ## data preparation - In this example, our focus is to represent the growth rate of GDP per capita in African countries over a span of 10 years (from 2021 to 2011). - To accomplish this, we will utilize World Bank data and the specific indicator NY.GDP.PCAP.CD. In order to proceed, we will require the WDI package in R. --- ## data preparation (code) ```r dt<-WDI::WDI(indicator = c("NY.GDP.PCAP.CD"),start = 2010,end=2021,extra = T) dt<-dt%>%filter(year%in%c(2011,2021)) dt<-dt%>%dplyr::filter(year%in%c(2011,2021)) dt<-dt%>%dplyr::filter(iso3c%in%africa$iso_a3)%>% dplyr::select(iso3c,year,NY.GDP.PCAP.CD) dt<-dt%>%pivot_wider(id_cols = c('iso3c'),names_from = 'year',values_from = 'NY.GDP.PCAP.CD',names_prefix = 'y') dt%>%head() ``` ``` #> # A tibble: 6 × 3 #> iso3c y2021 y2011 #> <chr> <dbl> <dbl> #> 1 DZA 3691. 5473. #> 2 AGO 1954. 4511. #> 3 BEN 1319. 1099. #> 4 BWA 6805. 7081. #> 5 BFA 893. 728. #> 6 BDI 221. 236. ``` --- ### We will calculate the growth rate of GDP per capita between the years 2011 and 2021. ```r dt=dt%>%mutate(chg_rt=(y2021-y2011)/y2011) dt%>%head() ``` ``` #> # A tibble: 6 × 4 #> iso3c y2021 y2011 chg_rt #> <chr> <dbl> <dbl> <dbl> #> 1 DZA 3691. 5473. -0.326 #> 2 AGO 1954. 4511. -0.567 #> 3 BEN 1319. 1099. 0.200 #> 4 BWA 6805. 7081. -0.0389 #> 5 BFA 893. 728. 0.227 #> 6 BDI 221. 236. -0.0633 ``` --- ### We will add the newly created variable to the ‘africa’ dataset. ```r africa<-africa%>%left_join(dt,by=c("iso_a3"="iso3c")) ``` --- ### create the data for the map (code) ```r africa.centers <- st_centroid(africa) africa.spdf <- methods::as(africa, 'Spatial') africa.spdf@data$id <- row.names(africa.spdf@data) africa.tidy <- broom::tidy(africa.spdf) africa.tidy <- dplyr::left_join(africa.tidy, africa.spdf@data, by='id') africa.tidy <- africa.tidy %>% mutate(labs=ifelse(is.na(chg_rt)==T,NA, paste0(round(100*chg_rt,1),"%")))%>% mutate(chg_rt=100*chg_rt) ``` --- ### Final data ```r africa.tidy<-africa.tidy%>% mutate(tip=ifelse(is.na(chg_rt)==T, paste0("<b>",name,"</b><br>","Data not available"), paste0("<b>",name,"</b><br>",labs))) head(africa.tidy) ``` ``` #> # A tibble: 6 × 180 #> long lat order hole piece group id featurecla scalerank labelrank #> <dbl> <dbl> <int> <lgl> <fct> <fct> <chr> <chr> <int> <int> #> 1 1655955. -105941. 1 FALSE 1 1.1 1 Admin-0 c… 1 3 #> 2 1674553. -118210. 2 FALSE 1 1.1 1 Admin-0 c… 1 3 #> 3 2071747. -347023. 3 FALSE 1 1.1 1 Admin-0 c… 1 3 #> 4 2078296. -412048. 4 FALSE 1 1.1 1 Admin-0 c… 1 3 #> 5 2233378. -525137. 5 FALSE 1 1.1 1 Admin-0 c… 1 3 #> 6 2179627. -662913. 6 FALSE 1 1.1 1 Admin-0 c… 1 3 #> # ℹ 170 more variables: sovereignt <chr>, sov_a3 <chr>, adm0_dif <int>, #> # level <int>, type <chr>, tlc <chr>, admin <chr>, adm0_a3 <chr>, #> # geou_dif <int>, geounit <chr>, gu_a3 <chr>, su_dif <int>, subunit <chr>, #> # su_a3 <chr>, brk_diff <int>, name <chr>, name_long <chr>, brk_a3 <chr>, #> # brk_name <chr>, brk_group <chr>, abbrev <chr>, postal <chr>, #> # formal_en <chr>, formal_fr <chr>, name_ciawf <chr>, note_adm0 <chr>, #> # note_brk <chr>, name_sort <chr>, name_alt <chr>, mapcolor7 <int>, … ``` --- ### Code for map (ggplot2) ```r g <- ggplot(africa.tidy) + geom_polygon_interactive( color='black',size=.5, aes(long, lat, group=group, fill=chg_rt, tooltip=tip))+ guides(fill=guide_legend(title="10 Years \n GDP per capita \n growth rate"))+ theme_bw()+ coord_fixed()+ scale_fill_gradientn(colours=c("red","white","blue"), na.value ='lightgray', values=scales::rescale(c(-80,0,180)))+ labs(title='GDP per capita growth rate \n from 2011 to 2021')+ theme_tufte(ticks = FALSE) + theme(axis.line=element_blank(), axis.text.x=element_blank(), axis.text.y=element_blank(), axis.ticks=element_blank(), axis.title.x=element_blank(), axis.title.y=element_blank(), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.border = element_blank(), panel.background = element_blank()) ``` --- ### Display map under ggplot2 ```r g ``` <img src="lec_09_files/figure-html/unnamed-chunk-27-1.png" style="display: block; margin: auto;" /> --- ### Displaying the map ```r ggiraph(code=print(g),width = .6, width_svg = 4, height_svg = 4, tooltip_opacity = 0.5, hover_css = "{fill:orange;}") ```
--- ## Package `ggiraph` - Add tooltips, animations, and JavaScript actions to ggplot graphics - In general, instead of `geom_<plot_type>()` use `geom_<plot_type>_interactive()` - Interactivity is added to ggplot geometries, legends and theme elements, via the following aesthetics: - tooltip: tooltips to be displayed when mouse is over elements. - onclick: JavaScript function to be executed when elements are clicked. - data_id: id to be associated with elements (used for hover and click actions) - Function `girafe()` translates the graphic into an interactive web-based graphic - See https://github.com/davidgohel/ggiraph --- class: inverse, center, middle # Home Exercise --- ## Flint water data Create a visualization of the data from object `flint`. Incorporate topics from today's lecture. ```r flint <- read_csv("http://www2.stat.duke.edu/~sms185/data/health/flint.csv") flint ``` ``` #> # A tibble: 271 × 6 #> id zip ward draw1 draw2 draw3 #> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> #> 1 1 48504 6 0.344 0.226 0.145 #> 2 2 48507 9 8.13 10.8 2.76 #> 3 4 48504 1 1.11 0.11 0.123 #> 4 5 48507 8 8.01 7.45 3.38 #> 5 6 48505 3 1.95 0.048 0.035 #> 6 7 48507 9 7.2 1.4 0.2 #> 7 8 48507 9 40.6 9.73 6.13 #> 8 9 48503 5 1.1 2.5 0.1 #> 9 12 48507 9 10.6 1.04 1.29 #> 10 13 48505 3 6.2 4.2 2.3 #> # ℹ 261 more rows ``` --- ## References 1. A Grammar of Animated Graphics. (2020). https://gganimate.com/ 2. Create GIFs with gifski in knitr Documents - Yihui Xie | 谢益辉. (2020). https://yihui.org/en/2018/08/gifski-knitr/ 3. davidgohel/ggiraph. (2020). https://github.com/davidgohel/ggiraph 4. erocoar/ggpol. (2020). https://github.com/erocoar/ggpol 5. Extending ggplot2. (2020). https://ggplot2.tidyverse.org/articles/extending-ggplot2.html 6. thomasp85/patchwork. (2020). https://github.com/thomasp85/patchwork 7. Top 50 ggplot2 Visualizations - The Master List (With Full R Code). (2020). http://r-statistics.co/Top50-Ggplot2-Visualizations-MasterList-R-Code.html 8. https://rpubs.com/mdhafer/interactivafrica