Learn R Programming

d3po (version 1.0.3)

po_geomap: Geomap

Description

Plot a geomap using sf spatial objects

Usage

po_geomap(d3po, ..., data = NULL, inherit_daes = TRUE, limits = NULL)

Value

an 'htmlwidgets' object with the desired interactive plot

Arguments

d3po

Either the output of d3po() or d3po_proxy().

...

Aesthetics, see daes().

data

Any dataset to use for plot, overrides data passed to d3po().

inherit_daes

Whether to inherit aesthetics previous specified.

limits

A numeric vector of length 2 specifying the minimum and maximum values for the color scale.

Examples

Run this code
if (interactive()) {
  world <- d3po::national

  # Fix geometries that cross the antimeridian (date line) to avoid horizontal lines
  # This affects Russia, Fiji, and other countries spanning the 180° meridian
  world$geometry <- sf::st_wrap_dateline(world$geometry, options = c("WRAPDATELINE=YES"))

  total_trade <- d3po::trade[
    d3po::trade$year == 2023L,
    c("reporter", "reporter_continent", "trade")
  ]
  total_trade <- aggregate(trade ~ reporter, data = total_trade, FUN = sum)
  colnames(total_trade) <- c("country", "trade")

  world <- merge(
    world,
    total_trade,
    by = "country",
    all.x = TRUE,
    all.y = FALSE
  )

  # my_pal <- tintin::tintin_pal(option = "The Calculus Affair")(7)
  # [1] "#16A2D2" "#3C8C96" "#658C4F" "#77C08A" "#7A826A" "#A3C059" "#A78239"

  my_pal <- c("#16A2D2", "#3C8C96", "#658C4F", "#77C08A", "#7A826A", "#A3C059", "#A78239")

  names(my_pal) <- c(
    "Africa", "Antarctica", "Asia",
    "Europe", "North America", "Oceania", "South America"
  )

  d3po(world, width = 800, height = 600) %>%
    po_geomap(daes(group = country, size = trade, color = my_pal, tooltip = country)) %>%
    po_labels(title = "Trade Volume by Country in 2023")
}

Run the code above in your browser using DataLab