Bubble map with R

Cedric Vidonne

Lei Chen

Bubble map with R

As a variation of a bubble chart, bubble maps display bubbles over geographical regions rather than the cartesian plane. The size or area of the bubble indicates the value of the particular variable, with the position on the map indicating location. Bubble maps are useful for comparing proportions against geographic regions without the issues caused by the size of different areas, as you would have in choropleth maps.

More about: Bubble map


Bubble map

# Loading required packages
library(unhcrthemes)
library(tidyverse)
library(scales)
library(sf)

# Data URL
df_url <- "https://raw.githubusercontent.com/GDS-ODSSS/unhcr-dataviz-platform/master/data/geospatial/bubble_map.csv"
poly_url <- "https://raw.githubusercontent.com/GDS-ODSSS/unhcr-dataviz-platform/master/data/geospatial/world_polygons_simplified.json"
line_url <- "https://raw.githubusercontent.com/GDS-ODSSS/unhcr-dataviz-platform/master/data/geospatial/world_lines_simplified.json"

# Read and transform data
df <- read_csv(df_url) |> 
  st_as_sf(coords = c("lon", "lat"),
           crs = 4326)

poly <- read_sf(poly_url) |> 
  st_set_crs(4326)

line <- read_sf(line_url) |>
  mutate(
    type = as_factor(type) |>
      fct_relevel("solid", "dashed", "dotted", "dashed-dot")
  ) |> 
  st_set_crs(4326)

# Plot
ggplot() +
  geom_sf(data = poly,
          fill = unhcr_pal(n = 5, "pal_grey")[2],
          color = "transparent") +
  geom_sf(data = line,
          aes(linetype = type),
          color = "white",
          linewidth = .25,
          show.legend = FALSE) +
  geom_sf(data = df,
          aes(size = ref),
          shape = 21,
          fill = unhcr_pal(n = 1, "pal_blue"),
          color = unhcr_pal(n = 5, "pal_blue")[5],
          alpha = 0.3) +
  scale_linetype_manual(values = c(1, 2, 3, 4)) +
  scale_size_area(max_size = 12,
                  labels = scales::label_number(
                    scale_cut = cut_short_scale()
                  ),
                  breaks = c(1e5, 1e6, 5e6)) +
  labs(
    title = "Global refugee displacement by country of origin | 2021",
    caption = "The boundaries and names shown and the designations used on this map do not imply official endorsement or\nacceptance by the United Nations.\nSource: UNHCR Refugee Data Finder\n© UNHCR, The UN Refugee Agency"
  ) +
  coord_sf(crs = st_crs('ESRI:54030')) +
  theme_unhcr_map(rel_small = .75,
                  rel_tiny = .65)


Related chart with R