Interactive Graphics with Plotly

Some interactive graphics packages

  • crosstalk: that’s what shiny is based on - we will look into shiny later
  • plotly: interactive javascript graphics (maintained by Carson Sievert)
  • leaflet: (RStudio) allows to make interactive maps. Has been picking up users and has developed a stable user base.
  • ggvis: both static and interactive graphics, work on it has stalled … (Wickham)
  • animint2: interactive, linked graphics with ggplot2 syntax (Toby Hocking)
  • rCharts, rbokeh, gridSVG, epivizr, cranvas previous approaches to packages with interactive graphics
  • see also https://r-graph-gallery.com/interactive-charts.html for additional packages (more specific)

plotly

The plotly package in R has two interfaces:

  • plot specification via plotly
  • translating ggplot2 plots and adding interactive elements

Plotly creates interactive plots with javascript.

plot_ly(data = penguins_std, 
        x = ~fl, 
        y = ~bl, 
        color = ~species, 
        size = 3, 
        width=420, height=300)

plotly

ggplot2 and plotly

gg <- ggplot(data=penguins_std, aes(x = fl, 
                                    y = bl, 
                                    colour = species)) +  
  geom_point(alpha=0.5) + 
  geom_smooth(method = "lm", se=F)
ggplotly(gg, width=600, height=490)

Scatterplot(ly) matrix

library(GGally)
p <- ggpairs(penguins_std, columns=2:5, 
             mapping = ggplot2::aes(color = species, 
                                    alpha = 0.8))
ggplotly(p, width=600, height=490)

Maps

data(canada.cities, package = "maps")
viz <- ggplot(canada.cities, aes(long, lat)) +
  borders(regions = "canada") +
  coord_equal() +
  geom_point(aes(text = name, size = log2(pop)), 
             colour = "red", alpha = 1/4) +
  theme_map()
ggplotly(viz)

Not all ggplot2 geoms are supported in plotly, but when they are, they just work out of the box

Modifying plotly

plotly uses elements of crosstalk to provide additional interactivity, such as linked highlighting

txh_shared <- highlight_key(txhousing, ~year)

p <- ggplot(txh_shared, aes(month, median)) +
   geom_line(aes(group = year)) + 
   geom_smooth(data = txhousing, method = "gam") + 
   scale_x_continuous("", breaks=seq(1, 12, 1),
        labels=c("J", "F", "M", "A", "M", "J", 
                 "J", "A", "S", "O", "N", "D")) +
   scale_y_continuous("Median price ('00,000)", 
                      breaks = seq(0,300000,100000),
                      labels = seq(0,3,1)) +
   facet_wrap(~ city)

gg <- ggplotly(p, height = 750, width = 900) %>%
   plotly::layout(title = "Click on a line to highlight a year")

The power of crosstalk

library(tsibble)     # install.packages("tsibble")
library(tsibbletalk) # install.packages("tsibbletalk")
library(feasts)      # install.packages("feasts")
tourism_shared <- tourism |>
  as_shared_tsibble(spec = (State / Region) * Purpose)

tourism_feat <- tourism_shared |>
  features(Trips, feat_stl)

p1 <- tourism_shared |>
  ggplot(aes(x = Quarter, y = Trips)) +
  geom_line(aes(group = Region), alpha = 0.5) +
  facet_wrap(~ Purpose, scales = "free_y") + theme_helvetica

p2 <- tourism_feat |>
  ggplot(aes(x = trend_strength, y = seasonal_strength_year)) +
  geom_point(aes(group = Region)) +
  xlab("trend") + ylab("season") + theme_helvetica

The shared data objects from crosstalk make linking between plots easier!

Code
subplot(
    ggplotly(p1, tooltip = "Region", width = 1200, height = 600) |>
  config(displayModeBar = FALSE),
    ggplotly(p2, tooltip = "Region", width = 1000, height = 500) |>
  config(displayModeBar = FALSE),
    nrows = 1, widths=c(0.5, 0.5), heights=1) |>
  highlight(dynamic = FALSE)

Your Turn

  • get the code from the previous plot to run in your RStudio session
  • the function highlight allows to make modifications in how highlighted values are presented in plotly. Read through the parameter details in ?highlight.
  • the parameter dynamic is set to FALSE by default. Turn it to TRUE. What is the effect?

Resources