Instant Interactivity with plotly
The previous modules taught you how to build virtually any static chart with R and ggplot2. But what if your audience could hover over data points, zoom into regions, or explore the data on their own?
This lesson introduces plotly, a library that turns your existing ggplot2 charts into interactive visualizations with just one line of code.
๐ฑ๏ธ Interactivity: Why?
This course has taught you a lot about ggplot2, the most powerful library for creating graphs with R. The possibilities are limitless: you can create literally anything with it.
Anything, yes. But only static outputs.
Even if that covers 95% of a data analyst's needs, it can sometimes lead to frustrating situations. Let's take the following chart for instance:
The result is pretty insightful already. But isn't it frustrating not to know which countries are behind the outliers? The circles that sit away from the main trend?
That's when interactivity comes in. Allowing to zoom in, add hover effects, tooltips, and cross-chart interactions is not always necessary โ but it can remove a lot of frustration when exploring data.
Don't add interactivity just because you can. A bar chart with a tooltip that shows the value of each bar is totally useless โ the value is already encoded by the bar height and can be read from the axis.
Interactivity shines when it reveals hidden information: identifying individual points in a dense scatter plot, exploring a long time series, or filtering across linked views. If hovering doesn't tell the reader something new, it's just noise.
โจ The ggplotly() Magic
This lesson brings some good news. Thanks to the plotly package, it literally takes one line of code to transform the scatterplot above into an interactive version.
Just store the static version in a variable (often simply called p), wrap it in a ggplotly() call, and you get an interactive version of the chart:
plotly sometimes ingnores your ggplot styling โ notice how the legend is floating on the top-right now?Check the result below and make sure you noticed all the features: you can hover to see values, zoom by dragging a selection, pan to navigate, autoscale to reset, filter groups in the legend, and even download as PNG from the toolbar.
๐งผ No More Tooltip Clutter
By default, ggplotly() shows every mapped aesthetic in the tooltip. For our gapminder chart, that means you'll see the raw GDP value, life expectancy, population number, and continent โ all dumped in a generic format.
It works โ but it's messy.
You can customize the tooltip content. The trick is to use the text aesthetic in your aes() call to craft exactly the tooltip you want, then tell ggplotly() to only show that:
\n in the paste0() call is a newline character. Without it, all the information would appear on a single line.Now hovering reveals a clean, formatted tooltip with the country name in bold, nicely labeled values, and no clutter:
๐ง How Does That Work?
It's awesome to know this little ggplotly() magic trick. But to go further into interactive graphs with R, you need to develop a basic understanding of how this actually works.
There is only one way to create interactive graphs: using the programming languages of the web.
The fundamental thing to understand is that a website, at its core, is built on three technologies:

The foundation of the web: HTML, CSS and Javascript.
Click the logo to learn more.
Building an interactive graph means writing code in those three languages. But we are R users, not web developers! That's why libraries like plotly exist. They are wrappers: we write R code to call them, and they convert our instructions into web languages behind the scenes.
In R, these wrapper libraries are called HTML Widgets, and there are dozens of them targeting very different use cases โ from maps (leaflet) to networks (visNetwork andnetworkD3) to tables (DT).
The plotly package is one of the most popular, specifically because of its ggplotly() function that wraps any ggplot2 chart.
Oh no! ๐ฑ
This lesson is not ready yet.
But we're working hard on it, releasing 1-2 lessons per week. If you're a student, check the discord channel for announcements! (
logo above. )
๐