Black Lives Matter. Please consider donating to Black Girls Code today.

Undesired Output in Hovertext

Any idea why my hover texts always displays “xx (size): x.xx” when using plot_ly in R? Even if I copy paste examples directly from the Plotly website I get this undesired text.

This behavior is unique to R; if you run the examples in something else you don’t get this extra information. A work around is to call use marker = list(size = variablename * 20) and remove the size = variablename parameter.

For example, in order to correctly replicate the example from the plotly website for custom hover text you could use:

p <- mtcars %>% plot_ly(x = disp, y = mpg, mode = “markers”, color = cyl, marker = list(size = wt * 20), hoverinfo = “text”, text = paste("Displacement = ", mtcars$disp, "Miles Per Gallon = ", mtcars$mpg)) %>% layout(title =“Custom Hover Text”)
p

1 Like

I couldn’t get the “wt (size)” part to show up, but you can use the plotly_build function and edit the text directly with regular expressions to get rid of that.
Here is your example where I got rid of the “Miles per gallon” part of the hover text.
In the first plot, p, you can see the full text. In the second plot, q, the “Miles per gallon” part is gone.

p <- mtcars %>% plot_ly(x = disp, y = mpg, mode = “markers”, color = cyl, marker = list(size = wt * 20), hoverinfo = “text”, text = paste("Displacement = ", mtcars$disp, "Miles Per Gallon = ", mtcars$mpg)) %>% layout(title =“Custom Hover Text”)
p

q = plotly_build§
q$data[[1]]$text = gsub(’ Miles.*’, ‘’, q$data[[1]]$text)
q

1 Like