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: distribution # Distribution .footnote[[< Contents](#2)] [![:scale 15%](assets/img/section/Violin150.png)](https://r-graph-gallery.com/violin.html) [![:scale 15%](assets/img/section/Density150.png)](https://r-graph-gallery.com/density-plot.html) [![:scale 15%](assets/img/section/Histogram150.png)](https://r-graph-gallery.com/histogram.html) [![:scale 15%](assets/img/section/Box1150.png)](https://r-graph-gallery.com/boxplot.html) [![:scale 15%](assets/img/section/Joyplot150.png)](https://r-graph-gallery.com/ridgeline-plot.html) .bottom[Violin] .bottom[Density] .bottom[Histogram] .bottom[Boxplot] .bottom[Ridgeline] <br> .black.font120[ - Visualization of one or multiple **univariate distributions** - Stacked versions are difficult to interpret and should be avoided - Some require fine-tuning of the parameters to avoid being misleading ] --- class: distribution # Distribution ![:scale 6%](assets/img/section/Histogram150.png) Histogram .footnote[[< Contents](#2)] .pull-left-mod[ ```r ggplot(mpg) + aes(hwy) + * geom_histogram() ``` ] .pull-right-mod[ <img src="ch3_files/figure-html/histogram-1.png" style="display: block; margin: auto;" /> ] --- class: distribution # Distribution ![:scale 6%](assets/img/section/Histogram150.png) Histogram .footnote[[< Contents](#2)] .pull-left-mod[ ```r ggplot(mpg) + * aes(hwy, after_stat(density)) + geom_histogram() ``` ] .pull-right-mod[ <img src="ch3_files/figure-html/histogram-density-1.png" style="display: block; margin: auto;" /> ] --- class: distribution # Distribution ![:scale 6%](assets/img/section/Histogram150.png) Histogram .footnote[[< Contents](#2)] .pull-left-mod[ ```r ggplot(mpg) + * aes(hwy, fill=class) + geom_histogram() + theme(legend.position=c(1, 1), legend.justification=c(1, 1)) ``` ] .pull-right-mod[ <img src="ch3_files/figure-html/histogram-fill-1.png" style="display: block; margin: auto;" /> ] --- class: distribution # Distribution ![:scale 6%](assets/img/section/Histogram150.png) Histogram .footnote[[< Contents](#2)] .pull-left-mod[ ```r ggplot(mpg) + aes(hwy) + geom_histogram() + * facet_grid(class ~ .) ``` ] .pull-right-mod[ <img src="ch3_files/figure-html/histogram-facet-1.png" style="display: block; margin: auto;" /> ] --- class: distribution # Distribution ![:scale 6%](assets/img/section/Histogram150.png) Histogram .footnote[[< Contents](#2)] .pull-left-mod[ ```r ggplot(mpg) + aes(hwy) + geom_histogram() + facet_grid( * reorder(class, -hwy, median) ~ .) ``` ] .pull-right-mod[ <img src="ch3_files/figure-html/histogram-reorder-1.png" style="display: block; margin: auto;" /> ] --- class: distribution # Distribution ![:scale 6%](assets/img/section/Density150.png) Density .footnote[[< Contents](#2)] .pull-left-mod[ ```r ggplot(mpg) + aes(hwy) + * geom_density() ``` ] .pull-right-mod[ <img src="ch3_files/figure-html/density-1.png" style="display: block; margin: auto;" /> ] --- class: distribution # Distribution ![:scale 6%](assets/img/section/Density150.png) Density .footnote[[< Contents](#2)] .pull-left-mod[ ```r ggplot(mpg) + * aes(hwy, after_stat(density)) + * geom_histogram(fill="gray") + geom_density() ``` ] .pull-right-mod[ <img src="ch3_files/figure-html/density-histogram-1.png" style="display: block; margin: auto;" /> ] --- class: distribution # Distribution ![:scale 6%](assets/img/section/Density150.png) Density .footnote[[< Contents](#2)] .pull-left-mod[ ```r ggplot(mpg) + aes(hwy, after_stat(density)) + geom_histogram(fill="gray") + * geom_density(adjust=0.2) ``` ] .pull-right-mod[ <img src="ch3_files/figure-html/density-adjust-1.png" style="display: block; margin: auto;" /> ] --- class: distribution # Distribution ![:scale 6%](assets/img/section/Density150.png) Density .footnote[[< Contents](#2)] .pull-left-mod[ ```r ggplot(mpg) + aes(hwy, after_stat(density)) + geom_histogram(fill=NA) + * geom_histogram(fill="gray", bins=10) + geom_density() ``` ] .pull-right-mod[ <img src="ch3_files/figure-html/density-bins-1.png" style="display: block; margin: auto;" /> ] --- class: distribution # Distribution ![:scale 6%](assets/img/section/Box1150.png) Boxplot .footnote[[< Contents](#2)] .pull-left-mod[ ```r ggplot(mpg) + aes(hwy, class) + * geom_boxplot() + labs(y=NULL) ``` ] .pull-right-mod[ <img src="ch3_files/figure-html/boxplot-1.png" style="display: block; margin: auto;" /> ] --- class: distribution # Distribution ![:scale 6%](assets/img/section/Box1150.png) Boxplot .footnote[[< Contents](#2)] .pull-left-mod[ ```r ggplot(mpg) + * aes(hwy, reorder(class, hwy, median)) + geom_boxplot() + labs(y=NULL) ``` ] .pull-right-mod[ <img src="ch3_files/figure-html/boxplot-reorder-1.png" style="display: block; margin: auto;" /> ] --- class: distribution # Distribution ![:scale 6%](assets/img/section/Box1150.png) Boxplot .footnote[[< Contents](#2)] .pull-left-mod[ ```r ggplot(mpg) + aes(hwy, reorder(class, hwy, median)) + * geom_boxplot(outlier.color="red") + labs(y=NULL) ``` ] .pull-right-mod[ <img src="ch3_files/figure-html/boxplot-outlier-1.png" style="display: block; margin: auto;" /> ] --- class: distribution # Distribution ![:scale 6%](assets/img/section/Box1150.png) Boxplot .footnote[[< Contents](#2)] .pull-left-mod[ ```r ggplot(mpg) + aes(hwy, reorder(class, hwy, median)) + geom_boxplot(outlier.color="red") + * geom_jitter(height=0.2, alpha=0.5) + labs(y=NULL) ``` ] .pull-right-mod[ <img src="ch3_files/figure-html/boxplot-jitter-1.png" style="display: block; margin: auto;" /> ] --- class: distribution # Distribution ![:scale 6%](assets/img/section/Box1150.png) Boxplot .footnote[[< Contents](#2)] .pull-left-mod[ ```r ggplot(mpg) + aes(hwy, reorder(class, hwy, median)) + geom_boxplot(outlier.color="red", * varwidth=TRUE) + geom_jitter(height=0.2, alpha=0.5) + labs(y=NULL) ``` ] .pull-right-mod[ <img src="ch3_files/figure-html/boxplot-varwidth-1.png" style="display: block; margin: auto;" /> ] --- class: distribution # Distribution ![:scale 6%](assets/img/section/Box1150.png) Boxplot .footnote[[< Contents](#2)] .pull-left-mod[ ```r ggplot(mpg) + aes(hwy, reorder(class, hwy, median)) + * geom_boxplot(aes(color=drv)) + labs(y=NULL) + theme(legend.position=c(1, 0), legend.justification=c(1, 0)) ``` ] .pull-right-mod[ <img src="ch3_files/figure-html/boxplot-color-1.png" style="display: block; margin: auto;" /> ] --- class: distribution # Distribution ![:scale 6%](assets/img/section/Box1150.png) Boxplot .footnote[[< Contents](#2)] .pull-left-mod[ ```r ggplot(mpg) + aes(hwy, reorder(class, hwy, median)) + * geom_boxplot(aes(fill=drv)) + labs(y=NULL) + theme(legend.position=c(1, 0), legend.justification=c(1, 0)) ``` ] .pull-right-mod[ <img src="ch3_files/figure-html/boxplot-fill-1.png" style="display: block; margin: auto;" /> ] --- class: distribution # Distribution ![:scale 6%](assets/img/section/Box1150.png) Boxplot .footnote[[< Contents](#2)] .pull-left-mod[ ```r ggplot(mpg) + aes(hwy, reorder(class, hwy, median)) + geom_boxplot(aes(fill=drv), * varwidth=TRUE) + labs(y=NULL) + theme(legend.position=c(1, 0), legend.justification=c(1, 0)) ``` ] .pull-right-mod[ <img src="ch3_files/figure-html/boxplot-fill-varwidth-1.png" style="display: block; margin: auto;" /> ] --- class: distribution # Distribution ![:scale 6%](assets/img/section/Violin150.png) Violin .footnote[[< Contents](#2)] .pull-left-mod[ ```r ggplot(mpg) + aes(hwy, reorder(class, hwy, median)) + * geom_violin() + labs(y=NULL) ``` ] .pull-right-mod[ <img src="ch3_files/figure-html/violin-1.png" style="display: block; margin: auto;" /> ] --- class: distribution # Distribution ![:scale 6%](assets/img/section/Violin150.png) Violin .footnote[[< Contents](#2)] .pull-left-mod[ ```r ggplot(mpg) + aes(hwy, reorder(class, hwy, median)) + * geom_violin(aes(fill=class)) + labs(y=NULL) + theme(legend.position="none") ``` ] .pull-right-mod[ <img src="ch3_files/figure-html/violin-fill-1.png" style="display: block; margin: auto;" /> ] --- class: distribution # Distribution ![:scale 6%](assets/img/section/Violin150.png) Violin .footnote[[< Contents](#2)] .pull-left-mod[ ```r ggplot(mpg) + aes(hwy, reorder(class, hwy, median)) + geom_violin(aes(fill=class)) + * scale_fill_viridis_d() + labs(y=NULL) + theme(legend.position="none") ``` ] .pull-right-mod[ <img src="ch3_files/figure-html/violin-viridis-1.png" style="display: block; margin: auto;" /> ] --- class: distribution # Distribution ![:scale 6%](assets/img/section/Violin150.png) Violin .footnote[[< Contents](#2)] .pull-left-mod[ ```r mpg |> mutate( * class=reorder(class, hwy, median)) |> ggplot() + * aes(hwy, class) + geom_violin(aes(fill=class)) + scale_fill_viridis_d() + labs(y=NULL) + theme(legend.position="none") ``` ] .pull-right-mod[ <img src="ch3_files/figure-html/violin-reorder-1.png" style="display: block; margin: auto;" /> ] --- class: distribution # Distribution ![:scale 6%](assets/img/section/Joyplot150.png) Ridgeline .footnote[[< Contents](#2)] .pull-left-mod[ ```r mpg |> mutate( class=reorder(class, hwy, median)) |> ggplot() + aes(hwy, class, fill=class) + * ggridges::geom_density_ridges() + scale_fill_viridis_d() + labs(y=NULL) + theme(legend.position="none") ``` ] .pull-right-mod[ <img src="ch3_files/figure-html/ridgeline-1.png" style="display: block; margin: auto;" /> ] --- class: distribution # Distribution ![:scale 6%](assets/img/section/Joyplot150.png) Ridgeline .footnote[[< Contents](#2)] .pull-left-mod[ ```r mpg |> mutate( class=reorder(class, hwy, median)) |> ggplot() + * aes(hwy, class, fill=after_stat(x)) + * ggridges::geom_density_ridges_gradient() + * scale_fill_viridis_c() + labs(y=NULL) + theme(legend.position="none") ``` ] .pull-right-mod[ <img src="ch3_files/figure-html/ridgeline-gradient-1.png" style="display: block; margin: auto;" /> ] --- class: distribution # Distribution ![:scale 6%](assets/img/section/Joyplot150.png) Ridgeline .footnote[[< Contents](#2)] .pull-left-mod[ ```r mpg |> mutate( class=reorder(class, hwy, median)) |> ggplot() + aes(hwy, class, fill=after_stat(x)) + ggridges::geom_density_ridges_gradient( * scale=1) + scale_fill_viridis_c() + labs(y=NULL) + theme(legend.position="none") ``` ] .pull-right-mod[ <img src="ch3_files/figure-html/ridgeline-scale-1.png" style="display: block; margin: auto;" /> ] --- class: distribution # Distribution ![:scale 6%](assets/img/section/Joyplot150.png) Ridgeline .footnote[[< Contents](#2)] .pull-left-mod[ ```r mpg |> mutate( class=reorder(class, hwy, median)) |> ggplot() + aes(hwy, class, fill=after_stat(x)) + ggridges::geom_density_ridges_gradient( * scale=1, quantile_lines=TRUE) + scale_fill_viridis_c() + labs(y=NULL) + theme(legend.position="none") ``` ] .pull-right-mod[ <img src="ch3_files/figure-html/ridgeline-quantile-1.png" style="display: block; margin: auto;" /> ] --- class: distribution # Distribution ![:scale 6%](assets/img/section/Joyplot150.png) Ridgeline .footnote[[< Contents](#2)] .pull-left-mod[ ```r mpg |> mutate( class=reorder(class, hwy, median)) |> ggplot() + * aes(hwy, class, fill=0.5 - abs( * 0.5 - after_stat(ecdf))) + ggridges::geom_density_ridges_gradient( * scale=1, calc_ecdf=TRUE) + scale_fill_viridis_c("Tail prob.") + labs(y=NULL) + theme(legend.position=c(1, 0), legend.justification=c(1, 0)) ``` ] .pull-right-mod[ <img src="ch3_files/figure-html/ridgeline-tailprob-1.png" style="display: block; margin: auto;" /> ]