Spatial Data Interaction

Some strategies for interactively visualizing spatial data.

Kris Sankaran (UW Madison)
03-12-2021

Reading, Recording, Rmarkdown

  1. These are optional notes. The reading does not cover interactivity in spatial data contexts, and there will be no problems in this class related to interactive spatial data visualization. However, these notes may be useful if your project involves interaction with spatial data.

  2. In vega-lite, vector data can be visualized using vl.markGeoshape. Any fields contained within the vector dataset can be visually encoded in the marks. This is analogous to geom_sf in ggplot2.

vl.markGeoshape({fill: "#e0e0e0", stroke: "#f7f7f7", strokeWidth: 1})
    .data(vector_data) // contains the vector data boundaries
    .project(vl.projection('albersUsa'))
    .render()
robservable("@krisrs1128/minimal-geoshape", 3, height = 300)
  1. Spatial data are often amenable to dynamic linking. For example, selections in a complementary figure can be used to query points on a map. Here are two examples from public observable notebooks. The first, by Chanwut Kittivorawong, updates the cities displayed on a map using a brush applied to histogram data. Conceptually, it is like the NYC house prices homework problem, except it includes several histogram selectors and draws a background map for context.
robservable("@chanwutk/happiness-of-each-country", 6, height = 750)

The second, by Alissa Chen, shows how the amount of daylight varies across cities, depending on time of the year. Brushing over the time series filters the data that are used to determine the circle widths (the little suns) on top of the map.

robservable("@alchan/whats-the-average-amount-of-sunlight-in-a-city-over-a-short-te", 3, height = 730)
  1. Alternatively, selections on the map can highlight elements in a complementary figure. For example, hovering over geographic elements might highlight series in a time series plot or update a barchart placed on the side. Vega-lite doesn’t support selections based on vector data elements, only points that are overlaid on the map. However, these sorts of selections can be made with d3.
robservable("@krisrs1128/geojson-mouseover", 7)
  1. Sometimes it’s useful to add an interactive map background to a vector dataset. In this case, the leaflet package in R can be useful. In the code block below, addTiles fetches the background map. addCircles overlays the new vector features on top of the map. It’s worth noting that the vector features were created automatically – there was no need to create or read in any type of sf object.
cities <- read_csv("https://uwmadison.box.com/shared/static/j98anvdoasfb1h651qxzrow2ua45oap1.csv")
leaflet(cities) %>% 
  addTiles() %>% 
  addCircles(
    lng = ~Long, 
    lat = ~Lat, 
    radius = ~sqrt(Pop) * 30
  )