install.packages("plotly", repos = "http://cran.rstudio.com")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:
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:
- 1
-
The
plot_lycommand 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 maphorsepowertox,mpgtoy, and thenameto `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
hoverinfoargument defines that we want to display thex,y, andtextvalues, sohorsepower,mpg, andnamewhen the mouse is hovered over a point in the scatterplot.
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)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.
- 1
-
3D scatterplots require mapping a feature to
z - 2
-
The
markerargument allows configuring the symbol, e.g. . Here, we control the size of the markers.
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)
)