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: flow # Flow .footnote[[< Contents](#2)] [![:scale 15%](assets/img/section/Chord150.png)](https://r-graph-gallery.com/chord-diagram.html) [![:scale 15%](assets/img/section/Network150.png)](https://r-graph-gallery.com/network.html) [![:scale 15%](assets/img/section/Sankey150.png)](https://r-graph-gallery.com/sankey-diagram.html) [![:scale 15%](assets/img/section/Arc150.png)](https://www.data-to-viz.com/graph/arc.html) [![:scale 15%](assets/img/section/Bundle150.png)](https://r-graph-gallery.com/hierarchical-edge-bundling.html) .bottom[Chord diagram] .bottom[Network] .bottom[Sankey] .bottom[Arc diagram] .bottom[Edge bundling] <br> .black.font120[ - Visualization of **interconnection** between entities - Generally, two datasets are required: **nodes** and **edges** - Data wrangling is a bit different ] --- class: flow # Flow ![:scale 6%](assets/img/section/Network150.png) Network .footnote[[< Contents](#2)] .pull-left-mod[ ```r library(ggraph) library(tidygraph) *as_tbl_graph(highschool) |> mutate(Popularity = centrality_degree( mode="in")) |> *ggraph(layout="kk") + * geom_node_point( * aes(size=Popularity)) + theme(legend.position="top") ``` ] .pull-right-mod[ <img src="ch3_files/figure-html/network-1.png" style="display: block; margin: auto;" /> ] --- class: flow # Flow ![:scale 6%](assets/img/section/Network150.png) Network .footnote[[< Contents](#2)] .pull-left-mod[ ```r library(ggraph) library(tidygraph) as_tbl_graph(highschool) |> mutate(Popularity = centrality_degree( mode="in")) |> ggraph(layout="kk") + geom_node_point( aes(size=Popularity)) + * geom_edge_link( * arrow=arrow( * length=unit(0.1, "inches"), * type="closed")) + theme(legend.position="top") ``` ] .pull-right-mod[ <img src="ch3_files/figure-html/network-edges-1.png" style="display: block; margin: auto;" /> ] --- class: flow # Flow ![:scale 6%](assets/img/section/Network150.png) Network .footnote[[< Contents](#2)] .pull-left-mod[ ```r library(ggraph) library(tidygraph) as_tbl_graph(highschool) |> mutate(Popularity = centrality_degree( mode="in")) |> ggraph(layout="kk") + geom_node_point( aes(size=Popularity)) + * geom_edge_fan( * aes(alpha=after_stat(index)), * show.legend=FALSE) + theme(legend.position="top") ``` ] .pull-right-mod[ <img src="ch3_files/figure-html/network-edges-fan-1.png" style="display: block; margin: auto;" /> ] --- class: flow # Flow ![:scale 6%](assets/img/section/Network150.png) Network .footnote[[< Contents](#2)] .pull-left-mod[ ```r library(ggraph) library(tidygraph) as_tbl_graph(highschool) |> mutate(Popularity = centrality_degree( mode="in")) |> ggraph(layout="kk") + geom_node_point( aes(size=Popularity)) + * geom_edge_fan( * aes(alpha=after_stat(index), * color=factor(year)), * show.legend=FALSE) + theme(legend.position="top") ``` ] .pull-right-mod[ <img src="ch3_files/figure-html/network-edges-color-1.png" style="display: block; margin: auto;" /> ] --- class: flow # Flow ![:scale 6%](assets/img/section/Network150.png) Network .footnote[[< Contents](#2)] .pull-left-mod[ ```r library(ggraph) library(tidygraph) as_tbl_graph(highschool) |> mutate(Popularity = centrality_degree( mode="in")) |> ggraph(layout="kk") + geom_node_point( aes(size=Popularity)) + geom_edge_fan( aes(alpha=after_stat(index)), show.legend=FALSE) + * facet_edges(~year, ncol=1) + theme(legend.position="top") ``` ] .pull-right-mod[ <img src="ch3_files/figure-html/network-facet-1.png" style="display: block; margin: auto;" /> ] --- class: flow # Flow ![:scale 6%](assets/img/section/Network150.png) Network .footnote[[< Contents](#2)] .pull-left-mod[ ```r library(ggraph) library(tidygraph) as_tbl_graph(highschool) |> mutate(Popularity = centrality_degree( mode="in")) |> ggraph(layout="kk") + geom_node_point( aes(size=Popularity)) + geom_edge_fan( aes(alpha=after_stat(index)), show.legend=FALSE) + facet_edges(~year, ncol=1) + * theme_graph(base_size=16, * foreground="#a53253", * fg_text_colour="white") + theme(legend.position="top") ``` ] .pull-right-mod[ <img src="ch3_files/figure-html/network-theme-1.png" style="display: block; margin: auto;" /> ] --- class: flow # Flow ![:scale 6%](assets/img/section/Arc150.png) Arc diagram .footnote[[< Contents](#2)] .pull-left-mod[ ```r library(ggraph) library(tidygraph) as_tbl_graph(highschool) |> mutate(Popularity = centrality_degree( mode="in")) |> *ggraph(layout="linear") + geom_node_point( aes(size=Popularity)) + * geom_edge_arc( aes(alpha=after_stat(index)), show.legend=FALSE) + facet_edges(~year, ncol=1) + theme_graph(base_size=16, foreground="#a53253", fg_text_colour="white") + theme(legend.position="top") ``` ] .pull-right-mod[ <img src="ch3_files/figure-html/network-arc-1.png" style="display: block; margin: auto;" /> ] --- class: flow # Flow ![:scale 6%](assets/img/section/Arc150.png) Arc diagram .footnote[[< Contents](#2)] .pull-left-mod[ ```r library(ggraph) library(tidygraph) as_tbl_graph(highschool) |> mutate(Popularity = centrality_degree( mode="in")) |> *ggraph(layout="linear", circular=TRUE) + * coord_fixed() + geom_node_point( aes(size=Popularity)) + geom_edge_arc( aes(alpha=after_stat(index)), show.legend=FALSE) + facet_edges(~year, ncol=1) + theme_graph(base_size=16, foreground="#a53253", fg_text_colour="white") + theme(legend.position="top") ``` ] .pull-right-mod[ <img src="ch3_files/figure-html/network-circular-1.png" style="display: block; margin: auto;" /> ] --- class: flow # Flow ![:scale 6%](assets/img/section/Bundle150.png) Edge bundling .footnote[[< Contents](#2) | Data from [**ggraph manual**](https://ggraph.data-imaginist.com/reference/geom_conn_bundle.html)] .pull-left-mod[ ```r library(ggraph) library(tidygraph) # flareGraph <- ... # importFrom <- ... # importTo <- ... *ggraph(flareGraph, "dendrogram", * circular=TRUE) + * coord_fixed() + geom_node_point() + theme_graph(base_size=16) ``` ] .pull-right-mod[ <img src="ch3_files/figure-html/network-bundle-1.png" style="display: block; margin: auto;" /> ] --- class: flow # Flow ![:scale 6%](assets/img/section/Bundle150.png) Edge bundling .footnote[[< Contents](#2) | Data from [**ggraph manual**](https://ggraph.data-imaginist.com/reference/geom_conn_bundle.html)] .pull-left-mod[ ```r library(ggraph) library(tidygraph) # flareGraph <- ... # importFrom <- ... # importTo <- ... ggraph(flareGraph, "dendrogram", circular=TRUE) + coord_fixed() + geom_node_point( * aes(filter=leaf, color=class)) + theme_graph(base_size=16) + theme(legend.position="top") ``` ] .pull-right-mod[ <img src="ch3_files/figure-html/network-bundle-nodes-1.png" style="display: block; margin: auto;" /> ] --- class: flow # Flow ![:scale 6%](assets/img/section/Bundle150.png) Edge bundling .footnote[[< Contents](#2) | Data from [**ggraph manual**](https://ggraph.data-imaginist.com/reference/geom_conn_bundle.html)] .pull-left-mod[ ```r library(ggraph) library(tidygraph) # flareGraph <- ... # importFrom <- ... # importTo <- ... ggraph(flareGraph, "dendrogram", circular=TRUE) + coord_fixed() + geom_node_point( aes(filter=leaf, color=class)) + * geom_conn_bundle( * aes(color=after_stat(index)), * data=get_con(importFrom, importTo), * edge_alpha=0.25) + theme_graph(base_size=16) + theme(legend.position="top") ``` ] .pull-right-mod[ <img src="ch3_files/figure-html/network-bundle-edges-1.png" style="display: block; margin: auto;" /> ] --- class: flow # Flow ![:scale 6%](assets/img/section/Bundle150.png) Edge bundling .footnote[[< Contents](#2) | Data from [**ggraph manual**](https://ggraph.data-imaginist.com/reference/geom_conn_bundle.html)] .pull-left-mod[ ```r library(ggraph) library(tidygraph) # flareGraph <- ... # importFrom <- ... # importTo <- ... ggraph(flareGraph, "dendrogram", circular=TRUE) + coord_fixed() + geom_node_point( aes(filter=leaf, color=class)) + geom_conn_bundle( aes(color=after_stat(index)), data=get_con(importFrom, importTo), edge_alpha=0.25) + * scale_edge_colour_distiller( * NULL, guide="edge_direction") + theme_graph(base_size=16) + theme(legend.position="top") ``` ] .pull-right-mod[ <img src="ch3_files/figure-html/network-bundle-guide-1.png" style="display: block; margin: auto;" /> ] --- class: flow # Flow ![:scale 6%](assets/img/section/Chord150.png) Chord diagram .footnote[[< Contents](#2)] .pull-left-mod[ - Out of the scope - Requires `circlize`, `chorddiag` - See the [**source**](https://www.data-to-viz.com/graph/chord.html) ] .pull-right-mod[ <img src="ch3_files/chorddiagram.png" width="860" style="display: block; margin: auto;" /> ] --- class: flow # Flow ![:scale 6%](assets/img/section/Sankey150.png) Sankey / Alluvial .footnote[[< Contents](#2)] .pull-left-mod[ ```r # remotes::install_github( # "davidsjoberg/ggsankey") df <- mtcars |> ggsankey::make_long( cyl, vs, am, gear, carb) ggplot(df) + * aes(x, next_x=next_x, * node=node, next_node=next_node, fill=factor(node)) + * ggsankey::geom_sankey() + scale_fill_viridis_d() + labs(x=NULL) + theme(legend.position="top") ``` ] .pull-right-mod[ <img src="ch3_files/figure-html/sankey-1.png" style="display: block; margin: auto;" /> ] --- class: flow # Flow ![:scale 6%](assets/img/section/Sankey150.png) Sankey / Alluvial .footnote[[< Contents](#2)] .pull-left-mod[ ```r # remotes::install_github( # "davidsjoberg/ggsankey") df <- mtcars |> ggsankey::make_long( cyl, vs, am, gear, carb) ggplot(df) + aes(x, next_x=next_x, node=node, next_node=next_node, fill=factor(node)) + ggsankey::geom_sankey( * flow.alpha=0.6, * node.color="gray30") + scale_fill_viridis_d() + labs(x=NULL) + theme(legend.position="top") ``` ] .pull-right-mod[ <img src="ch3_files/figure-html/sankey-nodes-1.png" style="display: block; margin: auto;" /> ] --- class: flow # Flow ![:scale 6%](assets/img/section/Sankey150.png) Sankey / Alluvial .footnote[[< Contents](#2)] .pull-left-mod[ ```r # remotes::install_github( # "davidsjoberg/ggsankey") df <- mtcars |> ggsankey::make_long( cyl, vs, am, gear, carb) ggplot(df) + aes(x, next_x=next_x, node=node, next_node=next_node, fill=factor(node), * label=node) + ggsankey::geom_sankey( flow.alpha=0.6, node.color="gray30") + * ggsankey::geom_sankey_label( * size=3, color="white", * fill="gray40") + scale_fill_viridis_d() + labs(x=NULL) + theme(legend.position="none") ``` ] .pull-right-mod[ <img src="ch3_files/figure-html/sankey-labels-1.png" style="display: block; margin: auto;" /> ] --- class: flow # Flow ![:scale 6%](assets/img/section/Sankey150.png) Sankey / Alluvial .footnote[[< Contents](#2)] .pull-left-mod[ ```r # remotes::install_github( # "davidsjoberg/ggsankey") df <- mtcars |> ggsankey::make_long( cyl, vs, am, gear, carb) ggplot(df) + aes(x, next_x=next_x, node=node, next_node=next_node, fill=factor(node), label=node) + ggsankey::geom_sankey( flow.alpha=0.6, node.color="gray30") + ggsankey::geom_sankey_label( size=3, color="white", fill="gray40") + scale_fill_viridis_d() + * ggsankey::theme_sankey(base_size=16) + labs(x=NULL) + theme(legend.position="none") ``` ] .pull-right-mod[ <img src="ch3_files/figure-html/sankey-theme-1.png" style="display: block; margin: auto;" /> ] --- class: flow # Flow ![:scale 6%](assets/img/section/Map150.png)![:scale 6%](assets/img/section/Network150.png) Geospatial network .footnote[[< Contents](#2)] .pull-left-mod[ ```r library(tidygraph) *library(sfnetworks) *net <- as_sfnetwork(roxel, directed=F) |> activate("nodes") |> mutate(bc = centrality_betweenness()) ggplot() + * geom_sf(data=sf::st_as_sf(net, "edges"), * color="grey50") + * geom_sf(data=sf::st_as_sf(net, "nodes"), * aes(color=bc)) + scale_color_viridis_c() + theme(legend.position=c(1, 0), legend.justification=c(1, 0)) ``` ] .pull-right-mod[ <img src="ch3_files/figure-html/sfnetwork-1.png" style="display: block; margin: auto;" /> ] --- class: flow # Flow ![:scale 6%](assets/img/section/Map150.png)![:scale 6%](assets/img/section/Network150.png) Geospatial network .footnote[[< Contents](#2)] .pull-left-mod[ ```r *library(ggraph) library(tidygraph) library(sfnetworks) net <- as_sfnetwork(roxel, directed=F) |> activate("nodes") |> mutate(bc = centrality_betweenness()) *ggraph(net) + * geom_edge_link(color="grey50") + * geom_node_point(aes(color=bc)) + scale_color_viridis_c() + theme(legend.position=c(1, 0), legend.justification=c(1, 0)) ``` ] .pull-right-mod[ <img src="ch3_files/figure-html/sfnetwork-ggraph-1.png" style="display: block; margin: auto;" /> ] --- class: flow # Flow ![:scale 6%](assets/img/section/Map150.png)![:scale 6%](assets/img/section/Network150.png) Geospatial network .footnote[[< Contents](#2)] .pull-left-mod[ ```r library(ggraph) library(tidygraph) library(sfnetworks) net <- as_sfnetwork(roxel, directed=F) |> activate("nodes") |> mutate(bc = centrality_betweenness()) *layout_sf <- function(graph) { * graph <- activate(graph, "nodes") * data.frame( * x=sf::st_coordinates(graph)[,"X"], * y=sf::st_coordinates(graph)[,"Y"]) *} *ggraph(net, layout=layout_sf) + geom_edge_link(color="grey50") + geom_node_point(aes(color=bc)) + scale_color_viridis_c() + theme(legend.position=c(1, 0), legend.justification=c(1, 0)) ``` ] .pull-right-mod[ <img src="ch3_files/figure-html/sfnetwork-ggraph-layout-1.png" style="display: block; margin: auto;" /> ] --- class: flow # Flow ![:scale 6%](assets/img/section/Map150.png)![:scale 6%](assets/img/section/Network150.png) Geospatial network .footnote[[< Contents](#2)] .pull-left-mod[ ```r library(ggraph) library(tidygraph) library(sfnetworks) net <- as_sfnetwork(roxel, directed=F) |> activate("nodes") |> mutate(bc = centrality_betweenness()) layout_sf <- function(graph) { graph <- activate(graph, "nodes") data.frame( x=sf::st_coordinates(graph)[,"X"], y=sf::st_coordinates(graph)[,"Y"]) } ggraph(net, layout=layout_sf) + geom_edge_link(color="grey50") + geom_node_point(aes(color=bc)) + * coord_sf(crs=sf::st_crs(net)) + scale_color_viridis_c() + theme(legend.position=c(1, 0), legend.justification=c(1, 0)) ``` ] .pull-right-mod[ <img src="ch3_files/figure-html/sfnetwork-ggraph-coord-1.png" style="display: block; margin: auto;" /> ]