This document is adapted from the Maps section of the Altair Example Gallery.

Our first step is to set up our environment:

Choropleth Map

Altair example

It is not straightforward to preview the topological data, or the unemployment data:

  • The vega_data$us_10m data is in topoJSON, not a data frame
  • The vega_data$unemployment data is in an unparsed format.

Data

counties <- alt$topo_feature(vega_data$us_10m$url, "counties")
unemp_data <- vega_data$unemployment$url

Chart

chart <- 
  alt$Chart(counties)$
  mark_geoshape()$
  encode(
    color = "rate:Q"
  )$
  transform_lookup(
    lookup = "id",
    from_ = alt$LookupData(unemp_data, "id", list("rate"))
  )$
  project(type = "albersUsa")$
  properties(width = 500, height = 300)

chart

Locations of US Airports

Altair example

Data

us <- vega_data$us_10m$url
airports <- vega_data$airports()
glimpse(airports)
#> Rows: 3,376
#> Columns: 7
#> $ iata      <chr> "00M", "00R", "00V", "01G", "01J", "01M", "02A", "02C", "02G…
#> $ name      <chr> "Thigpen", "Livingston Municipal", "Meadow Lake", "Perry-War…
#> $ city      <chr> "Bay Springs", "Livingston", "Colorado Springs", "Perry", "H…
#> $ state     <chr> "MS", "TX", "CO", "NY", "FL", "MS", "AL", "WI", "OH", "MO", …
#> $ country   <chr> "USA", "USA", "USA", "USA", "USA", "USA", "USA", "USA", "USA…
#> $ latitude  <dbl> 31.95376, 30.68586, 38.94575, 42.74135, 30.68801, 34.49167, …
#> $ longitude <dbl> -89.23450, -95.01793, -104.56989, -78.05208, -81.90594, -88.…

Chart

states <- alt$topo_feature(us, feature = "states")

# US states background
background <-
  alt$Chart(states)$
  mark_geoshape(
    fill = "lightgray",
    stroke = "white"
  )$
  properties(width = 500, height = 300)$
  project("albersUsa")

# airport positions on background
points <- 
  alt$Chart(airports)$
  transform_aggregate(
    latitude = "mean(latitude)",
    longitude = "mean(longitude)",
    count = "count()",
    groupby = list("state")
  )$
  mark_circle()$
  encode(
    longitude = "longitude:Q",
    latitude = "latitude:Q",
    size = alt$Size('count:Q', title = "Number of Airports"),
    color = alt$value("steelblue"),
    tooltip = list("state:N","count:Q")
  )$
  properties(title = 'Number of airports in US')

chart <- (background + points)

chart

Repeated Choropleth Map

Altair example

Data

glimpse(vega_data$population_engineers_hurricanes())
#> Rows: 52
#> Columns: 5
#> $ state      <chr> "Alabama", "Alaska", "Arizona", "Arkansas", "California", "…
#> $ id         <dbl> 1, 2, 4, 5, 6, 8, 9, 10, 11, 12, 13, 15, 16, 17, 18, 19, 20…
#> $ population <dbl> 4863300, 741894, 6931071, 2988248, 39250017, 5540545, 35764…
#> $ engineers  <dbl> 0.003421545, 0.001590524, 0.004774154, 0.002439557, 0.00712…
#> $ hurricanes <dbl> 22, 0, 0, 0, 0, 0, 10, 2, 0, 110, 20, 0, 0, 0, 0, 0, 0, 0, …

Chart

states <- alt$topo_feature(vega_data$us_10m$url, "states")
source <- vega_data$population_engineers_hurricanes$url
variable_list <- list("population", "engineers", "hurricanes")

chart <- 
  alt$Chart(states)$
  mark_geoshape()$
  encode(
    color = alt$Color(alt$`repeat`("row"), type = "quantitative")
  )$
  transform_lookup(
    lookup = "id",
    from_ = alt$LookupData(source, "id", variable_list)
  )$
  properties(width = 500, height = 300)$
  project(type = "albersUsa")$
  `repeat`(row = variable_list)$
  resolve_scale(color = "independent")

chart

World Projections

Altair example

Please see the Vega documentation for more details on the projections available.

countries <- alt$topo_feature(vega_data$world_110m$url, "countries")

base <-  
  alt$Chart(countries)$
  mark_geoshape(fill = "#666666",stroke = "white")$
  properties(width = 300, height = 180)

projections <- list("equirectangular", "mercator", "orthographic", "gnomonic")

# use purrr to fashion a facet_wrap
charts <- map(projections, ~base$project(.x)$properties(title = .x))

chart <- (charts[[1]] | charts[[2]]) & (charts[[3]] | charts[[4]])

chart