Tooltips in ggplot2

I’m sharing this project, trying to add tooltips to ggplot2 plots, because it changed a lot of things for me. In hindsight I think it represents a turning point in my life. It was the first time I worked with SVG or JavaScript. It was what made my skill with R stand out at work. I think it might even have set me on the path to learning as much about data visualization as I did.

Background

The original goal had nothing to do with tooltips. There were some legacy plots being built with base R that we wanted to upgrade to use ggplot2. At the time I didn’t really ask why that was. I assume there were reasons. Someone else had already started the task and gotten frustrated, so I took it up under the assumption I’d be at it a while. I wasn’t.

There were hundreds of plots being made systematically by some in-house software, as well as several dozen custom plots. I was new to my job and had to learn the code base yet, but I don’t think this task took me more than a few weeks. I even spotted a few potential sources of bugs along the way and cleaned some things up. It felt good and kind of new and I liked the challenge of it. The next challenge was soon to follow.

Almost immediately after finishing that task another was brought to my attention. There had long been a desire to make these plots interactive. People were hoping for tooltips, but it hadn’t made a lot of sense to try previously. Developers were busy with other tasks and not familiar with that part of the code base or with R. I agreed to try.

Development

In retrospect I should have questioned the goal itself. The ggplot2 package, as great as it is, isn’t designed to make interactive plots, but other tools are. Why had we taken the time to upgrade to ggplot2 if what we wanted were plots with tooltips? Today I’m much more careful about being sure what our requirements are and making certain we have the tools to meet them. That’s probably because of moments like this one.

In any case, I started some research online. I figured I couldn’t be the first to try to convince ggplot2 to make pots with tooltips. I came up with a plan. First, I’d move away from png and toward svg. I’d build the tooltips by modifying the svg with some inline JavaScript, since we had a requirement that these be self-contained files. Finally, I’d write a custom Geom as an extension of ggplot2 for any trace we used frequently. I started with Geom Bar and extended it to be able to associate a bit of custom text.

Result

The tooltips ended up being more or less useful for different plots. The more custom plots benefit the most. The majority of plots I think gained some accuracy. Here is an example of a typical result:

An example line plot showing tooltips providing exact X and Y coordinates
Interactive versions here

Take-Away

When I look at the code today (which is spread across several repositories) it looks hacky and a bit magical. I’m amazed it’s been in production for nearly five years without any real trouble. It seems like a house of cards, and has probably only stood so long because no one has really touched it since. To be honest though, I’m still proud of it. It taught me a lot and opened a whole new world for me. That wouldn’t happen again until the EDA.