+ - 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 / 25

Directory of Visualizations

Based on The R Graph Gallery

2 / 25

Correlation

< Contents

Scatter Heatmap Correlogram Bubble Connected scatter Density 2D


  • Visualization of the relationship between two variables
  • Two continuous, or two discrete, or mixed
  • Options to include a third one
3 / 25

Correlation Scatter

< Contents

gapminder::gapminder |>
filter(year == 1997) |>
ggplot() +
aes(gdpPercap, lifeExp) +
scale_x_log10() +
geom_point()

4 / 25

Correlation Scatter

< Contents

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

5 / 25

Correlation Bubble

< Contents

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

6 / 25

Correlation Bubble

< Contents

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

7 / 25

Correlation Bubble

< Contents

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

8 / 25

Correlation Bubble

< Contents

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

9 / 25

Correlation Connected Scatter

< Contents

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

10 / 25

Correlation Connected Scatter

< Contents

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

11 / 25

Correlation Connected Scatter

< Contents

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

12 / 25

Correlation Scatter

< Contents

ggplot(faithful) +
aes(eruptions, waiting) +
geom_point()

13 / 25

Correlation Scatter

< Contents

ggplot(faithful) +
aes(eruptions, waiting) +
geom_point() +
ggside::geom_xsidehistogram()

14 / 25

Correlation Density 2D

< Contents

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

15 / 25

Correlation Density 2D

< Contents

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

16 / 25

Correlation Density 2D

< Contents

ggplot(faithful) +
aes(eruptions, waiting) +
geom_density2d_filled() +
ggside::geom_xsidehistogram() +
theme(legend.position=c(1, 0),
legend.justification=c(1, 0))

17 / 25

Correlation Density 2D

< Contents

ggplot(faithfuld) +
aes(eruptions, waiting, fill=density) +
geom_raster() +
scale_fill_viridis_c() +
theme(legend.position=c(1, 0),
legend.justification=c(1, 0))

18 / 25

Correlation Density 2D

< Contents

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

19 / 25

Correlation Heatmap

< Contents

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)

20 / 25

Correlation Correlogram

< Contents

mtcars |>
cor(mtcars) |>
ggcorrplot::ggcorrplot() +
theme(legend.position="top")

21 / 25

Correlation Correlogram

< Contents

mtcars |>
cor(mtcars) |>
ggcorrplot::ggcorrplot(
hc.order=TRUE,
outline.color="white") +
theme(legend.position="top")

22 / 25

Correlation Correlogram

< Contents

mtcars |>
cor(mtcars) |>
ggcorrplot::ggcorrplot(
hc.order=TRUE,
method="circle") +
theme(legend.position="top")

23 / 25

Correlation Correlogram

< Contents

GGally::ggpairs(iris)

24 / 25

Correlation Correlogram

< Contents

GGally::ggpairs(
iris,
aes(color=Species)
)

25 / 25

Directory of Visualizations

Based on The R Graph Gallery

2 / 25
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