+ - 0:00:00
Notes for current slide
Notes for next slide

Data Visualization

Chapter 3. Data Visualization in R

Iñaki Úcar

Department of Statistics | uc3m-Santander Big Data Institute

Master in Computational Social Science

Licensed under Creative Commons Attribution CC BY 4.0 Last generated: 2023-01-25

1 / 21

Directory of Visualizations

Based on The R Graph Gallery

2 / 21

Part of a Whole

< Contents

Grouped and Stacked barplot Treemap Doughnut Pie chart Dendrogram Circular packing


  • Visualization of proportions
  • Some charts are also able to convey hierarchies
  • Some are rarely appropriate
3 / 21

Part of a Whole Stacked barplot

< Contents

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")

4 / 21

Part of a Whole Stacked barplot

< Contents

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")

5 / 21

Part of a Whole Stacked barplot

< Contents

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")

6 / 21

Part of a Whole Pie chart

< Contents

data.frame(
category=c("A", "B", "C"),
prop=c(0.1, 0.6, 0.3)) |>
ggplot() +
aes(1, prop) +
geom_col(aes(fill=category))

7 / 21

Part of a Whole Pie chart

< Contents

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)

8 / 21

Part of a Whole Doughnut

< Contents

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))

9 / 21

Part of a Whole Doughnut

< Contents

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)

10 / 21

Part of a Whole Circular packing

< Contents

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()

11 / 21

Part of a Whole Circular packing

< Contents

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")

12 / 21

Part of a Whole Circular packing

< Contents

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")

13 / 21

Part of a Whole Circular packing

< Contents

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")

14 / 21

Part of a Whole Circular packing

< Contents

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)

15 / 21

Part of a Whole Circular packing

< Contents

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)

16 / 21

Part of a Whole Treemap

< Contents

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)

17 / 21

Part of a Whole Dendrogram

< Contents

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)

18 / 21

Part of a Whole Dendrogram

< Contents

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)

19 / 21

Part of a Whole Dendrogram

< Contents

df <- hclust(dist(iris[, 1:4]))
ggraph::ggraph(df,
layout="dendrogram",
height=height) +
ggraph::geom_edge_elbow() +
theme_void(base_size=16)

20 / 21

Part of a Whole Dendrogram

< Contents

df <- hclust(dist(iris[, 1:4]))
ggraph::ggraph(df,
layout="dendrogram",
circular=TRUE) +
ggraph::geom_edge_elbow() +
coord_fixed() +
theme_void(base_size=16)

21 / 21

Directory of Visualizations

Based on The R Graph Gallery

2 / 21
Paused

Help

Keyboard shortcuts

, , Pg Up, k Go to previous slide
, , Pg Dn, Space, j Go to next slide
Home Go to first slide
End Go to last slide
Number + Return Go to specific slide
b / m / f Toggle blackout / mirrored / fullscreen mode
c Clone slideshow
p Toggle presenter mode
t Restart the presentation timer
?, h Toggle this help
sToggle scribble toolbox
Esc Back to slideshow