class: center, middle, inverse, title-slide .title[ # Data Analysis and Visualization ] .subtitle[ ## Chapter 3. Exploratory Data Analysis in R ] .author[ ### Iñaki Úcar ] .institute[ ### Department of Statistics | uc3m-Santander Big Data Institute ] .institute[ ### Bachelor in Data and Business Analytics ] .date[ ###
Licensed under Creative Commons Attribution
CC BY 4.0
Last generated: 2025-09-06
] --- class: base24 # Catalog of Graphs and Applications .footnote[Based on [The R Graph Gallery](https://r-graph-gallery.com/)] .pull-left[ - .distribution[[Distribution](ch3_1.html#3)].icons[      ] - .correlation[[Correlation](ch3_2.html#3)].icons[       ] - .ranking[[Ranking](ch3_3.html#3)].icons[       ] - .part[[Part of a Whole](ch3_4.html#3)].icons[       ] ] .pull-right[ - .evolution[[Evolution](ch3_5.html#3)].icons[      ] - .map[[Map](ch3_6.html#3)].icons[       ] - .flow[[Flow](ch3_7.html#3)].icons[      ] - .other[[Other resources](ch3_8.html#3)].icons[     ] ] --- class: part # Part of a Whole .footnote[[< Contents](#2)] [](https://r-graph-gallery.com/stacked-barplot.html) [](https://r-graph-gallery.com/treemap.html) [](https://r-graph-gallery.com/doughnut-plot.html) [](https://r-graph-gallery.com/pie-plot.html) [](https://r-graph-gallery.com/dendrogram.html) [](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  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  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  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  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  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  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  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  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  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  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  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  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  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  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  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  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  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  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;" /> ]