Lesson 1 of 3

Interactive Charts & Maps

Maya's one neighbourhood bakery, the shop whose Q1 numbers you turned into a report table earlier in this track, did so well that she opened five more. She now runs six coffee-and-pastry shops across the city, and a printed bar chart no longer keeps up with her questions. Which shop is that tall bar again? What exactly did the quiet one take last month? Where are they on the map?

A static chart is a photograph: it shows the shape and then stops answering. An interactive chart is a conversation. You hover a point and it tells you which shop and the exact dollars; you drag a box around a crowded corner and it zooms in; you pan a map and the streets move under your shops. Move the Area filter on the dashboard below and watch every number and chart answer at once. That responsiveness is what this lesson builds, one chart and one map at a time.

By the end of this lesson you will be able to:

  • Say what interactivity actually adds to a chart, and when it helps versus when it hurts
  • Turn an ordinary ggplot into an interactive plotly chart with one line, ggplotly()
  • Put your data on a leaflet map: a street basemap with a marker and a popup for every row
  • Choose between an interactive and a static chart by where it has to live

Prerequisites: you can run R and load a package with library(), and you have built a ggplot (data, then aes(), then a geom) in the ggplot2 course. Every new term is defined as it appears.

Why bother

What interactivity actually buys you

A good static chart answers the big-picture question well: do busier shops make more money? Maya can read that off a scatter in a second. But the moment she asks a pointed question, a static picture goes quiet. Three things interactivity adds, each fixing a real limit of a printed chart:

  • Hover reveals the exact value and the identity behind a mark. Instead of "that dot is around forty-something thousand," she hovers and reads Old Town: 240 customers/day, $53,000. The label was always in the data; hover just surfaces it on demand instead of cluttering the page.
  • Zoom and pan let her explore density. When ten shops pile into one corner of a scatter, she drags a box around them and the chart zooms in so she can tell them apart.
  • A live map lets her drag and zoom the city itself, instead of squinting at a fixed image.

First, the data. Each lesson runs in a fresh R session, so we build Maya's six shops right here (run this once):

RInteractive R
shops <- data.frame( name = c("Riverside", "Old Town", "Market Square", "University", "Harbour", "Garden Gate"), area = c("South", "North", "South", "North", "South", "North"), lon = c(-73.985, -73.972, -73.991, -73.962, -73.951, -73.978), # longitude lat = c(40.748, 40.761, 40.722, 40.735, 40.715, 40.770), # latitude customers = c(180, 240, 95, 210, 130, 160), # average customers per day revenue = c(42000, 53000, 21000, 47000, 28000, 36000) # revenue last month, dollars ) shops #> name area lon lat customers revenue #> 1 Riverside South -73.985 40.748 180 42000 #> 2 Old Town North -73.972 40.761 240 53000 #> 3 Market Square South -73.991 40.722 95 21000 #> 4 University North -73.962 40.735 210 47000 #> 5 Harbour South -73.951 40.715 130 28000 #> 6 Garden Gate North -73.978 40.770 160 36000

  

Everything we draw, the chart and the map, comes from this one frame. Start with the plain static chart Maya already knows how to make: customers across the bottom, revenue up the side, one point per shop. It answers the big question (busier shops do take more) but it cannot tell you which dot is which:

RInteractive R
library(ggplot2) p <- ggplot(shops, aes(x = customers, y = revenue)) + geom_point(size = 3, colour = "steelblue") + labs(x = "customers per day", y = "revenue ($)") p

  

That p is our starting point. In the next step we make it talk back.