Towards Interactivity

HES 505 Fall 2022: Session 26

Matt Williamson

Objectives

By the end of today you should be able to:

  • Describe the motivations for building more complex graphics

  • Understand User-Centered Design

  • Build a multi-layered interactive map

Integrating Rasters

Using ggmap

  • get_map downloads ready-made image to serve as backdrop

  • Need to know projection of baselayer

  • Lots of maptypes

  • Need to experiment with zoom

bg <- ggmap::get_map(as.vector(
  st_bbox(
    st_transform(cty.info, 4326))), 
  zoom = 7)

p <- ggmap(bg) +
  geom_sf(data = st_transform(cty.info, 4326), 
          mapping = aes(fill = medincome), 
          alpha=0.7, 
          inherit.aes = FALSE) +
  geom_sf(data=st_transform(cty.info, 4326), 
          color="yellow", 
          fill=NA, 
          inherit.aes = FALSE) +
  scale_fill_continuous() +
  coord_sf(crs = st_crs(4326))

Using ggmap

Using ggmap

bg <- ggmap::get_stamenmap(bbox = as.vector(st_bbox(
  st_transform(cty.info, 4326))), 
  source = "stamen", 
  maptype = "toner",
  zoom = 7)

p <- ggmap(bg) +
  geom_sf(data = st_transform(cty.info, 4326), mapping = aes(fill = medincome), alpha=0.7, inherit.aes = FALSE) +
  geom_sf(data=st_transform(cty.info, 4326), color="yellow", fill=NA, inherit.aes = FALSE) +
  scale_fill_continuous() +
  coord_sf(crs = st_crs(4326))

Using ggmap

Using RStoolbox

  • Sometimes we want more flexibility

  • May want to label ourselves

  • May want custom backdrops

elev <- rast(geodata::elevation_30s(path=tempdir(),
                             country="USA")[[1]])
elev.crop <- project(crop(elev, 
                  project(vect(cty.info), 
                          elev)), crs(vect(cty.info)))
slp <- terrain(elev.crop, "slope", unit="radians")
asp <- terrain(elev.crop, "aspect")

hillshade <- shade(slp, asp) 

p <- ggR(hillshade) +
  geom_sf(data = cty.info, 
          mapping = aes(fill = medincome), 
          alpha=0.7, 
          inherit.aes = FALSE) +
  geom_sf(data=st_transform(cty.info, 4326), 
          color="yellow", 
          fill=NA, 
          inherit.aes = FALSE) +
  scale_fill_continuous() +
  coord_sf(crs = st_crs(4326))

Using RStoolbox

Using ggplot2

  • Have to convert to a dataframe

  • Then use geom_raster

  • Use caution!!

##convert to SpatialPixelsDataFrame for ggplot
hills.spdf <- as(hillshade, "SpatialPixelsDataFrame")
hills.df <- as.data.frame(hills.spdf)
colnames(hills.df) <- c("value", "x", "y")

globe.map <- ggplot() +
  geom_raster(data=hills.df,aes(x=x, y=y, alpha=value)) 

Towards Interactive Visualizations

3 Categories of data visualization

  • Static

  • Interactive

  • Dynamic

dynamic

Why Move Beyond Static Maps?

Dealing with complex datasets

  • Identifying structure that might otherwise be hidden

  • Diagnosing models and interpreting results

  • Aiding the sense-making process

Clarity in presentation

  • Zooming allows the user to determine scale of presentation

  • Hovering allows more information to be displayed ‘on-demand’

  • Subsetting facilitates ease of interpretation

Designing for the User

Who is your audience?

  • Your advisor and colleagues?

  • An external collaborator?

  • The general public?

    • User archetypes

Iteration

From Usability.gov

  • Feedback is critical

  • Ideation: What specifically does the user need?

  • Meaning: Are the data clearly defined and explained? Are the conclusions obvious?

  • Function: Given the usecases, will the application (visualization) actually perform?

Building interactive visualizations in R

A note about APIs

  • API: Application Programming Interface

  • A software intermediary that allows two applications to “communicate”

  • Lots of R packages rely on APIs to access data on the web (e.g.,tidycensus)

  • Facilitates reproducibility and powerful web applications built on R analyses

  • May require “keys” and addtional parsing (Mapbox and Google)

Clarity in presentation (revisited)

Using plotly

g <- txhousing %>% 
  # group by city
  group_by(city) %>%
  # initiate a plotly object with date on x and median on y
   plotly::plot_ly(x = ~date, y = ~median) %>%
  # add a line plot for all texan cities
   plotly::add_lines(name = "Texan Cities", hoverinfo = "none", 
            type = "scatter", mode = "lines", 
            line = list(color = 'rgba(192,192,192,0.4)')) %>%
  # plot separate lines for Dallas and Houston
   plotly::add_lines(name = ~city, 
            data = filter(txhousing, 
                          city %in% c("Dallas", "Houston")),
            hoverinfo = ~city,
            line = list(color = c("red", "blue")),
            color = ~city)

Interactive maps with mapview and tmap

  • Easy extension of your existing

  • Class Demo

Other options

  • leaflet

  • mapview

  • mapsapi