繁体   English   中英

文本参数未正确传递给 plotly 条形图中的 hovertemplate

[英]Text argument is not passed correctly to hovertemplate in plotly bar chart

我使用自定义悬停模板创建下面的条形图,但是当我在条形上方 hover 时,我得到的不是我的text ,而是一个"text"

Cum4c<-structure(list(Country = c("United States Of America", "Mexico", 
                                  "United Kingdom", "Brazil", "Germany"), Deaths = c(91765, 31887, 
                                                                                     31134, 29081, 22546), lab = c("91,765", "31,887", "31,134", "29,081", 
                                                                                                                   "22,546")), row.names = c(NA, -5L), class = c("tbl_df", "tbl", 
                                                                                                                                                                 "data.frame"))
    
    fig1 <- plot_ly()%>%
      add_bars(data = Cum4c, x = ~Country, y = ~Deaths,
               text = ~ paste("<b>Country:</b>", Country, "<br><b>Deaths:</b>", lab),
               hovertemplate  = paste('%{text}<extra></extra>'),
               colors = c("#60ab3d","#6bbabf","#c4d436","#3e5b84","#028c75"),
               color = ~Country
               
               
               
      ) 
    fig1 <- fig1 %>% layout(showlegend = TRUE,title=list(text="worldwide,by selected territories and period",x = 0,y=1,font=list(size=10)),
                            font = list(color = '#a2a2a2'),
                            legend=list(y=1.2,x=0,title=list(text='<b> Top 5 </b>')),
                            
                            yaxis = list(fixedrange = TRUE,title="",
                                         #dtick = 250000
                                         showgrid = T,gridcolor = "#a2a2a2", showline = FALSE, showticklabels = TRUE, domain= c(0, 0.85)),
                            xaxis = list(fixedrange = TRUE,title="",zeroline = FALSE, showline = T,showticklabels = F,tickangle=45, showgrid = FALSE))%>% 
      
      config(modeBarButtonsToRemove = c('toImage',"zoom2d","toggleSpikelines","hoverClosestCartesian","hoverCompareCartesian","drawline","autoScale2d" ,"resetScale2d","zoomIn2d","zoomOut2d","pan2d",'select2d','lasso2d'))%>%
      config(displaylogo = FALSE)
    fig1

有两种方法可以达到您想要的结果:

  1. 去掉text ,改用hovertemplate = ~paste("<b>Country:</b>", Country, "<br><b>Deaths:</b>", lab)

  2. 摆脱hovertemplate并改用hoverinfo = 'text'

这两种方法都会给你相同的结果。

library(plotly)

fig1 <- plot_ly() %>%
  add_bars(
    data = Cum4c, x = ~Country, y = ~Deaths,
    hovertemplate = ~paste("<b>Country:</b>", Country, "<br><b>Deaths:</b>", lab),
    colors = c("#60ab3d", "#6bbabf", "#c4d436", "#3e5b84", "#028c75"),
    color = ~Country
  )
fig1

fig1 <- plot_ly() %>%
  add_bars(
    data = Cum4c, x = ~Country, y = ~Deaths,
    text = ~ paste("<b>Country:</b>", Country, "<br><b>Deaths:</b>", lab),
    hoverinfo = 'text',
    
    colors = c("#60ab3d", "#6bbabf", "#c4d436", "#3e5b84", "#028c75"),
    color = ~Country
  )
fig1

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM