在 R Plotly 工具提示中使用自定义文本无法显示 %{text} 值

问题描述 投票:0回答:1

我正在尝试使用

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")
r plotly
1个回答
0
投票

我不确定为什么

%{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 问题。

© www.soinside.com 2019 - 2024. All rights reserved.