1. Labeling your data
Now that you know how to customize the appearance of your traces, you'll learn how to customize a key interactive tool: the hover information.
By default, plotly will add hover information to your chart. Let's see what information appears by default.
2. Hover info
First, let's consider the scatterplot of alcohol content against flavanoids. As we hover our mouse above specific points the coordinates appear; however, they are displayed only as coordinate pairs, without variable names.
3. Hover info
Next, let's consider a bar chart of wine type. The hover info again reveals the coordinates of the top of each bar but lacks variable names.
4. Hover info
As another example, consider boxplots of alcohol content by wine quality. Here, the hover information gives the category name, high, medium, or low, as well as summary statistics, including the minimum, first quartile, median, third quartile, and maximum.
By now you see the pattern. By default, plotly shows the coordinates in the hover info, but the default settings can be improved.
5. Changing the default
In some situations, a simple change to the hover info, such as only displaying the y coordinate may make your chart more intuitive. For example, on a bar chart, each bar is clearly labeled with the level of a factor.
Let's take a look at the code.
The only change necessary is to add the hoverinfo argument to plot_ly(). The hoverinfo argument allows you to choose what information is displayed. By default hoverinfo is set to "all", displaying information about all elements of a trace. To restrict the amount of information, you can set hoverinfo to "x" or "y", or you can remove it altogether by setting hoverinfo to "none".
For multivariate displays "x + y" and "x + y + z" are additional options.
6. Custom hover text
Let's return to the scatterplot of alcohol content against flavanoids.
Here, we want to customize the hover information to include the variable names, rather than simply displaying an ordered pair.
To customize the hover info there are two key arguments that are passed to the plot_ly() function: hoverinfo and text.
We set hoverinfo equal to "text" to indicate that we will be manually defining the information.
The text argument expects a character string, so we use the paste() function to string together the information we want to display. Here, we type polished variable labels as character strings and use the variable without quotes to specify the value. To start a new line use the HTML line-break, br, wrapped in angled brackets.
Finally, notice that we use the tilde operator to specify the hover info text. Remember that the tilde operator is used to map columns to aesthetic parameters. Here, we map the variables Flavanoids and Alcohol to the hover info. Without this tilde, plotly would return an error, since these objects are not in the global environment, but are stored as columns in our dataset.
7. Let's practice!
While you are polishing your charts, remember to ask yourself whether the hover info clearly communicates information to the reader. If it doesn't, then you should think about how to change the hover info so that the reader never needs to guess what you want them to see.