【问题标题】:plotly: the custom hover text feature is brokenplotly:自定义悬停文本功能已损坏
【发布时间】:2018-09-19 07:34:33
【问题描述】:

似乎自定义plotly 悬停已损坏。我正在使用自定义悬停文本和hoveron='points+fills' 在点和填充形状上显示此内容。当悬停在点上时,正如预期的那样,它会显示我的自定义字符串。但是,当悬停在形状上时,它会显示不同的悬停(没有我的自定义字符串)!

示例代码:

library(plotly)
data.frame(AA=c(2,3,3,2,NA, 6,7,7,6,NA),
       BB=c(2,2,3,2,NA, 6,6,7,6,NA),
       CC=c(rep('abc', 5), rep('xyz', 5)),
       LL=c(rep('A', 5), rep('B', 5))) %>%
plot_ly() %>%
         add_trace(x=~AA,
                   y=~BB,
                   text=~paste('<br> <b>Example</b> of <em>custom</em> hover text <br>', LL, '<br>', CC, '<br>.'),
                   split=~LL, 
                   mode="lines", 
                   fill="toself", 
                   hoveron='points+fills',
                   type="scatter", 
                   color = I(c(rep(toRGB("black", 1), 5),
                               rep(toRGB("red", 1), 5)))
                   )

悬停在点上(使用自定义悬停文本按预期工作):

悬停在填充形状上(不起作用 - 自定义悬停文本不存在):

这个问题的另一方面可以在下面演示。将hoveron='points+fills' 替换为hoveron='fills'。自定义文本无处可见。

library(plotly)
data.frame(AA=c(2,3,3,2,NA, 6,7,7,6,NA),
           BB=c(2,2,3,2,NA, 6,6,7,6,NA),
           CC=c(rep('abc', 5), rep('xyz', 5)),
           LL=c(rep('A', 5), rep('B', 5))) %>%
    plot_ly() %>%
             add_trace(x=~AA,
                       y=~BB,
                       text=~paste('<br> <b>Example</b> of <em>custom</em>     hover text <br>', LL, '<br>', CC, '<br>.'),
                       split=~LL, 
                       mode="lines", 
                       fill="toself", 
                       hoveron='fills',
                       type="scatter", 
                       color = I(c(rep(toRGB("black", 1), 5),
                                   rep(toRGB("red", 1), 5)))
                       )

结果:

这可能是一个错误,知道如何修复它吗?

谢谢

sessionInfo() 的输出:

R version 3.4.4 (2018-03-15)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 17.10

Matrix products: default
BLAS: /usr/lib/x86_64-linux-gnu/openblas/libblas.so.3
LAPACK: /usr/lib/x86_64-linux-gnu/libopenblasp-r0.2.20.so

locale:
 [1] LC_CTYPE=pt_BR.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=pt_BR.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=pt_BR.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=pt_BR.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=pt_BR.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] bindrcpp_0.2  plotly_4.7.1  ggplot2_2.2.1

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.16        bindr_0.1.1         magrittr_1.5       
 [4] munsell_0.4.3       xtable_1.8-2        viridisLite_0.3.0  
 [7] colorspace_1.3-2    R6_2.2.2            rlang_0.2.0        
[10] httr_1.3.1          plyr_1.8.4          dplyr_0.7.4        
[13] tools_3.4.4         grid_3.4.4          data.table_1.10.4-3
[16] gtable_0.2.0        crosstalk_1.0.0     htmltools_0.3.6    
[19] yaml_2.1.18         lazyeval_0.2.1      assertthat_0.2.0   
[22] digest_0.6.15       tibble_1.4.2        shiny_1.0.5        
[25] purrr_0.2.4         tidyr_0.8.0         htmlwidgets_1.0    
[28] mime_0.5            glue_1.2.0          compiler_3.4.4     
[31] pillar_1.2.1        scales_0.5.0        jsonlite_1.5       
[34] httpuv_1.3.6.2      pkgconfig_2.0.1

【问题讨论】:

  • 可以添加sessionInfo()的输出吗?
  • 好的,我刚刚添加了。
  • 您希望看到什么文字? hovertext 显示的是特定于某个点的信息,因此为形状显示相同的信息会令人困惑。
  • 这是问题的演示,所以数据没有意义。我想说的是自定义文本悬停没有按预期工作,因为它不考虑text 信息。请注意,在实际情况下,您可能希望在自定义悬停中添加一些有用的东西。

标签: plotly r-plotly plotly.js


【解决方案1】:

使用name 属性似乎有效(R 3.6.1,plotly 包 v.4.9.0) - 与 text 不同,text 只是将一个文本块与每一行/点相关联,name 服务在此上下文中作为分组变量:

library(plotly)

geod = data.frame(
  cx = c(0,10,6,0,5,5,9,5),
  cy = c(1,8,2,1,6,8,7,6),
  grps = rep(c('foobar','barfou'),each = 4),
  ptxt = LETTERS[1:8]
)
plot_ly() %>% 
  add_polygons(
    data = geod,
    x = ~cx, y = ~cy,
    name = ~grps, text = ~ptxt,
    hoveron = 'points+fills'
  )

这种行为的另一个好处是我们不需要插入 NA 值来分隔组。

如果您想为填充和点自定义悬停标签的方式完全不同,那么将其分成两条单独的轨迹可能更直接:在这里,悬停在点上both显示组名和x/y/text,我并不总是觉得可取。

【讨论】:

    猜你喜欢
    • 2018-09-04
    • 2021-10-03
    • 2020-12-28
    • 2014-10-24
    • 2021-08-14
    • 1970-01-01
    • 2021-04-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多