我正在尝试使用
text =
和 %{text}
在绘图栏中显示 myvar1
值,并作为工具提示文本的一部分。但是,我的代码无法充分生成工具提示并显示“%{text}”而不是其相应的值(如下图所示)。
这是我的代码:
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")
我不确定为什么
%{text}
或%{hovertext}
失败(这里它正在工作),但是,%{customdata}
似乎有效:
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")
可以在此处找到相关的 GitHub 问题。