plotly is a package for building interactive visualizations in R. It is especially useful when you want users to hover, zoom, pan, filter visually, and explore the data on their own.
plot_ly(data =..., x =..., y =..., type =..., mode =...)
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
plot_ly( data =mtcars, x =~wt, y =~mpg, type ="scatter", mode ="markers")
6.3 Why Use plotly?
NoteKey Advantage
The main reason to use plotly is interactivity.
Users can: - hover for details - zoom into regions - pan across the figure - toggle traces on and off - explore patterns without changing the code
6.4 Encode Additional Information
plot_ly( data =mtcars, x =~wt, y =~mpg, color =~factor(cyl), type ="scatter", mode ="markers")
6.5 Mapping vs Fixed Values
NoteKey Rule
Map variables with ~variable. Set constants directly.
plot_ly( data =mtcars, x =~wt, y =~mpg, type ="scatter", mode ="markers", marker =list(color ="blue"))
6.6 Add Rich Hover Information
plot_ly( data =mtcars, x =~wt, y =~mpg, type ="scatter", mode ="markers", text =~paste("Model:", rownames(mtcars),"<br>MPG:", mpg,"<br>Weight:", wt,"<br>Cylinders:", cyl), hoverinfo ="text")
Hover text is one of the most useful features in plotly. It lets you keep the figure clean while still giving viewers access to more detail.
6.7 Add Multiple Traces
plot_ly(data =mtcars, x =~wt, y =~mpg, type ="scatter", mode ="markers")%>%add_lines(y =~fitted(lm(mpg~wt, data =mtcars)), name ="Trend line")
Interactive plots are often built by adding traces instead of layering geoms.
6.8 Color Best Practices
TipUsing Color Effectively
Use color to distinguish meaningful groups
Avoid using too many categories at once
Keep contrast strong enough for readability
Use accessible palettes when possible
plot_ly( data =mtcars, x =~wt, y =~mpg, color =~factor(cyl), colors ="Set1", type ="scatter", mode ="markers")
plot_ly( data =mtcars, x =~wt, y =~mpg, color =~mpg, colors ="viridis", type ="scatter", mode ="markers")
6.9 Improve Labels and Layout
plot_ly( data =mtcars, x =~wt, y =~mpg, type ="scatter", mode ="markers")%>%layout( title ="Fuel Efficiency vs Weight", xaxis =list(title ="Weight"), yaxis =list(title ="Miles per Gallon"))
6.10 Styling and Theming
In plotly, overall appearance is usually controlled through layout() rather than a theme system like ggplot2.
plot_ly( data =mtcars, x =~wt, y =~mpg, type ="scatter", mode ="markers")%>%layout( title ="Fuel Efficiency vs Weight", paper_bgcolor ="white", plot_bgcolor ="white", xaxis =list(showgrid =FALSE), yaxis =list(showgrid =FALSE))
6.10.1 Custom Marker Styling
plot_ly( data =mtcars, x =~wt, y =~mpg, type ="scatter", mode ="markers", marker =list( size =10, opacity =0.7))
6.11 Compare Groups
plot_ly( data =mtcars, x =~wt, y =~mpg, color =~factor(cyl), type ="scatter", mode ="markers")
A common interactive alternative to faceting is to let users isolate groups by clicking the legend.
6.12 Common Chart Types
# Scatterplotplot_ly(mtcars, x =~wt, y =~mpg, type ="scatter", mode ="markers")
# Line chartplot_ly(economics, x =~date, y =~unemploy, type ="scatter", mode ="lines")
# Bar chartplot_ly(mtcars, x =~factor(cyl), type ="histogram")
# Histogramplot_ly(mtcars, x =~mpg, type ="histogram")
# Boxplotplot_ly(mtcars, x =~factor(cyl), y =~mpg, type ="box")
6.13 A Brief Note on ggplotly()
plotly can convert a ggplot2 graphic into an interactive figure with ggplotly().