The information shown on hover in visualizations is by default the series name and the value plotted. However, you may want to add the count or other additional information to the hover. This article describes how to go from a visualization with a standard hover text format...
...to one with own that uses a custom hover text template to modify the hover, in this example it adds the count:
Requirements
-
An Area, Bar, Column, Line, or Radar Visualization created using a table as the input.
- Please note: You currently cannot modify the hover text on scatterplots. If this is something you would like to be able to do, please contact support@displayr.com.
- Any statistics that you want to appear on the hover text will need to be applied to the table, such as Count.
In this example, we have a column chart with the below source table that includes the statistics we wish to include in our hover text:
Method
1. Select your visualization.
2. In the object inspector, go to Data > Show Advanced Options > R Code > Edit Code.
3. Find the last row that references hovertext
and click Enter to add a new line.
4. Select Yes to allow you to modify the underlying R code.
5. Add the below code:
hovertext.template = matrix(paste0("%{x}:%{y}<br>n=",
pd$data[,,"Count"]), ncol=NCOL(pd$data)),
- The
hovertext.template
argument requires a matrix as an input so we wrap our code within thematrix
function to include the updated label and the number of columns. - Here,
pd$data
is the input table for the visualization. -
%{x}
is the X axis label while%{y}
is the Y axis value which, in this case, is separated by a colon. We have then added a line break using the HTML <br> character and will append the input table's Count value to go alongside the percentages. Note, this statistic must be showing in our source table. - We then use the
paste0
function to combine all the elements together to form the hover text.
6. Press Calculate.
Technical details
-
You can't use commas in your table rownames or hover text directly. A way around this is to use the comma html tag
<span>,</span>
to insert a comma. If there are commas in the rownames or other headers you'd like to use in the hover you can replace them as so:gsub(",", "<span>,</span>", rownames(pd$data))
- For scatterplots, you will not use
ncol=NCOL(pd$data)
in the code snippet above, instead you will usencol=1
because each row is a single point on the graph (vs bars which will need a hover for each row+column pair).