highcharter:突出显示一组中的点

highcharter: Highlight points in a group

当使用 highcharter 进行交互式绘图时,如何指定组中的数据点必须一起突出显示?

library(highcharter)
library(dplyr)
library(tidyr)

dfr <- data.frame(sample=c("A","B","C","D"),part=c(2,3,4,6),cat=c(5,7,3,3))
dfr1 <- dfr %>% tidyr::gather(key=metric,value=value,-sample)
dfr1
#>   sample metric value
#> 1      A   part     2
#> 2      B   part     3
#> 3      C   part     4
#> 4      D   part     6
#> 5      A    cat     5
#> 6      B    cat     7
#> 7      C    cat     3
#> 8      D    cat     3

dfr1 %>%
  hchart(.,"scatter",hcaes(x=metric,y=value,group=factor(sample))) %>%
  hc_xAxis(type="category",title=list(text="Metrics"),crosshair=TRUE) %>%
  hc_yAxis(type="linear",title=list(text="Counts"),crosshair=TRUE) %>%
  hc_chart(zoomType="xy",inverted=T) %>%
  hc_tooltip(useHTML=TRUE,formatter = JS("function(){
                                         return('<b>Sample: </b>'+this.point.sample+'</br>')
                                         }"))

在此示例中,当我将鼠标悬停在蓝点(样本 A)上时,我希望所有其他蓝点(样本 A)也突出显示。

reprex package (v2.0.1)

创建于 2022-03-11 会话信息
sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R version 4.1.0 (2021-05-18)
#>  os       Ubuntu 20.04.4 LTS
#>  system   x86_64, linux-gnu
#>  ui       X11
#>  language en_GB:en
#>  collate  en_GB.UTF-8
#>  ctype    en_GB.UTF-8
#>  tz       Europe/Stockholm
#>  date     2022-03-11
#>  pandoc   2.14.0.3 @ /usr/lib/rstudio/bin/pandoc/ (via rmarkdown)
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version date (UTC) lib source
#>  assertthat    0.2.1   2019-03-21 [1] CRAN (R 4.1.0)
#>  backports     1.4.1   2021-12-13 [1] CRAN (R 4.1.0)
#>  broom         0.7.11  2022-01-03 [1] CRAN (R 4.1.0)
#>  callr         3.7.0   2021-04-20 [1] CRAN (R 4.1.0)
#>  cli           3.1.0   2021-10-27 [1] CRAN (R 4.1.0)
#>  crayon        1.4.2   2021-10-29 [1] CRAN (R 4.1.0)
#>  curl          4.3.2   2021-06-23 [1] CRAN (R 4.1.0)
#>  data.table    1.14.2  2021-09-27 [1] CRAN (R 4.1.0)
#>  DBI           1.1.2   2021-12-20 [1] CRAN (R 4.1.0)
#>  digest        0.6.29  2021-12-01 [1] CRAN (R 4.1.0)
#>  dplyr       * 1.0.7   2021-06-18 [1] CRAN (R 4.1.0)
#>  ellipsis      0.3.2   2021-04-29 [1] CRAN (R 4.1.0)
#>  evaluate      0.14    2019-05-28 [1] CRAN (R 4.1.0)
#>  fansi         1.0.2   2022-01-14 [1] CRAN (R 4.1.0)
#>  fastmap       1.1.0   2021-01-25 [1] CRAN (R 4.1.0)
#>  fs            1.5.2   2021-12-08 [1] CRAN (R 4.1.0)
#>  generics      0.1.1   2021-10-25 [1] CRAN (R 4.1.0)
#>  glue          1.6.0   2021-12-17 [1] CRAN (R 4.1.0)
#>  highcharter * 0.9.4   2022-01-03 [1] CRAN (R 4.1.0)
#>  highr         0.9     2021-04-16 [1] CRAN (R 4.1.0)
#>  htmltools     0.5.2   2021-08-25 [1] CRAN (R 4.1.0)
#>  htmlwidgets   1.5.4   2021-09-08 [1] CRAN (R 4.1.0)
#>  httr          1.4.2   2020-07-20 [1] CRAN (R 4.1.0)
#>  igraph        1.2.11  2022-01-04 [1] CRAN (R 4.1.0)
#>  jsonlite      1.7.3   2022-01-17 [1] CRAN (R 4.1.0)
#>  knitr         1.37    2021-12-16 [1] CRAN (R 4.1.0)
#>  lattice       0.20-45 2021-09-22 [1] CRAN (R 4.1.0)
#>  lifecycle     1.0.1   2021-09-24 [1] CRAN (R 4.1.0)
#>  lubridate     1.8.0   2021-10-07 [1] CRAN (R 4.1.0)
#>  magrittr      2.0.1   2020-11-17 [1] CRAN (R 4.1.0)
#>  mime          0.12    2021-09-28 [1] CRAN (R 4.1.0)
#>  pillar        1.6.4   2021-10-18 [1] CRAN (R 4.1.0)
#>  pkgconfig     2.0.3   2019-09-22 [1] CRAN (R 4.1.0)
#>  processx      3.5.2   2021-04-30 [1] CRAN (R 4.1.0)
#>  ps            1.6.0   2021-02-28 [1] CRAN (R 4.1.0)
#>  purrr         0.3.4   2020-04-17 [1] CRAN (R 4.1.0)
#>  quantmod      0.4.18  2020-12-09 [1] CRAN (R 4.1.0)
#>  R.cache       0.15.0  2021-04-30 [1] CRAN (R 4.1.0)
#>  R.methodsS3   1.8.1   2020-08-26 [1] CRAN (R 4.1.0)
#>  R.oo          1.24.0  2020-08-26 [1] CRAN (R 4.1.0)
#>  R.utils       2.11.0  2021-09-26 [1] CRAN (R 4.1.0)
#>  R6            2.5.1   2021-08-19 [1] CRAN (R 4.1.0)
#>  reprex        2.0.1   2021-08-05 [1] CRAN (R 4.1.0)
#>  rlang         0.4.12  2021-10-18 [1] CRAN (R 4.1.0)
#>  rlist         0.4.6.2 2021-09-03 [1] CRAN (R 4.1.0)
#>  rmarkdown     2.11    2021-09-14 [1] CRAN (R 4.1.0)
#>  rstudioapi    0.13    2020-11-12 [1] CRAN (R 4.1.0)
#>  sessioninfo   1.2.2   2021-12-06 [1] CRAN (R 4.1.0)
#>  stringi       1.7.6   2021-11-29 [1] CRAN (R 4.1.0)
#>  stringr       1.4.0   2019-02-10 [1] CRAN (R 4.1.0)
#>  styler        1.6.2   2021-09-23 [1] CRAN (R 4.1.0)
#>  tibble        3.1.6   2021-11-07 [1] CRAN (R 4.1.0)
#>  tidyr       * 1.1.4   2021-09-27 [1] CRAN (R 4.1.0)
#>  tidyselect    1.1.1   2021-04-30 [1] CRAN (R 4.1.0)
#>  TTR           0.24.3  2021-12-12 [1] CRAN (R 4.1.0)
#>  utf8          1.2.2   2021-07-24 [1] CRAN (R 4.1.0)
#>  vctrs         0.3.8   2021-04-29 [1] CRAN (R 4.1.0)
#>  webshot       0.5.2   2019-11-22 [1] CRAN (R 4.1.0)
#>  withr         2.4.3   2021-11-30 [1] CRAN (R 4.1.0)
#>  xfun          0.29    2021-12-14 [1] CRAN (R 4.1.0)
#>  xml2          1.3.3   2021-11-30 [1] CRAN (R 4.1.0)
#>  xts           0.12.1  2020-09-09 [1] CRAN (R 4.1.0)
#>  yaml          2.2.1   2020-02-01 [1] CRAN (R 4.1.0)
#>  zoo           1.8-9   2021-03-09 [1] CRAN (R 4.1.0)
#> 
#>  [1] /home/user/miniconda3/envs/r-4.1/lib/R/library
#> 
#> ──────────────────────────────────────────────────────────────────────────────

我知道时间已经过去很久了,但万一有人对此感兴趣...

another solution 建议共享工具提示的格式化程序。我认为可以在这里进行调整。

当将鼠标悬停在组中的某个点上时,同一组中的所有点都会突出显示。

library(highcharter)
library(tidyverse)

dfr <- data.frame(sample=c("A","B","C","D"),part=c(2,3,4,6),cat=c(5,7,3,3))
dfr1 <- dfr %>% pivot_longer(cols = -sample, names_to = "metric")
dfr1

dfr1 %>%
  hchart(.,"scatter",hcaes(x=metric,y=value,group=factor(sample))) %>%
  hc_xAxis(type="category",title=list(text="Metrics"),crosshair=TRUE) %>%
  hc_yAxis(type="linear",title=list(text="Counts"),crosshair=TRUE) %>%
  hc_chart(zoomType="xy",inverted=T) %>%
  hc_tooltip(useHTML=TRUE,formatter = JS(
    "function(args){
      var this_point_index = this.series.data.indexOf(this.point);
      var this_series_index = this.series.index;
      var that_series_index = this.series.index == 0 ? 1 : 0;
      var that_series = args.chart.series[that_series_index];
      var that_point = that_series.data[this_point_index];
      return ('<b>Sample: </b>'+this.point.sample+'</br>');
    }"
  ))

