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.


Learning Objectives

Create interactive graphics using and design a data dashboard using flexdashboard.

Video Lecture


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.


## 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 and filter to include a subset of the data. In part, this makes sure that the resulting dataset and plots are computationally feasible – for large datasets, you may need to downsample.


nyc_airbnb = 
  nyc_airbnb |> 
  mutate(rating = review_scores_location / 2) |>
    neighbourhood_group, neighbourhood, rating, price, room_type, lat, long) |>
    neighbourhood_group == "Manhattan",
    room_type == "Entire home/apt",
    price %in% 100:500)

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)) |> 
    x = ~lat, y = ~long, type = "scatter", mode = "markers",
    color = ~price, text = ~text_label, alpha = 0.5)

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. 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.

nyc_airbnb |> 
  mutate(neighbourhood = fct_reorder(neighbourhood, price)) |> 
  plot_ly(y = ~price, color = ~neighbourhood, type = "box", colors = "viridis")

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", colors = "viridis")

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.


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) +