4  Interactive visualization

In Chapter 3, we used ggplot to create data visualizations. These are great for reports and when you already know what you want to demonstrate with your data. At an early stage of exploratory data analysis, it can however be useful to explore data interactively. A popular open-source graphing library is plotly. It can be used with Python, R, and Javascript and allows the creation of interactive dashboards. Here, we will only briefly discuss how to create interactive visualizations in R using the plotly package and embed them into R Markdown docuemnts.

4.1 plotly in R.

The plotly package is available on CRAN and can be installed as follows:

install.packages("plotly", repos = "http://cran.rstudio.com")

and loaded using:

library(plotly)

Interactivity depends of course on the output format; interactivity works well in HTML pages or in RStudio, but is not supported when the Rmarkdown is converted to PDF.

4.2 Two dimensional scatter plot using plot_ly

Here is the code to create a simple two dimensional scatterplot:

auto <- ISLR2::Auto %>%
  mutate(cylinders = as.factor(cylinders))

1plot_ly(
  data = auto,
2  x = ~horsepower,
  y = ~mpg,
  text = ~name,
3  type = "scatter",
  mode = "markers",
4  hoverinfo = "text+x+y"
)
1
The plot_ly command takes a variety of arguments to specify the data, how they are mapped onto aesthetics (x, y, text) and what type of graph should be created. We map horsepower to x, mpg to y, and the name to `text.
2
You can also specify transformatios of data here, e.g. y = ~log(mpg)
3
We specify a scatterplot where the data are represented using markers.
4
The hoverinfo argument defines that we want to display the x, y, and text values, so horsepower, mpg, and name when the mouse is hovered over a point in the scatterplot.
Figure 4.1: Interactive scatterplot of mpg versus horsepower (plot_ly version)

The resulting plot (Figure 4.1) can be zoomed and dragged. Hovering over a point displays information about the car.

4.3 Add interactivity to ggplot figure using ggplotly

If you already have a ggplot2 visualization, you can conveniently convert it into an interactive plotly graph using the ggplotly() function. For example, the following code creates a scatterplot of mpg versus horsepower from the auto data set and converts it into an interactive plot:

g <- ggplot(auto, aes(x = horsepower, y = mpg, label = name)) +
  geom_point()

ggplotly(g)
Figure 4.2: Interactive scatterplot of mpg versus horsepower (ggplotly version)

This is all that needs to be done.

4.4 Three dimensional plots using plot_ly

You can use plot_ly also to create three dimensional plots as shown in the following example.

plot_ly(
  x = auto$mpg,
  y = auto$weight,
1  z = auto$horsepower,
  color = auto$cylinders,
2  type = "scatter3d",
  mode = "markers",
  marker = list(size = 4)
)
1
3D scatterplots require mapping a feature to z
2
The marker argument allows configuring the symbol, e.g. . Here, we control the size of the markers.
Figure 4.3: Interactive 3D scatterplot of mpg versus horsepower and weight
NoteFurther information

Plotly is more than just scatterplots. There are a much wider variety of interactive plots possible. Check out the documentation at https://plotly.com/r/ to learn more about plotly’s interface to R.

Code

The code of this chapter is summarized here.

Show the code
knitr::opts_chunk$set(echo = TRUE, cache = TRUE, autodep = TRUE,
  fig.align = "center")
install.packages("plotly", repos = "http://cran.rstudio.com")
library(plotly)
auto <- ISLR2::Auto %>%
  mutate(cylinders = as.factor(cylinders))

plot_ly(
  data = auto,
  x = ~horsepower,
  y = ~mpg,
  text = ~name,
  type = "scatter",
  mode = "markers",
  hoverinfo = "text+x+y"
)
g <- ggplot(auto, aes(x = horsepower, y = mpg, label = name)) +
  geom_point()

ggplotly(g)
plot_ly(
  x = auto$mpg,
  y = auto$weight,
  z = auto$horsepower,
  color = auto$cylinders,
  type = "scatter3d",
  mode = "markers",
  marker = list(size = 4)
)