HES 505 Fall 2022: Session 26
Matt Williamson
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
ggmapget_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))ggmapggmapbg <- 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))ggmapRStoolboxSometimes 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))RStoolboxggplot2Have to convert to a dataframe
Then use geom_raster
Use caution!!
Static
Interactive
Dynamic
Identifying structure that might otherwise be hidden
Diagnosing models and interpreting results
Aiding the sense-making process
Zooming allows the user to determine scale of presentation
Hovering allows more information to be displayed ‘on-demand’
Subsetting facilitates ease of interpretation
Your advisor and colleagues?
An external collaborator?
The general public?
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?
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)
plotlyg <- 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)mapview and tmapEasy extension of your existing
Class Demo
leaflet
mapview
mapsapi