Interactive selection in ggplotly with geom_hex()

2020-03-12 03:36发布

问题:

I am trying to create a scatterplot matrix with interactive selection between geom items in individual matrix plots. This works when I am dealing with geom_points (inside ggpairs()). When a user selects points in any of the 3 bottom-left individual matrix plots, then they can see the points highlighted in the other matrix plots. Below is a simple example:

library(GGally)
library(ggplot2)
library(plotly)
dat = mtcars[,1:3]
p <- ggpairs(dat)
ggplotly(p)

However, in my case, I am aiming to use geom_hex instead of geom_points. I am hoping to develop a way that a user can highlight a hexagon(s) in an individual matrix, and see all hexagons in the other individual matrices that contain at least one of the observations that composed that original hexagon(s).

I believe this may be a step more complicated, because hexagons are binning summaries that group together several observations. Below is my progress so far toward this goal:

my_fn <- function(data, mapping, ...){
  p <- ggplot(data = data, mapping = mapping) + geom_hex(binwidth=3)
  p
}
p <- ggpairs(dat, lower = list(continuous = my_fn))
ggplotly(p)

When I hover over individual hexagons, I receive interactive information about their count number (which I like). However, when I attempt to select hexagon(s) in one matrix, I do not see corresponding hexagons that contain at least one observation of the original hexagon.

I am relatively familiar with Shiny and R. If you know of a method that I can use to possibly solve this problem, please kindly share!

=============== Session Info ===============

R version 3.3.2 (2016-10-31)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X Mavericks 10.9.5

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] data.table_1.10.0  shiny_0.14.2       hexbin_1.27.1      plotly_4.5.6.9000 
[5] ggplot2_2.2.0.9000 GGally_1.3.0      

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.8        colourpicker_0.3   RColorBrewer_1.1-2 plyr_1.8.4        
 [5] prettyunits_1.0.2  base64enc_0.1-3    tools_3.3.2        progress_1.1.2    
 [9] digest_0.6.10      jsonlite_1.1       tibble_1.2         gtable_0.2.0      
[13] viridisLite_0.1.3  lattice_0.20-34    DBI_0.5-1          crosstalk_0.3.1   
[17] yaml_2.1.14        stringr_1.1.0      dplyr_0.5.0        httr_1.2.1        
[21] htmlwidgets_0.8    grid_3.3.2         reshape_0.8.6      R6_2.2.0          
[25] purrr_0.2.2        tidyr_0.6.0        reshape2_1.4.2     magrittr_1.5      
[29] scales_0.4.1.9000  htmltools_0.3.5    assertthat_0.1     mime_0.5          
[33] xtable_1.8-2       colorspace_1.3-2   httpuv_1.3.3       labeling_0.3      
[37] stringi_1.1.2      miniUI_0.1.1       lazyeval_0.2.0     munsell_0.4.3