Plotly is a flexible framework for producing interactive graphics; it has a variety of implementations, including one for R. We’ll take a look at a few common plot types, and then introduce flexdashboards as a way to collect plots (either static or interactive).

This is the second module in the Interactivity topic; the relevant slack channel is here.

Example

For this example, I’ll create a new .Rmd file that knits to .html in the repo / R Project holding the website I made in making websites. In addition to some usual packages, I’ll load plotly.

library(tidyverse)
library(viridis)
library(p8105.datasets)

library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout

We’re going to focus on the Airbnb data for this topic. The code below extracts what we need right now; specifically, we select only a few of the variables, filter to include a subset of the data, and down-sample for computational efficiency.

set.seed(1)

data(nyc_airbnb)
nyc_airbnb = 
  nyc_airbnb %>% 
  mutate(rating = review_scores_location / 2) %>%
  select(
    neighbourhood_group, neighbourhood, rating, price, room_type, lat, long) %>%
  filter(
    !is.na(rating), 
    neighbourhood_group == "Manhattan",
    room_type == "Entire home/apt",
    price %in% 100:500)  %>% 
  sample_n(5000)

We’ll use this dataset as the basis for our plots.

Plotly scatterplot

There are several practical differences comparing ggplot and plot_ly, but the underlying conceptual framework is similar. We need to define a dataset, specify how variables map to plot elements, and pick a plot type.

Below we’re plotting the location (latitude and longitude) of the rentals in our dataset, and mapping price to color. We also define a new variable text_label and map that to text.

The type of plot is scatter, which has several “modes”: markers produces the same kind of plot as ggplot::geom_point, lines produces the same kind of plot as ggplot::geom_line.

nyc_airbnb %>%
  mutate(text_label = str_c("Price: $", price, '\nRating: ', rating)) %>% 
  plot_ly(
    x = ~lat, y = ~long, type = "scatter", mode = "markers",
    color = ~price, text = ~text_label, alpha = 0.5)
## This version of Shiny is designed to work with 'htmlwidgets' >= 1.5.
##     Please upgrade via install.packages('htmlwidgets').

This can be a useful way to show the data – it gives additional information on hovering and allows you to zoom in or out, for example.

Plotly boxplot

Next up is the boxplot. We’re going to do some pre-processing here to show only the neighborhoods with the most rentals.

After we’ve done that subsetting, the process for creating the boxplot is similar to above: define the dataset, specify the mappings, pick a plot type. Here the type is box, and there aren’t modes to choose from.

common_neighborhoods =
  nyc_airbnb %>% 
  count(neighbourhood, sort = TRUE) %>% 
  top_n(8) %>% 
  select(neighbourhood)
## Selecting by n
inner_join(nyc_airbnb, common_neighborhoods, by = "neighbourhood") %>% 
  mutate(neighbourhood = fct_reorder(neighbourhood, price)) %>% 
  plot_ly(y = ~price, color = ~neighbourhood, type = "box",
          colors = "Set2")

Again, this can be helpful – we have a five-number summary when we hover, and by clicking we can select groups we want to include or exclude.

Plotly barchart

Lastly, we’ll make a bar chart. Plotly expects data in a specific format for bar charts, so we use count to get the number of rentals in each neighborhood (i.e. to get the bar height). Otherwise, the process should seem pretty familiar …

nyc_airbnb %>% 
  count(neighbourhood) %>% 
  mutate(neighbourhood = fct_reorder(neighbourhood, n)) %>% 
  plot_ly(x = ~neighbourhood, y = ~n, color = ~neighbourhood, type = "bar")

Interactivity in bar charts is kinda neat, but needs a bit more justification – you can zoom, which helps in some cases, or you could build in some addition information in hover text.

ggplotly

You can convert a ggplot object straight to an interactive graphic using ggplotly.

For example, the code below recreates our scatterplot using ggplot followed by ggplotly.

scatter_ggplot = 
  nyc_airbnb %>%
  ggplot(aes(x = lat, y = long, color = price)) +
  geom_point(alpha = 0.25) +
  scale_color_viridis() +
  coord_cartesian() +
  theme_classic()

ggplotly(scatter_ggplot)