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: part # Part of a Whole .footnote[[< Contents](#2)] [![:scale 15%](assets/img/section/GroupedRed150.png)](https://r-graph-gallery.com/stacked-barplot.html) [![:scale 15%](assets/img/section/Tree150.png)](https://r-graph-gallery.com/treemap.html) [![:scale 15%](assets/img/section/Doughnut150.png)](https://r-graph-gallery.com/doughnut-plot.html) [![:scale 15%](assets/img/section/Pie150.png)](https://r-graph-gallery.com/pie-plot.html) [![:scale 15%](assets/img/section/Dendrogram150.png)](https://r-graph-gallery.com/dendrogram.html) [![:scale 15%](assets/img/section/CircularPacking150.png)](https://r-graph-gallery.com/circle-packing.html) .bottom[Grouped and Stacked barplot] .bottom[Treemap] .bottom[Doughnut] .bottom[Pie chart] .bottom[Dendrogram] .bottom[Circular packing] <br> .black.font120[ - Visualization of **proportions** - Some charts are also able to convey **hierarchies** - Some are rarely appropriate ] --- class: part # Part of a Whole ![:scale 6%](assets/img/section/GroupedRed150.png) Stacked barplot .footnote[[< Contents](#2)] .pull-left-mod[ ```r mpg |> count(drv, class, name="count") |> ggplot() + aes(count, reorder(class, count, sum)) + * geom_col(aes(fill=drv)) + labs(y=NULL) + theme(legend.position="top") ``` ] .pull-right-mod[ <img src="ch3_files/figure-html/sbarplot-1.png" style="display: block; margin: auto;" /> ] --- class: part # Part of a Whole ![:scale 6%](assets/img/section/GroupedRed150.png) Stacked barplot .footnote[[< Contents](#2)] .pull-left-mod[ ```r mpg |> count(drv, class, name="count") |> * group_by(class) |> * mutate(prop = count / sum(count)) |> ggplot() + * aes(prop, reorder(class, count, sum)) + geom_col(aes(fill=drv)) + labs(y=NULL) + theme(legend.position="top") ``` ] .pull-right-mod[ <img src="ch3_files/figure-html/sbarplot-proportion-1.png" style="display: block; margin: auto;" /> ] --- class: part # Part of a Whole ![:scale 6%](assets/img/section/GroupedRed150.png) Stacked barplot .footnote[[< Contents](#2)] .pull-left-mod[ ```r mpg |> count(drv, class, name="count") |> group_by(class) |> mutate(prop = count / sum(count)) |> ggplot() + aes(prop, reorder(class, count, sum)) + geom_col(aes(fill=drv)) + scale_x_continuous( * label=scales::percent) + labs(y=NULL) + theme(legend.position="top") ``` ] .pull-right-mod[ <img src="ch3_files/figure-html/sbarplot-percent-1.png" style="display: block; margin: auto;" /> ] --- class: part # Part of a Whole ![:scale 6%](assets/img/section/Pie150.png) Pie chart .footnote[[< Contents](#2)] .pull-left-mod[ ```r data.frame( category=c("A", "B", "C"), prop=c(0.1, 0.6, 0.3)) |> ggplot() + * aes(1, prop) + * geom_col(aes(fill=category)) ``` ] .pull-right-mod[ <img src="ch3_files/figure-html/pie-pre-1.png" style="display: block; margin: auto;" /> ] --- class: part # Part of a Whole ![:scale 6%](assets/img/section/Pie150.png) Pie chart .footnote[[< Contents](#2)] .pull-left-mod[ ```r data.frame( category=c("A", "B", "C"), prop=c(0.1, 0.6, 0.3)) |> ggplot() + aes(1, prop) + geom_col(aes(fill=category)) + * coord_polar(theta="y") + * theme_void(base_size=16) ``` ] .pull-right-mod[ <img src="ch3_files/figure-html/pie-1.png" style="display: block; margin: auto;" /> ] --- class: part # Part of a Whole ![:scale 6%](assets/img/section/Doughnut150.png) Doughnut .footnote[[< Contents](#2)] .pull-left-mod[ ```r data.frame( category=c("A", "B", "C"), prop=c(0.1, 0.6, 0.3)) |> ggplot() + aes(1, prop) + geom_col(aes(fill=category)) + * xlim(c(-0.5, 1.5)) ``` ] .pull-right-mod[ <img src="ch3_files/figure-html/doughnut-pre-1.png" style="display: block; margin: auto;" /> ] --- class: part # Part of a Whole ![:scale 6%](assets/img/section/Doughnut150.png) Doughnut .footnote[[< Contents](#2)] .pull-left-mod[ ```r data.frame( category=c("A", "B", "C"), prop=c(0.1, 0.6, 0.3)) |> ggplot() + aes(1, prop) + geom_col(aes(fill=category)) + xlim(c(-0.5, 1.5)) + * coord_polar(theta="y") + * theme_void(base_size=16) ``` ] .pull-right-mod[ <img src="ch3_files/figure-html/doughnut-1.png" style="display: block; margin: auto;" /> ] --- class: part # Part of a Whole ![:scale 6%](assets/img/section/CircularPacking150.png) Circular packing .footnote[[< Contents](#2)] .pull-left-mod[ ```r *library(packcircles) df <- mpg |> count(class, name="count") *df <- cbind(df, circleProgressiveLayout( df$count, sizetype="area")) ggplot(df) + * aes(x0=x, y0=y, r=radius) + * ggforce::geom_circle() ``` ] .pull-right-mod[ <img src="ch3_files/figure-html/cpacking-1.png" style="display: block; margin: auto;" /> ] --- class: part # Part of a Whole ![:scale 6%](assets/img/section/CircularPacking150.png) Circular packing .footnote[[< Contents](#2)] .pull-left-mod[ ```r library(packcircles) df <- mpg |> count(class, name="count") df <- cbind(df, circleProgressiveLayout( df$count, sizetype="area")) ggplot(df) + aes(x0=x, y0=y, r=radius) + ggforce::geom_circle(aes( * fill=class), color=NA) + * coord_fixed() + geom_text(aes(x, y, label=class)) + theme_void(base_size=16) + theme(legend.position="none") ``` ] .pull-right-mod[ <img src="ch3_files/figure-html/cpacking-fixed-1.png" style="display: block; margin: auto;" /> ] --- class: part # Part of a Whole ![:scale 6%](assets/img/section/CircularPacking150.png) Circular packing .footnote[[< Contents](#2)] .pull-left-mod[ ```r library(packcircles) df <- data.frame( x = c(0, 1, 0, 1), y = c(0, 0, 1, 1), radius = c(0.7, 0.5, 0.6, 1.2)) ggplot(df) + aes(x0=x, y0=y, r=radius) + * ggforce::geom_circle() + geom_point(aes(x, y), color="red") + coord_fixed() + theme_void(base_size=16) + theme(legend.position="none") ``` ] .pull-right-mod[ <img src="ch3_files/figure-html/cpacking-overlap-1.png" style="display: block; margin: auto;" /> ] --- class: part # Part of a Whole ![:scale 6%](assets/img/section/CircularPacking150.png) Circular packing .footnote[[< Contents](#2)] .pull-left-mod[ ```r library(packcircles) df <- data.frame( x = c(0, 1, 0, 1), y = c(0, 0, 1, 1), radius = c(0.7, 0.5, 0.6, 1.2)) *df.new <- circleRepelLayout( df, sizetype="radius")$layout *ggplot(df.new) + aes(x0=x, y0=y, r=radius) + ggforce::geom_circle() + geom_point(aes(x, y), color="red") + * geom_point(aes(x, y), df) + coord_fixed() + theme_void(base_size=16) + theme(legend.position="none") ``` ] .pull-right-mod[ <img src="ch3_files/figure-html/cpacking-repel-1.png" style="display: block; margin: auto;" /> ] --- class: part # Part of a Whole ![:scale 6%](assets/img/section/CircularPacking150.png) Circular packing .footnote[[< Contents](#2)] .pull-left-mod[ ```r edges <- ggraph::flare$edges vertices <- ggraph::flare$vertices graph <- igraph::graph_from_data_frame( edges, vertices=vertices) ggraph::ggraph(graph, * layout="circlepack", weight=size) + * ggraph::geom_node_circle() + theme_void(base_size=16) ``` ] .pull-right-mod[ <img src="ch3_files/figure-html/cpacking-hierarchical-1.png" style="display: block; margin: auto;" /> ] --- class: part # Part of a Whole ![:scale 6%](assets/img/section/CircularPacking150.png) Circular packing .footnote[[< Contents](#2)] .pull-left-mod[ ```r edges <- ggraph::flare$edges vertices <- ggraph::flare$vertices graph <- igraph::graph_from_data_frame( edges, vertices=vertices) ggraph::ggraph(graph, layout="circlepack", weight=size) + ggraph::geom_node_circle( * aes(fill=depth)) + scale_fill_viridis_c() + * coord_fixed() + theme_void(base_size=16) ``` ] .pull-right-mod[ <img src="ch3_files/figure-html/cpacking-hierarchical-fill-1.png" style="display: block; margin: auto;" /> ] --- class: part # Part of a Whole ![:scale 6%](assets/img/section/Tree150.png) Treemap .footnote[[< Contents](#2)] .pull-left-mod[ ```r edges <- ggraph::flare$edges vertices <- ggraph::flare$vertices graph <- igraph::graph_from_data_frame( edges, vertices=vertices) ggraph::ggraph(graph, * layout="treemap", weight=size) + * ggraph::geom_node_tile( aes(fill=depth)) + scale_fill_viridis_c() + theme_void(base_size=16) ``` ] .pull-right-mod[ <img src="ch3_files/figure-html/treemap-1.png" style="display: block; margin: auto;" /> ] --- class: part # Part of a Whole ![:scale 6%](assets/img/section/Dendrogram150.png) Dendrogram .footnote[[< Contents](#2)] .pull-left-mod[ ```r edges <- ggraph::flare$edges vertices <- ggraph::flare$vertices graph <- igraph::graph_from_data_frame( edges, vertices=vertices) ggraph::ggraph(graph, * layout="tree") + * ggraph::geom_edge_diagonal() + theme_void(base_size=16) ``` ] .pull-right-mod[ <img src="ch3_files/figure-html/tree-1.png" style="display: block; margin: auto;" /> ] --- class: part # Part of a Whole ![:scale 6%](assets/img/section/Dendrogram150.png) Dendrogram .footnote[[< Contents](#2)] .pull-left-mod[ ```r edges <- ggraph::flare$edges vertices <- ggraph::flare$vertices graph <- igraph::graph_from_data_frame( edges, vertices=vertices) ggraph::ggraph(graph, * layout="dendrogram") + ggraph::geom_edge_diagonal() + theme_void(base_size=16) ``` ] .pull-right-mod[ <img src="ch3_files/figure-html/dendrogram-1.png" style="display: block; margin: auto;" /> ] --- class: part # Part of a Whole ![:scale 6%](assets/img/section/Dendrogram150.png) Dendrogram .footnote[[< Contents](#2)] .pull-left-mod[ ```r df <- hclust(dist(iris[, 1:4])) ggraph::ggraph(df, layout="dendrogram", * height=height) + * ggraph::geom_edge_elbow() + theme_void(base_size=16) ``` ] .pull-right-mod[ <img src="ch3_files/figure-html/dendrogram-elbow-1.png" style="display: block; margin: auto;" /> ] --- class: part # Part of a Whole ![:scale 6%](assets/img/section/Dendrogram150.png) Dendrogram .footnote[[< Contents](#2)] .pull-left-mod[ ```r df <- hclust(dist(iris[, 1:4])) ggraph::ggraph(df, layout="dendrogram", * circular=TRUE) + ggraph::geom_edge_elbow() + * coord_fixed() + theme_void(base_size=16) ``` ] .pull-right-mod[ <img src="ch3_files/figure-html/dendrogram-circular-1.png" style="display: block; margin: auto;" /> ]