Interactive Plots with plotly
plotly turns R charts into interactive, web-based graphics — hover for tooltips, drag to zoom, click the legend to toggle series — all from familiar R syntax.
Learn Interactive Plots with plotly in our free R course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick…
Part of the free R course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
By the end of this lesson you'll build figures with plot_ly(), convert ggplot2 plots with ggplotly(), layer traces, and shape the figure with layout().
What You'll Learn in This Lesson
1️⃣ Building a Chart with plot_ly()
plot_ly() creates an interactive figure. Map columns to axes with formula notation — x = ~wt — and choose a type and mode .
2️⃣ Converting ggplot2 with ggplotly()
Already built a ggplot? ggplotly() wraps it into an interactive chart in one call, adding hover tooltips, a clickable legend, and zoom while keeping your aesthetics.
3️⃣ Traces and layout()
A figure is made of traces — individual data series. add_trace() layers more series in, and layout() sets the title, axis labels, and legend.
Your turn. Fill in the # TODO blank, run it, and compare with the expected output.
Build a ggplot bar chart of mean mpg by cylinder, then convert it with ggplotly(). The one-line conversion is the fastest path from a static figure to an explorable one.
📋 Quick Reference — plotly
Practice quiz
Which function creates an interactive plot directly in plotly?
- plot_ly()
- ggplot()
- interactive()
- draw_ly()
Answer: plot_ly(). plot_ly() is plotly's primary function for building interactive graphics in R.
What does ggplotly() do?
- Adds a regression line
- Deletes a ggplot
- Converts an existing ggplot2 plot into an interactive plotly chart
- Saves a plot to PNG
Answer: Converts an existing ggplot2 plot into an interactive plotly chart. ggplotly() takes a ggplot2 object and makes it interactive with hover and zoom.
What is the main advantage of plotly over static ggplot2?
- It uses less code
- It adds interactivity like hover, zoom, and pan
- It needs no data
- It is always faster to render
Answer: It adds interactivity like hover, zoom, and pan. plotly's defining feature is interactivity: hovering, zooming, panning, and tooltips in the browser.
In plot_ly(), what does a 'trace' represent?
- The plot title
- A data import step
- An error message
- A single layer or data series in the figure
Answer: A single layer or data series in the figure. A trace is one data series or layer; figures can combine several traces with add_trace().
How do you map data columns to axes in plot_ly()?
- With formula arguments like x = ~col and y = ~col
- By renaming the data frame
- With the aes() function only
- It is automatic and cannot be set
Answer: With formula arguments like x = ~col and y = ~col. plot_ly() uses formula notation such as x = ~wt, y = ~mpg to map columns to aesthetics.
What appears when a user hovers over a point in a plotly chart?
- The plot is deleted
- A tooltip showing the underlying values
- A new window opens
- Nothing
Answer: A tooltip showing the underlying values. Hovering reveals a tooltip with the data values for that point by default.
Which function customizes titles and axes in plotly?
- theme()
- labs()
- title()
- layout()
Answer: layout(). layout() controls the figure's title, axes, legend, and overall appearance in plotly.
Which add_* function overlays an extra series on a plot_ly figure?
- add_series()
- add_plot()
- add_trace()
- add_layer()
Answer: add_trace(). add_trace() (and helpers like add_lines/add_markers) layer additional data onto a figure.
When is static ggplot2 often preferable to plotly?
- Never
- For print, PDFs, and publications where interactivity is not useful
- Only for 3D charts
- Only for time series
Answer: For print, PDFs, and publications where interactivity is not useful. Static ggplot2 is ideal for print and PDF output where hover and zoom add nothing.
What type of output does a plotly chart render as in a browser?
- An HTML/JavaScript widget
- A CSV file
- A LaTeX document
- A plain PNG image
Answer: An HTML/JavaScript widget. plotly renders an interactive HTML widget powered by JavaScript (htmlwidgets).