会话信息:

R version 4.1.3 (2022-03-10)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Monterey 12.2.1

Matrix products: default
LAPACK: /Library/Frameworks/R.framework/Versions/4.1/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

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

other attached packages:
 [1] forcats_0.5.1     stringr_1.4.0     dplyr_1.0.7       purrr_0.3.4      
 [5] readr_2.1.0       tidyr_1.1.4       tibble_3.1.4      ggplot2_3.3.5    
 [9] tidyverse_1.3.1   highcharter_0.9.4

loaded via a namespace (and not attached):
 [1] httr_1.4.2         jsonlite_1.8.0     viridisLite_0.4.0 
 [4] modelr_0.1.8       shiny_1.7.1        assertthat_0.2.1  
 [7] TTR_0.24.2         cellranger_1.1.0   yaml_2.2.1        
[10] pillar_1.6.2       backports_1.2.1    lattice_0.20-45   
[13] glue_1.6.0         rlist_0.4.6.1      digest_0.6.29     
[16] RColorBrewer_1.1-2 promises_1.2.0.1   rvest_1.0.1       
[19] colorspace_2.0-2   htmltools_0.5.2    httpuv_1.6.4      
[22] plyr_1.8.6         pkgconfig_2.0.3    broom_0.7.9       
[25] haven_2.4.3        xtable_1.8-4       scales_1.1.1      
[28] openxlsx_4.2.4     later_1.3.0        tzdb_0.1.2        
[31] generics_0.1.0     ellipsis_0.3.2     withr_2.4.3       
[34] lazyeval_0.2.2     cli_3.0.1          quantmod_0.4.18   
[37] magrittr_2.0.2     crayon_1.4.2       readxl_1.3.1      
[40] mime_0.12          evaluate_0.14      GGally_2.1.2      
[43] fs_1.5.2           fansi_0.5.0        xml2_1.3.2        
[46] xts_0.12.1         tools_4.1.3        data.table_1.14.2 
[49] hms_1.1.0          lifecycle_1.0.1    plotly_4.9.4.1    
[52] reprex_2.0.1       munsell_0.5.0      zip_2.2.0         
[55] formattable_0.2.1  compiler_4.1.3     rlang_0.4.12      
[58] grid_4.1.3         rstudioapi_0.13    htmlwidgets_1.5.4 
[61] igraph_1.2.6       rmarkdown_2.10     gtable_0.3.0      
[64] DBI_1.1.2          reshape_0.8.8      curl_4.3.2        
[67] R6_2.5.1           zoo_1.8-9          lubridate_1.7.10  
[70] knitr_1.34         fastmap_1.1.0      utf8_1.2.2        
[73] stringi_1.7.4      Rcpp_1.0.8         vctrs_0.3.8       
[76] dbplyr_2.1.1       tidyselect_1.1.1   xfun_0.26  

情节