Hover text not working with a single element

I find that the hover text doesn’t work as expected when it has only one element.

When there are more than one element provided as the ‘text’ argument of plot_ly, ‘hovertemplate’ works as intended.
However, when the ‘text’ is a singleton, ‘hovertemplate’ does not replace %{text} to its value but shows it in verbatim ("%{text}" appears in the tooltip).

Is this a bug, or am I using it incorrectly?
I’m using Plotly 4.9.3 with R 4.0.3 on Ubuntu Linux.

The following is the minimal working example:

library(plotly)
library(tidyverse)

full <- data.frame(categories = letters[1:5], values = c(1:5))

selection <- c("c", "d")
lab <- letters[10:10+length(selection)]
df <- full %>% filter(categories %in% selection)

fig <- plot_ly(data=df, x = ~values, y = ~categories,
               type = "bar", orientation = "h", name = "test",
               text = lab,
               hovertemplate = "%{y} (%{text}): %{x}"
)

fig

This works as intended:
image

However, when the selection has only one element in it, it doesn’t:

selection <- c("c")
lab <- letters[10:10+length(selection)]
df <- full %>% filter(categories %in% selection)

fig <- plot_ly(data=df, x = ~values, y = ~categories,
               type = "bar", orientation = "h", name = "test",
               text = lab,
               hovertemplate = "%{y} (%{text}): %{x}"
)

fig

image

Is there any work around for this?

Thanks!

Were you able to find a solution to this? I’m having the exact same problem.

I have the same problem. I haven’t found a way to fix it.

Came across this thread as I was encountering the same issue and found this post with an explanation and solution: https://stackoverflow.com/questions/64127628/cannot-display-the-text-in-plotly-with-r-if-there-is-only-one-data-point. Hope this helps!