Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

event_data doesn't work with scatterplot (type = "scattergl") #624

Closed
naikai opened this issue Jun 7, 2016 · 3 comments
Closed

event_data doesn't work with scatterplot (type = "scattergl") #624

naikai opened this issue Jun 7, 2016 · 3 comments

Comments

@naikai
Copy link

naikai commented Jun 7, 2016

Hello,

I was trying to follow the examples from these two links:
Shiny-coupled-events
#596

Everything works fine until I changed the type = 'scatter' to type = 'scattergl'
I lost the "box select" option after the change.
In comparison, it work fine for ggplot + ggplotly and I can still get the data info from the selected box area.
I would like to use the additional features from plot_ly instead of switching back to ggplot
Thank you for your help.

Here is a minimal example code

library(shiny)
library(plotly)

adds <- mtcars[1:5, ]
adds[, c(1, 6)] <- NA
rownames(adds) <- paste(rownames(adds), "(NA)")
some_null_cars <- rbind(mtcars, adds)

ui <- fluidPage(
  radioButtons("plotType", "Plot Type:", choices = c("ggplotly", "plotly")),
  plotlyOutput("plot"),
  verbatimTextOutput("brush")
)

server <- function(input, output, session) {

  output$plot <- renderPlotly({
    # use the key aesthetic/argument to help uniquely identify selected observations
    key <- row.names(some_null_cars)
    if (identical(input$plotType, "ggplotly")) {
      p <- ggplot(some_null_cars, aes(x = mpg, y = wt, colour = factor(vs), key = key)) +
        geom_point()
      ggplotly(p) %>% layout(dragmode = "select")
    } else {
      plot_ly(some_null_cars, x = mpg, y = wt, key = key, mode = "markers", type = "scattergl") %>%
        layout(dragmode = "select")
    }
  })

  output$brush <- renderPrint({
    d <- event_data("plotly_selected")
    if (is.null(d)) "Click and drag events (i.e., select/lasso) appear here (double-click to clear)" else d
  })
}

shinyApp(ui, server)

Here is my session info:

> sessionInfo()
R version 3.3.0 (2016-05-03)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X 10.11.5 (El Capitan)

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] plotly_3.6.4  ggplot2_2.1.0 shiny_0.13.2 

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.5      magrittr_1.5     munsell_0.4.3    colorspace_1.2-6 xtable_1.8-2    
 [6] R6_2.1.2         httr_1.1.0       plyr_1.8.3       dplyr_0.4.3      tools_3.3.0     
[11] parallel_3.3.0   grid_3.3.0       gtable_0.2.0     DBI_0.4-1        htmltools_0.3.5 
[16] yaml_2.1.13      digest_0.6.9     assertthat_0.1   gridExtra_2.2.1  tidyr_0.4.1     
[21] base64enc_0.1-3  htmlwidgets_0.6  viridis_0.3.4    rsconnect_0.4.3  mime_0.4        
[26] labeling_0.3     scales_0.4.0     jsonlite_0.9.20  httpuv_1.3.3    
@cpsievert
Copy link
Collaborator

Marking this as a duplicate since this is a known limitation of gl2d trace types -- plotly/plotly.js#130

@naikai
Copy link
Author

naikai commented Jun 9, 2016

Sorry I missed it. Thanks

@myles-lewis
Copy link

myles-lewis commented Jan 6, 2017

I know this is supposed to have been fixed in plotly.js but I find that the 2d scatter plot using either type='scattergl' or toWebGL() breaks the plotly_click event in plotly.r. It works fine for me for scatter3d. I want to show a 2d plot of 21,000 points, hence the need for scattergl.

This issue was reported here: plotly/plotly.js#743
but is supposed to have been completed here: plotly/plotly.js#994

Here is demo code:

# Dummy data
  pathotype.z <- data.frame(x=rnorm(15000), y=rnorm(15000), logP=rnorm(15000)^2, hcol=sample(1:5, 15000, replace=T), 
                          gene=paste0("A", as.integer(rnorm(15000)*10000)))

