R
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Data Structures
-
Section 4: Data Manipulation
-
Section 5: Visualization and Statistics
-
Section 6: Advanced R
-
Section 7: Practical Projects
-
Section 8: Interview Practice
-
Section 9: More Practice Exercises
-
Section 10: Shiny Apps in Depth
-
Section 11: More Data Wrangling
-
Section 12: Tidyverse Deep Dive
-
Section 13: Statistical Modeling Deep Dive
-
Section 14: Machine Learning in R
-
Section 15: R Visualization Deep Dive
-
Section 16: R Package Development Deep Dive
-
Section 17: R for Reproducible Research
-
Section 18: R and Databases
-
Section 19: R Performance Optimization
-
Section 20: Bioinformatics and Specialized R
-
Section 21: More Shiny Practice
-
Section 22: More Practice Exercises
-
Section 23: R Data Structures Deep Dive
-
Section 24: More Interview and Review
-
Section 25: R for Business Analytics
-
Section 26: R Text Mining and NLP
-
Section 27: R Spatial Data Analysis
-
Section 28: R Deep Learning
-
Section 29: Advanced Statistical Techniques
-
Section 30: R Object Systems Deep Dive
-
Section 31: R Environments and Metaprogramming
-
Section 32: R for Finance
-
Section 33: R for Clinical and Health Data
-
Section 34: More Shiny Advanced Practice
-
Section 35: R Data Cleaning Deep Dive
-
Section 36: R Reporting Automation
-
Section 37: More Practical Projects Round 2
-
Section 38: R Ecosystem and Career
-
Section 39: More Visualization Practice
-
Section 40: R for Experimentation
-
Section 41: R for Genomics and Bioinformatics Deep Dive
-
Section 42: R for Marketing Analytics
-
Section 43: R Data Import/Export Deep Dive
-
Section 44: R String Processing Deep Dive
-
Section 45: R for Actuarial and Insurance Analytics
-
Section 46: R Testing and Quality Assurance Deep Dive
-
Section 47: R Version Control and Collaboration
-
Section 48: R Advanced Functional Programming
-
Section 49: R for Supply Chain and Operations
-
Section 50: More Practice Exercises Round 3
-
Section 51: R Dashboards and BI Integration
-
Section 52: R Data Governance and Ethics
-
Section 53: More Modeling Practice
-
Section 54: R Final Capstone Projects
-
Section 55: R for Sports Analytics
-
Section 56: More Interview Practice Round 2
-
Section 57: R Networking and APIs
-
Section 58: R for Environmental Science
-
Section 59: R Notebook and Documentation Practices
-
Section 60: More Data Wrangling Mastery
-
Section 61: R for A/B Testing at Scale
-
Section 62: R Package Ecosystem Deep Dive
65: Interactive Visualizations with plotly
A few years ago, I was presenting a complex scatter plot of server latency across fifty different regions to my CTO. I had spent hours perfecting the ggplot2 aesthetic—the colors were right, the facets were clean, and the labels were crisp. But then he pointed to a single, lonely outlier floating far above the rest of the data and asked, "Which specific cluster is that, and why is it spiking?"
I spent the next three minutes scrolling through a dataframe in the console, filtering by coordinates, and trying to find the ID of that one point while the meeting went silent. It was an embarrassing realization: static plots are great for papers, but for exploration and presentations, they're often a bottleneck. That's exactly why I started using plotly. It turns your data into something you can actually touch.
Bridging the Gap with ggplotly
If you've already mastered ggplot2, you don't actually have to learn a whole new plotting grammar to get started. The plotly package provides a wrapper called ggplotly() that takes a static ggplot object and renders it as an interactive HTML widget. It's almost like magic—it preserves your scales and colors but adds zooming, panning, and hover-tooltips automatically.
library(ggplot2)
library(plotly)
# Create a standard ggplot
p <- ggplot(mtcars, aes(x = wt, y = mpg, color = factor(cyl))) +
geom_point() +
labs(title = "Fuel Efficiency vs Weight")
# Turn it interactive
ggplotly(p)
I usually use this approach when I'm in the "exploratory" phase of a project. I can build my plot quickly using the ggplot syntax I know by heart, and then wrap it in ggplotly() to hunt for those outliers that I might have missed in a flat image.
Building Native Interactive Plots
While the wrapper is convenient, building plots directly with plot_ly() gives you much finer control over the interactivity. One thing that trips people up is the syntax: plotly uses the tilde ~ before column names. This is because plotly isn't just looking for a vector; it's looking for a mapping to the data frame.
When you use the native API, you can specify the "mode" of the plot—whether you want markers, lines, or both—which allows you to create highly customized dashboards. For example, look at how we can create a bubble chart where the size of the point is tied to a third variable:
plot_ly(data = mtcars,
x = ~wt,
y = ~mpg,
size = ~hp,
color = ~factor(cyl),
type = 'scatter',
mode = 'markers',
text = ~paste("Car:", rownames(mtcars))) %>%
layout(title = 'Engine Performance Explorer',
xaxis = list(title = 'Weight'),
yaxis = list(title = 'Miles Per Gallon'))
Refining the Hover Experience
The real power of an interactive plot isn't just the zoom; it's the information you provide when the user hovers over a point. By default, plotly shows the X and Y coordinates, but that's rarely enough. Using the text argument in plot_ly() or the text aesthetic in ggplot2 allows you to pass a custom string—often created with paste0()—to give the user a full story about that specific data point.
I've found that the most effective plots are the ones where the user doesn't have to ask "What is this point?" because the hover label already told them. Whether it's a timestamp, a client ID, or a specific error message, that context is what transforms a chart into a tool.
📋 Practical Task
Build a Global Temperature Anomaly Explorer
Your goal is to create an interactive time-series visualization that allows a user to explore temperature changes over time. You will use the built-in AirPassengers dataset (which we'll treat as a proxy for temperature anomalies for this exercise) to practice native plotly implementation.
Requirements:
- Convert the
AirPassengerstime series object into a data frame with two columns:Month(as a date or factor) andPassengers. - Use
plot_ly()to create a line chart (mode = 'lines+markers'). - Customize the hover text so that it displays "Value: [Number]" and "Date: [Month]" instead of the default coordinates.
- Add a custom layout title: "Monthly Passenger Volume Trends".
- Ensure the X-axis and Y-axis have descriptive labels.
Success Criteria: When you hover over a point on the line, a custom tooltip should appear, and you should be able to zoom into specific years using your mouse.
There are no comments for now.