+ - 0:00:00
Notes for current slide
Notes for next slide

Interactive graphics

1 / 20

When interactive?

There are three main categories of tasks1 that interactive graphics can be useful for:

  1. Exploratory data analysis
  2. Understanding models / algorithms
  3. When searching for information quickly without fully specified questions

[1] Sievert, Carson. Interactive web-based data visualization with R, plotly, and shiny. https://plotly-r.com/introduction.html

2 / 20

How interactive?

Enter plotly!

  • The plotly package builds on what you already know using ggplot
  • All you need to do [mostly] is wrap your ggplot calls in a ggplotly() function.

3 / 20

ggplot + plotly

library(ggplot2)
ggplot(mpg, aes(displ, hwy)) +
geom_point()

4 / 20

ggplot + plotly

How could we save this plot as an R object to print later?

ggplot(mpg, aes(displ, hwy)) +
geom_point()

5 / 20

ggplot + plotly

How could we save this plot as an R object to print later?

p <- ggplot(mpg, aes(displ, hwy)) +
geom_point()
p

6 / 20

ggplot + plotly

library(plotly)
p <- ggplot(mpg, aes(displ, hwy)) +
geom_point()
ggplotly(p)
234567203040
displhwy
7 / 20

ggplot + plotly

This works for (almost) any ggplot!!

library(plotly)
p <- ggplot(mpg, aes(displ, hwy)) +
geom_point()
ggplotly(p)
234567203040
displhwy
8 / 20

Interactive Vis

  • Create a ggplot examining the relationship between height and mass using the starwars dataset
  • Make this plot interactive using the ggplotly() function
9 / 20

More plotly magic

  • Often when doing exploratory data analysis, it can be helpful to examine data points more closely
  • the highlight_key() and highlight() functions in plotly can allow you to select certain points
  • We can then use the crosstalk and DT packages to look more closely at these points
10 / 20

More plotly magic

Note: the :: syntax means that I am calling a function from a specific package, for example crosstalk::bscols means I am calling the bscols function from the crosstalk package.

m <- highlight_key(mpg)
p <- ggplot(m, aes(displ, hwy)) + geom_point()
gg <- highlight(ggplotly(p), "plotly_selected")
crosstalk::bscols(gg, DT::datatable(m))
11 / 20

More plotly magic

m <- highlight_key(mpg)
p <- ggplot(m, aes(displ, hwy)) + geom_point()
gg <- highlight(ggplotly(p), "plotly_selected")
crosstalk::bscols(gg, DT::datatable(m))
234567203040
displhwy
12 / 20

Interactive Vis

  • Create an interactive plot of height by mass using the starwars data
  • Add the ability to highlight specific points on this plot
  • Using crosstalk::bscols() and DT::datatable() link this plot to a data table
  • Knit your .Rmd file and select the "outlier" to see details about this data point
13 / 20

Interactive maps

  • Another fun package in R is the leaflet package - this allows you to create interactive maps
14 / 20

leaflet

  • To get started, call the leaflet() function. Then addTiles()
  • By default, leaflet uses OpenStreetMap map tiles
library(leaflet)
leaflet() %>%
addTiles()
15 / 20

leaflet

  • To get started, call the leaflet() function. Then addTiles()
  • You can add markers using addMarkers() with latitude and longitude specified
library(leaflet)
leaflet() %>%
addTiles() %>%
addMarkers(lng = -80.2765, lat = 36.1334, popup = "Dr. D'Agostino McGowan's classroom")
16 / 20

leaflet

  • You can pass a data frame to leaflet as well
  • Dataset of seismic events off Fiji since 1964
data(quakes)
quakes %>%
slice(1:20) %>%
leaflet() %>%
addTiles() %>%
addMarkers(~long, ~lat,
popup = ~as.character(mag),
label = ~as.character(mag))
17 / 20

leaflet

What is different in the addMarkers() function?

data(quakes)
quakes %>%
slice(1:20) %>%
leaflet() %>%
addTiles() %>%
addMarkers(~long, ~lat,
popup = ~as.character(mag),
label = ~as.character(mag))
18 / 20

leaflet

data(quakes)
quakes %>%
slice(1:20) %>%
mutate(color = ifelse(mag > 5, "red", "yellow")) %>%
leaflet() %>%
addTiles() %>%
addCircleMarkers(~long, ~lat,
color = ~color,
label = ~as.character(mag))
19 / 20

Interactive Vis

  • Load the leaflet package
  • Load the starbucks.csv data
  • Subset the starbucks data to only locations in North Carolina
  • Use the leaflet package to plot the starbucks locations
  • Label the locations by the store_name variable
20 / 20

When interactive?

There are three main categories of tasks1 that interactive graphics can be useful for:

  1. Exploratory data analysis
  2. Understanding models / algorithms
  3. When searching for information quickly without fully specified questions

[1] Sievert, Carson. Interactive web-based data visualization with R, plotly, and shiny. https://plotly-r.com/introduction.html

2 / 20
Paused

Help

Keyboard shortcuts

, , Pg Up, k Go to previous slide
, , Pg Dn, Space, j Go to next slide
Home Go to first slide
End Go to last slide
Number + Return Go to specific slide
b / m / f Toggle blackout / mirrored / fullscreen mode
c Clone slideshow
p Toggle presenter mode
t Restart the presentation timer
?, h Toggle this help
Esc Back to slideshow