genelookup <- list()
  genelookup <- tapply(pathotype.z$gene, pathotype.z$hcol, function(x) x)

  ui <- fluidPage(fluidRow(column(8, plotlyOutput("volc3d", height=640)), 
                         column(4, plotlyOutput("scatterplot", height=500),
                                fluidRow(
                                  column(12, radioButtons("plottype", label = h5("Plot"),
                                                         choices = list("2d" = 1, "3d" = 2), selected = 2))
                                  ))),
        verbatimTextOutput("selection"))

  server <- function(input, output, session) {
  output$volc3d <- renderPlotly({
    pt <- as.numeric(input$plottype)
    if (length(pt)==0) pt <- 2
    if (pt==2) {
      plot_ly(d=pathotype.z, x=~x, y=~y, z=~logP, 
              marker=list(size=2.5), color=~hcol, 
              text=~gene, type='scatter3d', mode = 'markers', source='volc3d')
    } else {
      plot_ly(data=pathotype.z, x=~x, y=~y, 
                  marker=list(size=5), color=~hcol,
                  text=~gene, type="scattergl", mode = "markers")
    }})
  
  output$selection <- renderPrint({
    s <- event_data("plotly_click", source = "volc3d")
    if (length(s) == 0) {"Click on gene to display scatterplot"
    } else {
    as.list(s)
    }})
  
  output$scatterplot <- renderPlotly({
    s <- event_data("plotly_click", source = "volc3d")
    if (length(s)) {
      p <- s[["pointNumber"]]
      c <- s[["curveNumber"]]
      gene <- genelookup[[c+1]][p+1]
      plot_ly(pathotype.z, x=~hcol, y=~y, type='box') %>%
        layout(title=gene, yaxis=list(title=gene), 
               margin=list(t=100,l=50,r=50,b=50))
    } else {plotly_empty()}
  })
  }

  shinyApp(ui, server)

Here is session info:

> sessionInfo()
R version 3.3.1 (2016-06-21)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

locale:
[1] LC_COLLATE=English_United Kingdom.1252  LC_CTYPE=English_United Kingdom.1252    LC_MONETARY=English_United Kingdom.1252
[4] LC_NUMERIC=C                            LC_TIME=English_United Kingdom.1252    

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

other attached packages:
 [1] gplots_3.0.1               ComplexHeatmap_1.10.2      ggforce_0.1.1              shiny_0.14.2              
 [5] plotly_4.5.6               ggplot2_2.2.0              DESeq2_1.12.4              SummarizedExperiment_1.2.3
 [9] Biobase_2.32.0             GenomicRanges_1.24.3       GenomeInfoDb_1.8.7         IRanges_2.6.1             
[13] S4Vectors_0.10.3           BiocGenerics_0.18.0       

loaded via a namespace (and not attached):
 [1] bitops_1.0-6         RColorBrewer_1.1-2   httr_1.2.1           prabclus_2.2-6       tools_3.3.1          R6_2.2.0            
 [7] KernSmooth_2.23-15   rpart_4.1-10         Hmisc_4.0-1          DBI_0.5-1            lazyeval_0.2.0       colorspace_1.3-2    
[13] trimcluster_0.1-2    nnet_7.3-12          GetoptLong_0.1.5     gridExtra_2.2.1      base64_2.0           htmlTable_1.7       
[19] labeling_0.3         caTools_1.17.1       diptest_0.75-7       scales_0.4.1         DEoptimR_1.0-8       mvtnorm_1.0-5       
[25] robustbase_0.92-7    genefilter_1.54.2    stringr_1.1.0        digest_0.6.10        foreign_0.8-67       XVector_0.12.1      
[31] base64enc_0.1-3      htmltools_0.3.5      htmlwidgets_0.8      GlobalOptions_0.0.10 RSQLite_1.1-1        shape_1.4.2         
[37] jsonlite_1.1         mclust_5.2           BiocParallel_1.6.6   gtools_3.5.0         acepack_1.4.1        dendextend_1.3.0    
[43] dplyr_0.5.0          RCurl_1.95-4.8       magrittr_1.5         modeltools_0.2-21    Formula_1.2-1        Matrix_1.2-7.1      
[49] Rcpp_0.12.8          munsell_0.4.3        yaml_2.1.14          stringi_1.1.2        whisker_0.3-2        MASS_7.3-45         
[55] zlibbioc_1.18.0      flexmix_2.3-13       plyr_1.8.4           gdata_2.17.0         udunits2_0.13        lattice_0.20-33     
[61] splines_3.3.1        annotate_1.50.1      circlize_0.3.9       locfit_1.5-9.1       knitr_1.15.1         rjson_0.2.15        
[67] fpc_2.1-10           geneplotter_1.50.0   XML_3.98-1.5         latticeExtra_0.6-28  data.table_1.10.0    tweenr_0.1.5        
[73] httpuv_1.3.3         gtable_0.2.0         openssl_0.9.5        purrr_0.2.2          tidyr_0.6.0          kernlab_0.9-25      
[79] assertthat_0.1       mime_0.5             xtable_1.8-2         class_7.3-14         survival_2.39-4      viridisLite_0.1.3   
[85] tibble_1.2           AnnotationDbi_1.34.4 memoise_1.0.0        units_0.4-1          cluster_2.0.5   

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants