class: center, middle, inverse, title-slide .title[ # Data Visualization ] .subtitle[ ## Chapter 3. Data Visualization in R ] .author[ ### Iñaki Úcar ] .institute[ ### Department of Statistics | uc3m-Santander Big Data Institute ] .institute[ ### Master in Computational Social Science ] .date[ ###
Licensed under Creative Commons Attribution
CC BY 4.0
Last generated: 2023-01-25
] --- class: base24 # Directory of Visualizations .footnote[Based on [The R Graph Gallery](https://r-graph-gallery.com/)] .pull-left[ - .distribution[[Distribution](ch3_1.html#3)].icons[ ![:scale 10%](assets/img/section/Violin150.png) ![:scale 10%](assets/img/section/Density150.png) ![:scale 10%](assets/img/section/Histogram150.png) ![:scale 10%](assets/img/section/Box1150.png) ![:scale 10%](assets/img/section/Joyplot150.png) ] - .correlation[[Correlation](ch3_2.html#3)].icons[ ![:scale 10%](assets/img/section/ScatterPlot150.png) ![:scale 10%](assets/img/section/Heatmap150.png) ![:scale 10%](assets/img/section/Correlogram150.png) ![:scale 10%](assets/img/section/BubblePlot150.png) ![:scale 10%](assets/img/section/ScatterConnected150.png) ![:scale 10%](assets/img/section/2dDensity150.png) ] - .ranking[[Ranking](ch3_3.html#3)].icons[ ![:scale 10%](assets/img/section/Bar150.png) ![:scale 10%](assets/img/section/Spider150.png) ![:scale 10%](assets/img/section/Wordcloud150.png) ![:scale 10%](assets/img/section/Parallel1150.png) ![:scale 10%](assets/img/section/Lollipop150.png) ![:scale 10%](assets/img/section/CircularBarplot150.png) ] - .part[[Part of a Whole](ch3_4.html#3)].icons[ ![:scale 10%](assets/img/section/GroupedRed150.png) ![:scale 10%](assets/img/section/Tree150.png) ![:scale 10%](assets/img/section/Doughnut150.png) ![:scale 10%](assets/img/section/Pie150.png) ![:scale 10%](assets/img/section/Dendrogram150.png) ![:scale 10%](assets/img/section/CircularPacking150.png) ] ] .pull-right[ - .evolution[[Evolution](ch3_5.html#3)].icons[ ![:scale 10%](assets/img/section/Line150.png) ![:scale 10%](assets/img/section/Area150.png) ![:scale 10%](assets/img/section/StackedArea150.png) ![:scale 10%](assets/img/section/Stream150.png) ![:scale 10%](assets/img/section/Time150.gif) ] - .map[[Map](ch3_6.html#3)].icons[ ![:scale 10%](assets/img/section/Map150.png) ![:scale 10%](assets/img/section/Choropleth150.png) ![:scale 10%](assets/img/section/MapHexbin150.png) ![:scale 10%](assets/img/section/Cartogram150.png) ![:scale 10%](assets/img/section/ConnectedMap150.png) ![:scale 10%](assets/img/section/BubbleMap150.png) ] - .flow[[Flow](ch3_7.html#3)].icons[ ![:scale 10%](assets/img/section/Chord150.png) ![:scale 10%](assets/img/section/Network150.png) ![:scale 10%](assets/img/section/Sankey150.png) ![:scale 10%](assets/img/section/Arc150.png) ![:scale 10%](assets/img/section/Bundle150.png) ] - .other[[Other resources](ch3_8.html#3)].icons[ ![:scale 10%](assets/img/section/anim150.gif) ![:scale 10%](assets/img/section/Interactive150.png) ![:scale 10%](assets/img/section/Bad150.png) ![:scale 10%](assets/img/section/DataArt1150.png) ] ] --- class: map # Map .footnote[[< Contents](#2)] [![:scale 15%](assets/img/section/Map150.png)](https://r-graph-gallery.com/map.html) [![:scale 15%](assets/img/section/Choropleth150.png)](https://r-graph-gallery.com/choropleth-map.html) [![:scale 15%](assets/img/section/MapHexbin150.png)](https://r-graph-gallery.com/hexbin-map.html) [![:scale 15%](assets/img/section/Cartogram150.png)](https://r-graph-gallery.com/cartogram.html) [![:scale 15%](assets/img/section/ConnectedMap150.png)](https://r-graph-gallery.com/connection-map.html) [![:scale 15%](assets/img/section/BubbleMap150.png)](https://r-graph-gallery.com/bubble-map.html) .bottom[Map] .bottom[Choropleth] .bottom[Hexbin map] .bottom[Cartogram] .bottom[Connection] .bottom[Bubble map] <br> .black.font120[ - Visualizaton of **geospatial data** - Usually, the map is just a reference - Sometimes, it **encodes** the information ] --- class: map # Map ![:scale 6%](assets/img/section/Map150.png) Map .footnote[[< Contents](#2) | Many resources and examples at [**r-spatial.org**](https://r-spatial.org/)] .pull-left-mod[ ```r world <- giscoR::gisco_get_countries() ggplot(world) + * geom_sf() ``` ] .pull-right-mod[ <img src="ch3_files/figure-html/map-1.png" style="display: block; margin: auto;" /> ] --- class: map # Map ![:scale 6%](assets/img/section/Map150.png) Map .footnote[[< Contents](#2)] .pull-left-mod[ ```r *usa <- sf::st_as_sf(maps::map( "state", plot=FALSE, fill=TRUE)) ggplot(usa) + geom_sf() ``` ] .pull-right-mod[ <img src="ch3_files/figure-html/map-usa-1.png" style="display: block; margin: auto;" /> ] --- class: map # Map ![:scale 6%](assets/img/section/Map150.png) Map .footnote[[< Contents](#2)] .pull-left-mod[ ```r africa <- giscoR::gisco_get_countries( * region="Africa") ggplot(africa) + geom_sf() ``` ] .pull-right-mod[ <img src="ch3_files/figure-html/map-africa-1.png" style="display: block; margin: auto;" /> ] --- class: map # Map ![:scale 6%](assets/img/section/Map150.png) Map .footnote[[< Contents](#2) | See [**Coordinate Reference Systems**](https://en.wikipedia.org/wiki/Spatial_reference_system)] .pull-left-mod[ ```r africa <- giscoR::gisco_get_countries( region="Africa") ggplot(africa) + geom_sf() + * coord_sf(xlim=c(-20, 50), * ylim=c(-40, 40)) ``` ] .pull-right-mod[ <img src="ch3_files/figure-html/map-africa-cut-1.png" style="display: block; margin: auto;" /> ] --- class: map # Map ![:scale 6%](assets/img/section/Choropleth150.png) Choropleth .footnote[[< Contents](#2)] .pull-left-mod[ ```r africa <- giscoR::gisco_get_countries( region="Africa") |> * left_join( * africamonitor::am_data( * series="SP_POP_TOTL", * from=2021, to=2021), * by=c("ISO3_CODE"="ISO3")) ggplot(africa) + * geom_sf(aes(fill=SP_POP_TOTL)) + coord_sf(xlim=c(-20, 50), ylim=c(-40, 40)) + scale_fill_viridis_c() + labs(fill="Population") + theme(legend.position=c(0, 0), legend.justification=c(0, 0)) ``` ] .pull-right-mod[ <img src="ch3_files/figure-html/choropleth-1.png" style="display: block; margin: auto;" /> ] --- class: map # Map ![:scale 6%](assets/img/section/Cartogram150.png) Cartogram .footnote[[< Contents](#2)] .pull-left-mod[ ```r africa <- giscoR::gisco_get_countries( region="Africa") |> left_join( africamonitor::am_data( series="SP_POP_TOTL", from=2021, to=2021), by=c("ISO3_CODE"="ISO3")) |> sf::st_transform(3857) |> * cartogram::cartogram_cont( * "SP_POP_TOTL", itermax=5) |> sf::st_transform(4326) ggplot(africa) + * geom_sf(aes(fill=SP_POP_TOTL)) + coord_sf(xlim=c(-20, 50), ylim=c(-40, 40)) + scale_fill_viridis_c() + labs(fill="Population") + theme(legend.position=c(0, 0), legend.justification=c(0, 0)) ``` ] .pull-right-mod[ <img src="ch3_files/figure-html/cartogram-1.png" style="display: block; margin: auto;" /> ] --- class: map # Map ![:scale 6%](assets/img/section/BubbleMap150.png) Bubble map .footnote[[< Contents](#2)] .pull-left-mod[ ```r africa <- giscoR::gisco_get_countries( region="Africa") |> left_join( africamonitor::am_data( series="SP_POP_TOTL", from=2021, to=2021), by=c("ISO3_CODE"="ISO3")) *centroids <- africa |> * sf::st_centroid() *ggplot(centroids) + geom_sf(data=africa) + * geom_sf(aes(size=SP_POP_TOTL)) + coord_sf(xlim=c(-20, 50), ylim=c(-40, 40)) + labs(size="Population") + theme(legend.position=c(0, 0), legend.justification=c(0, 0)) ``` ] .pull-right-mod[ <img src="ch3_files/figure-html/bubble-1.png" style="display: block; margin: auto;" /> ] --- class: map # Map ![:scale 6%](assets/img/section/ConnectedMap150.png) Connection .footnote[[< Contents](#2)] .pull-left-mod[ ```r africa <- giscoR::gisco_get_countries( region="Africa") |> left_join( africamonitor::am_data( series="SP_POP_TOTL", from=2021, to=2021), by=c("ISO3_CODE"="ISO3")) centroids <- africa |> sf::st_centroid() *lines <- centroids |> * sf::st_union() |> * sf::st_cast("LINESTRING") *ggplot(lines) + geom_sf(data=africa) + geom_sf(aes(size=SP_POP_TOTL), data=centroids) + * geom_sf() + coord_sf(xlim=c(-20, 50), ylim=c(-40, 40)) + labs(size="Population") + theme(legend.position=c(0, 0), legend.justification=c(0, 0)) ``` ] .pull-right-mod[ <img src="ch3_files/figure-html/connection-1.png" style="display: block; margin: auto;" /> ] --- class: map # Map ![:scale 6%](assets/img/section/MapHexbin150.png) Hexbin map .footnote[[< Contents](#2)] .pull-left-mod[ ```r esp <- mapSpain::esp_get_country() *hexccaa <- mapSpain::esp_get_hex_ccaa() *ggplot(hexccaa) + geom_sf(data=esp) + * geom_sf(aes(fill=codauto), alpha=0.3, show.legend=FALSE) + geom_sf_text( aes(label=label), check_overlap=TRUE) + theme_void(base_size=16) ``` ] .pull-right-mod[ <img src="ch3_files/figure-html/hexbin-1.png" style="display: block; margin: auto;" /> ] --- class: map # Map ![:scale 6%](assets/img/section/MapHexbin150.png) Hexbin map .footnote[[< Contents](#2)] .pull-left-mod[ ```r esp <- mapSpain::esp_get_country() *hexprov <- mapSpain::esp_get_hex_prov() *ggplot(hexprov) + geom_sf(data=esp) + geom_sf(aes(fill=codauto), alpha=0.3, show.legend=FALSE) + geom_sf_text( aes(label=label), check_overlap=TRUE) + theme_void(base_size=16) ``` ] .pull-right-mod[ <img src="ch3_files/figure-html/hexbin-prov-1.png" style="display: block; margin: auto;" /> ] --- class: map # Map ![:scale 6%](assets/img/section/MapHexbin150.png) Hexbin map .footnote[[< Contents](#2)] .pull-left-mod[ ```r esp <- mapSpain::esp_get_country() *gridprov <- mapSpain::esp_get_grid_prov() *ggplot(gridprov) + geom_sf(data=esp) + geom_sf(aes(fill=codauto), alpha=0.3, show.legend=FALSE) + geom_sf_text( aes(label=label), check_overlap=TRUE) + theme_void(base_size=16) ``` ] .pull-right-mod[ <img src="ch3_files/figure-html/hexbin-prov-grid-1.png" style="display: block; margin: auto;" /> ]