I am trying to use text =
and %{text}
to display myvar1
values in Plotly bars, and also as part of the tooltip text. However, my code fails to produce the tooltip adequatly and shows "%{text}" instead of its corresponding value (as shown in image below).
Here is my code:
library(plotly)
dfcom <- data.frame(
myvar1 = c("Media", "Campaigns", "Website"),
alcance = c(6814511L, 400000L, 225329L),
type = c("viewers", "viewers", "viewers"))
plot_ly(
dfcom,
x = ~alcance, y = ~type,
text = ~myvar1,
textfont = list(color = I("white")),
type = 'bar',
color = ~myvar1,
colors = c("chartreuse4", "darkolivegreen", "darkgreen"),
showlegend = FALSE,
hovertemplate = "%{x:,.0f} people reached by %{text} <extra></extra>") %>%
layout(barmode = "stack")
I'm not sure why %{text}
or %{hovertext}
fail (here it's working), however, %{customdata}
seems to work:
library(plotly)
dfcom <- data.frame(
myvar1 = c("Media", "Campaigns", "Website"),
alcance = c(6814511L, 400000L, 225329L),
type = c("viewers", "viewers", "viewers")
)
plot_ly(
data = dfcom,
x = ~ alcance,
y = ~ type,
text = ~ myvar1,
# hovertext = ~ myvar1,
customdata = ~ myvar1,
textfont = list(color = I("white")),
type = 'bar',
color = ~ myvar1,
colors = c("chartreuse4", "darkolivegreen", "darkgreen"),
showlegend = FALSE,
hovertemplate = "%{x:,.0f} people reached by %{customdata}<extra></extra>"
) %>%
layout(barmode = "stack")
A related GitHub issue can be found here.