GP.
GP.

Reputation: 487

plotly: the custom hover text feature is broken

It seems that custom plotly hovers are broken. I am using a custom hover text and hoveron='points+fills' to show this both on points and on the filled shape. When hovering on points, as expected it shows my custom string. However, when hovering on the shape, it shows a different hover (without my custom string)!

Example code:

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)))
                   )

Hovering on point (working as expected with the custom hover text): hover point

Hovering on the filled shape (not working - the custom hover text is not present): hover fill

Another side of this issue can be demonstrated below. Replacing hoveron='points+fills' by hoveron='fills'. The custom text is visible nowhere.

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)))
                       )

Result: another example

This is probably a bug, any idea about how to fix it?

Thanks

output of 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

Upvotes: 2

Views: 554

Answers (1)

smarchese
smarchese

Reputation: 530

Using the name attribute seems to work (R 3.6.1, plotly package v.4.9.0) - as opposed to text which just associates a blob of text with each row/point, name serves as a grouping variable in this context:

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'
  )

Another nice thing about this behavior is that we don't need to insert NA values to delimit groups.

If you want to customize the hover labels quite differently for fills vs. points then it may be more straightforward to just separate into two separate traces: here, hovering over points shows both the group name and the x/y/text, which I don't always find desirable.

hover over point

enter image description here

Upvotes: 1

Related Questions