5 App 3: Adding tooltips with plotly

Tooltips are great for examining single datapoints in the data. We’re going to add tooltips to our scatterplot so that users can hover over a datapoint and see all the other values that go along with a datapoint.

There is an easy way to add tooltips to our graphs: use the plotly library. This is a JavaScript (built on top of d3.js) library that makes your ggplots more interactive, with built in tooltips.

The plotly library itself relies on d3.js, which is a client-side visualization library. So if you add plotly, does this make your shiny app client-based? Not quite. All of the controls and data serving are still done server-side. It’s only the last bit that is translated to JavaScript and is client-side.

Please note that we’re not putting anything on the plotly servers when we do this. All of the data still lives on your server. We’re just using the JavaScript library.

5.1 Exercise

  1. Try running the app.R in 03_tooltips_plotly. Mouse over a datapoint. Neat, huh?
shiny::runApp("03_tooltips_plotly")
  1. How did we supply the infomation to the tooltip? Take a look at the aes_string() code. Notice we’re passing in a bunch of dummy variables into aes_string. Do these variables appear in the tooltip?

  2. Modify the aes_string() statement to only show

5.2 For More Info

The plotly package can instantly make your graphs more interactive using the ggplotly() function. For more info and some recipes, go here: https://plot.ly/ggplot2/