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

Directory of Visualizations

Based on The R Graph Gallery

2 / 14

Map

< Contents

Map Choropleth Hexbin map Cartogram Connection Bubble map


  • Visualizaton of geospatial data
  • Usually, the map is just a reference
  • Sometimes, it encodes the information
3 / 14

Map Map

< Contents | Many resources and examples at r-spatial.org

world <- giscoR::gisco_get_countries()
ggplot(world) +
geom_sf()

4 / 14

Map Map

< Contents

usa <- sf::st_as_sf(maps::map(
"state", plot=FALSE, fill=TRUE))
ggplot(usa) +
geom_sf()

5 / 14

Map Map

< Contents

africa <- giscoR::gisco_get_countries(
region="Africa")
ggplot(africa) +
geom_sf()

6 / 14

Map Map

< Contents | See Coordinate Reference Systems

africa <- giscoR::gisco_get_countries(
region="Africa")
ggplot(africa) +
geom_sf() +
coord_sf(xlim=c(-20, 50),
ylim=c(-40, 40))

7 / 14

Map Choropleth

< Contents

africa <- giscoR::gisco_get_countries(
region="Africa") |>
left_join(
africamonitor::am_data(
series="SP_POP_TOTL",
from=2021, to=2021),
by=c("ISO3_CODE"="ISO3"))
ggplot(africa) +
geom_sf(aes(fill=SP_POP_TOTL)) +
coord_sf(xlim=c(-20, 50),
ylim=c(-40, 40)) +
scale_fill_viridis_c() +
labs(fill="Population") +
theme(legend.position=c(0, 0),
legend.justification=c(0, 0))

8 / 14

Map Cartogram

< Contents

africa <- giscoR::gisco_get_countries(
region="Africa") |>
left_join(
africamonitor::am_data(
series="SP_POP_TOTL",
from=2021, to=2021),
by=c("ISO3_CODE"="ISO3")) |>
sf::st_transform(3857) |>
cartogram::cartogram_cont(
"SP_POP_TOTL", itermax=5) |>
sf::st_transform(4326)
ggplot(africa) +
geom_sf(aes(fill=SP_POP_TOTL)) +
coord_sf(xlim=c(-20, 50),
ylim=c(-40, 40)) +
scale_fill_viridis_c() +
labs(fill="Population") +
theme(legend.position=c(0, 0),
legend.justification=c(0, 0))

9 / 14

Map Bubble map

< Contents

africa <- giscoR::gisco_get_countries(
region="Africa") |>
left_join(
africamonitor::am_data(
series="SP_POP_TOTL",
from=2021, to=2021),
by=c("ISO3_CODE"="ISO3"))
centroids <- africa |>
sf::st_centroid()
ggplot(centroids) +
geom_sf(data=africa) +
geom_sf(aes(size=SP_POP_TOTL)) +
coord_sf(xlim=c(-20, 50),
ylim=c(-40, 40)) +
labs(size="Population") +
theme(legend.position=c(0, 0),
legend.justification=c(0, 0))

10 / 14

Map Connection

< Contents

africa <- giscoR::gisco_get_countries(
region="Africa") |>
left_join(
africamonitor::am_data(
series="SP_POP_TOTL",
from=2021, to=2021),
by=c("ISO3_CODE"="ISO3"))
centroids <- africa |>
sf::st_centroid()
lines <- centroids |>
sf::st_union() |>
sf::st_cast("LINESTRING")
ggplot(lines) +
geom_sf(data=africa) +
geom_sf(aes(size=SP_POP_TOTL),
data=centroids) +
geom_sf() +
coord_sf(xlim=c(-20, 50),
ylim=c(-40, 40)) +
labs(size="Population") +
theme(legend.position=c(0, 0),
legend.justification=c(0, 0))

11 / 14

Map Hexbin map

< Contents

esp <- mapSpain::esp_get_country()
hexccaa <- mapSpain::esp_get_hex_ccaa()
ggplot(hexccaa) +
geom_sf(data=esp) +
geom_sf(aes(fill=codauto),
alpha=0.3,
show.legend=FALSE) +
geom_sf_text(
aes(label=label),
check_overlap=TRUE) +
theme_void(base_size=16)

12 / 14

Map Hexbin map

< Contents

esp <- mapSpain::esp_get_country()
hexprov <- mapSpain::esp_get_hex_prov()
ggplot(hexprov) +
geom_sf(data=esp) +
geom_sf(aes(fill=codauto),
alpha=0.3,
show.legend=FALSE) +
geom_sf_text(
aes(label=label),
check_overlap=TRUE) +
theme_void(base_size=16)

13 / 14

Map Hexbin map

< Contents

esp <- mapSpain::esp_get_country()
gridprov <- mapSpain::esp_get_grid_prov()
ggplot(gridprov) +
geom_sf(data=esp) +
geom_sf(aes(fill=codauto),
alpha=0.3,
show.legend=FALSE) +
geom_sf_text(
aes(label=label),
check_overlap=TRUE) +
theme_void(base_size=16)

14 / 14

Directory of Visualizations

Based on The R Graph Gallery

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