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: correlation # Correlation .footnote[[< Contents](#2)] [![:scale 15%](assets/img/section/ScatterPlot150.png)](https://r-graph-gallery.com/scatterplot.html) [![:scale 15%](assets/img/section/Heatmap150.png)](https://r-graph-gallery.com/heatmap.html) [![:scale 15%](assets/img/section/Correlogram150.png)](https://r-graph-gallery.com/correlogram.html) [![:scale 15%](assets/img/section/BubblePlot150.png)](https://r-graph-gallery.com/bubble-chart.html) [![:scale 15%](assets/img/section/ScatterConnected150.png)](https://r-graph-gallery.com/connected-scatterplot.html) [![:scale 15%](assets/img/section/2dDensity150.png)](https://r-graph-gallery.com/2d-density-chart.html) .bottom[Scatter] .bottom[Heatmap] .bottom[Correlogram] .bottom[Bubble] .bottom[Connected scatter] .bottom[Density 2D] <br> .black.font120[ - Visualization of the **relationship** between two variables - Two continuous, or two discrete, or mixed - Options to include a third one ] --- class: correlation # Correlation ![:scale 6%](assets/img/section/ScatterPlot150.png) Scatter .footnote[[< Contents](#2)] .pull-left-mod[ ```r gapminder::gapminder |> filter(year == 1997) |> ggplot() + aes(gdpPercap, lifeExp) + scale_x_log10() + * geom_point() ``` ] .pull-right-mod[ <img src="ch3_files/figure-html/scatter-1.png" style="display: block; margin: auto;" /> ] --- class: correlation # Correlation ![:scale 6%](assets/img/section/ScatterPlot150.png) Scatter .footnote[[< Contents](#2)] .pull-left-mod[ ```r gapminder::gapminder |> filter(year == 1997) |> ggplot() + aes(gdpPercap, lifeExp) + scale_x_log10() + * geom_point(aes(color=continent)) + theme(legend.position=c(1, 0), legend.justification=c(1, 0)) ``` ] .pull-right-mod[ <img src="ch3_files/figure-html/scatter-color-1.png" style="display: block; margin: auto;" /> ] --- class: correlation # Correlation ![:scale 6%](assets/img/section/BubblePlot150.png) Bubble .footnote[[< Contents](#2)] .pull-left-mod[ ```r gapminder::gapminder |> filter(year == 1997) |> ggplot() + aes(gdpPercap, lifeExp) + scale_x_log10() + geom_point(aes(color=continent, * size=pop)) + theme(legend.position=c(1, 0), legend.justification=c(1, 0)) ``` ] .pull-right-mod[ <img src="ch3_files/figure-html/scatter-bubble-1.png" style="display: block; margin: auto;" /> ] --- class: correlation # Correlation ![:scale 6%](assets/img/section/BubblePlot150.png) Bubble .footnote[[< Contents](#2)] .pull-left-mod[ ```r gapminder::gapminder |> filter(year == 1997) |> ggplot() + aes(gdpPercap, lifeExp) + scale_x_log10() + geom_point(aes(color=continent, size=pop), * alpha=0.7) + * scale_size_area(max_size=20) + theme(legend.position=c(1, 0), legend.justification=c(1, 0)) ``` ] .pull-right-mod[ <img src="ch3_files/figure-html/scatter-bubble-scale-1.png" style="display: block; margin: auto;" /> ] --- class: correlation # Correlation ![:scale 6%](assets/img/section/BubblePlot150.png) Bubble .footnote[[< Contents](#2)] .pull-left-mod[ ```r gapminder::gapminder |> filter(year == 1997) |> ggplot() + aes(gdpPercap, lifeExp) + scale_x_log10() + geom_point(aes(color=continent, size=pop), alpha=0.7) + scale_size_area(max_size=20) + * geom_smooth() + theme(legend.position=c(1, 0), legend.justification=c(1, 0)) ``` ] .pull-right-mod[ <img src="ch3_files/figure-html/scatter-smooth-1.png" style="display: block; margin: auto;" /> ] --- class: correlation # Correlation ![:scale 6%](assets/img/section/BubblePlot150.png) Bubble .footnote[[< Contents](#2)] .pull-left-mod[ ```r gapminder::gapminder |> filter(year == 1997) |> ggplot() + aes(gdpPercap, lifeExp) + scale_x_log10() + geom_point(aes(color=continent, size=pop), alpha=0.7) + scale_size_area(max_size=20) + * geom_smooth(method="lm") + theme(legend.position=c(1, 0), legend.justification=c(1, 0)) ``` ] .pull-right-mod[ <img src="ch3_files/figure-html/scatter-smooth-lm-1.png" style="display: block; margin: auto;" /> ] --- class: correlation # Correlation ![:scale 6%](assets/img/section/ScatterConnected150.png) Connected Scatter .footnote[[< Contents](#2)] .pull-left-mod[ ```r gapminder::gapminder |> filter(year == 1997) |> ggplot() + aes(gdpPercap, lifeExp) + scale_x_log10() + geom_point(aes(color=continent)) + * geom_line(aes(color=continent)) + theme(legend.position=c(1, 0), legend.justification=c(1, 0)) ``` ] .pull-right-mod[ <img src="ch3_files/figure-html/scatter-connected-1.png" style="display: block; margin: auto;" /> ] --- class: correlation # Correlation ![:scale 6%](assets/img/section/ScatterConnected150.png) Connected Scatter .footnote[[< Contents](#2)] .pull-left-mod[ ```r babynames::babynames |> filter(name %in% c( "Ashley", "Amanda")) |> filter(sex == "F") |> filter(year > 1970) |> select(year, name, n) |> spread(key = name, value=n, -1) |> ggplot() + * aes(Amanda, Ashley, color=year) + geom_point() + * geom_path() + scale_color_viridis_c() + theme(legend.position=c(0, 1), legend.justification=c(0, 1)) ``` ] .pull-right-mod[ <img src="ch3_files/figure-html/scatter-connected-path-1.png" style="display: block; margin: auto;" /> ] --- class: correlation # Correlation ![:scale 6%](assets/img/section/ScatterConnected150.png) Connected Scatter .footnote[[< Contents](#2)] .pull-left-mod[ ```r df <- babynames::babynames |> filter(name %in% c( "Ashley", "Amanda")) |> filter(sex == "F") |> filter(year > 1970) |> select(year, name, n) |> spread(key = name, value=n, -1) *text <- df |> filter(year %in% c( 1972, 1980, 1984, 1987, 2012)) ggplot(df) + aes(Amanda, Ashley, color=year) + * geom_path(arrow=arrow( * angle=15, type="closed", * length=unit(0.1, "inches"))) + scale_color_viridis_c() + * geom_label(aes(label=year), text) + theme(legend.position=c(0, 1), legend.justification=c(0, 1)) ``` ] .pull-right-mod[ <img src="ch3_files/figure-html/scatter-connected-path-label-1.png" style="display: block; margin: auto;" /> ] --- class: correlation # Correlation ![:scale 6%](assets/img/section/ScatterPlot150.png) Scatter .footnote[[< Contents](#2)] .pull-left-mod[ ```r ggplot(faithful) + aes(eruptions, waiting) + * geom_point() ``` ] .pull-right-mod[ <img src="ch3_files/figure-html/scatter-distribution-1.png" style="display: block; margin: auto;" /> ] --- class: correlation # Correlation ![:scale 6%](assets/img/section/ScatterPlot150.png) Scatter .footnote[[< Contents](#2)] .pull-left-mod[ ```r ggplot(faithful) + aes(eruptions, waiting) + geom_point() + * ggside::geom_xsidehistogram() ``` ] .pull-right-mod[ <img src="ch3_files/figure-html/scatter-distribution-margin-1.png" style="display: block; margin: auto;" /> ] --- class: correlation # Correlation ![:scale 6%](assets/img/section/2dDensity150.png) Density 2D .footnote[[< Contents](#2)] .pull-left-mod[ ```r ggplot(faithful) + aes(eruptions, waiting) + * geom_bin2d() + scale_fill_viridis_c() + ggside::geom_xsidehistogram() + theme(legend.position=c(1, 0), legend.justification=c(1, 0)) ``` ] .pull-right-mod[ <img src="ch3_files/figure-html/density2d-histogram-1.png" style="display: block; margin: auto;" /> ] --- class: correlation # Correlation ![:scale 6%](assets/img/section/2dDensity150.png) Density 2D .footnote[[< Contents](#2)] .pull-left-mod[ ```r ggplot(faithful) + aes(eruptions, waiting) + * geom_hex() + scale_fill_viridis_c() + ggside::geom_xsidehistogram() + theme(legend.position=c(1, 0), legend.justification=c(1, 0)) ``` ] .pull-right-mod[ <img src="ch3_files/figure-html/density2d-hex-1.png" style="display: block; margin: auto;" /> ] --- class: correlation # Correlation ![:scale 6%](assets/img/section/2dDensity150.png) Density 2D .footnote[[< Contents](#2)] .pull-left-mod[ ```r ggplot(faithful) + aes(eruptions, waiting) + * geom_density2d_filled() + ggside::geom_xsidehistogram() + theme(legend.position=c(1, 0), legend.justification=c(1, 0)) ``` ] .pull-right-mod[ <img src="ch3_files/figure-html/density2d-1.png" style="display: block; margin: auto;" /> ] --- class: correlation # Correlation ![:scale 6%](assets/img/section/2dDensity150.png) Density 2D .footnote[[< Contents](#2)] .pull-left-mod[ ```r ggplot(faithfuld) + * aes(eruptions, waiting, fill=density) + * geom_raster() + scale_fill_viridis_c() + theme(legend.position=c(1, 0), legend.justification=c(1, 0)) ``` ] .pull-right-mod[ <img src="ch3_files/figure-html/density2d-raster-1.png" style="display: block; margin: auto;" /> ] --- class: correlation # Correlation ![:scale 6%](assets/img/section/2dDensity150.png) Density 2D .footnote[[< Contents](#2)] .pull-left-mod[ ```r ggplot(faithfuld) + aes(eruptions, waiting, fill=density) + * geom_raster(interpolate=TRUE) + scale_fill_viridis_c() + theme(legend.position=c(1, 0), legend.justification=c(1, 0)) ``` ] .pull-right-mod[ <img src="ch3_files/figure-html/density2d-raster-interpolate-1.png" style="display: block; margin: auto;" /> ] --- class: correlation # Correlation ![:scale 6%](assets/img/section/Heatmap150.png) Heatmap .footnote[[< Contents](#2)] .pull-left-mod[ ```r mtcars |> tibble::rownames_to_column("model") |> gather("key", "value", -model) |> ggplot() + aes(key, model, fill=value) + * geom_tile() + scale_fill_viridis_c( * trans="pseudo_log") + labs(x=NULL, y=NULL) ``` ] .pull-right-mod[ <img src="ch3_files/figure-html/heatmap-1.png" style="display: block; margin: auto;" /> ] --- class: correlation # Correlation ![:scale 6%](assets/img/section/Correlogram150.png) Correlogram .footnote[[< Contents](#2)] .pull-left-mod[ ```r mtcars |> cor(mtcars) |> * ggcorrplot::ggcorrplot() + theme(legend.position="top") ``` ] .pull-right-mod[ <img src="ch3_files/figure-html/correlation-1.png" style="display: block; margin: auto;" /> ] --- class: correlation # Correlation ![:scale 6%](assets/img/section/Correlogram150.png) Correlogram .footnote[[< Contents](#2)] .pull-left-mod[ ```r mtcars |> cor(mtcars) |> ggcorrplot::ggcorrplot( * hc.order=TRUE, * outline.color="white") + theme(legend.position="top") ``` ] .pull-right-mod[ <img src="ch3_files/figure-html/correlation-order-1.png" style="display: block; margin: auto;" /> ] --- class: correlation # Correlation ![:scale 6%](assets/img/section/Correlogram150.png) Correlogram .footnote[[< Contents](#2)] .pull-left-mod[ ```r mtcars |> cor(mtcars) |> ggcorrplot::ggcorrplot( hc.order=TRUE, * method="circle") + theme(legend.position="top") ``` ] .pull-right-mod[ <img src="ch3_files/figure-html/correlation-circle-1.png" style="display: block; margin: auto;" /> ] --- class: correlation # Correlation ![:scale 6%](assets/img/section/Correlogram150.png) Correlogram .footnote[[< Contents](#2)] .pull-left-mod[ ```r GGally::ggpairs(iris) ``` ] .pull-right-mod[ <img src="ch3_files/figure-html/correlogram-1.png" style="display: block; margin: auto;" /> ] --- class: correlation # Correlation ![:scale 6%](assets/img/section/Correlogram150.png) Correlogram .footnote[[< Contents](#2)] .pull-left-mod[ ```r GGally::ggpairs( iris, * aes(color=Species) ) ``` ] .pull-right-mod[ <img src="ch3_files/figure-html/correlogram-color-1.png" style="display: block; margin: auto;" /> ]