exaexa / scattermore

very fast scatterplots for R
https://exaexa.github.io/scattermore/
GNU General Public License v3.0
238 stars 7 forks source link

how to generate interactive scattermore plot? #5

Closed BhaktiDwivedi closed 3 years ago

BhaktiDwivedi commented 3 years ago

Thank you for developing the scattermore. I find it very useful. Is it possible to generate an interactive scattermore plot? (something in lines to plotly). My data consists of ~ million data points and plotly function takes a while to plot. I would really like to speed up the plotting function especially the time it takes the plotly to load/print the plot.

For example, I have the below plotly function written in R:

` bins <- length(unique(df[["exp"]])) exp_col <- colorRampPalette(c('khaki','darkred'))(bins)

p <- plot_ly(df, x = ~x, y = ~y, color = ~exp, colors = exp_col, marker = list( symbol = "circle", size = 6, opacity = 0.40, mode = "markers", line = list(color = ~exp, colors = exp_col, width=0.5) ), width = 1300, height = 900, type = "scattergl", mode = "markers", hoverinfo = "text", text = ~updated_info, hoverlabel = list(font=list(size=15))) %>% layout( xaxis=list(title="UMAP_1", zeroline=F), yaxis=list(title="UMAP_2", zeroline=F), showlegend = TRUE, legend = list(font = list(size = 14)))

system.time(print(p)) user system elapsed 3.402 0.350 3.754 `

Appreciate any help and suggestions. Thank you.

exaexa commented 3 years ago

Hi!

Is there any demo of what the above code does? I'm not sure if I get the "interactive" thing right.

You can certainly put scattermore into interactive tools (eg. with shiny) and generate plots from new data (I did that in several projects already); brushing works as with normal plots. OTOH I'm not sure scattermore will be able to display various animated contents and tooltips -- it's just a rasterizer. I've got some code for displaying the cells quickly in WebGL that might be developed to support this feature (actually a predecessor of scattermore.. :D ); if you're interested I can try to find it.

BhaktiDwivedi commented 3 years ago

Thank you for your prompt response!

Here is the link... https://bbisr.shinyapps.winship.emory.edu/ClusterViewer/. This is what I am trying to achieve. To reduce the time it takes to build and print plotly, I am displaying (pre-generated) saved html images htmlwidgets::saveWidget(as_widget(p), file=paste0("www/", sample_collection[i], "_", study_subjects[j], ".html")) for any combination of datasets. Loading html images help but still not as fast as I would have hoped.

However, for the "gene look up" option I have to build and generate the plotly scatterplot given the user-input (e.g., TP53). It is slow... and the chunk of the time goes into printing/loading the R plotly. What other alternatives I can try to speed up this R plotly code? Appreciate any suggestions. Thank you.

exaexa commented 3 years ago

How many cells (roughly) do you want to have there? That's probably a central question for performance here.

Anyway, scattermore won't help much here -- it can generate the raster to display, but there is no way to easily represent the cell origin information so that it's interpretable by the browsers to "know" where the mouse pointer is actually pointing. I recommend you try some libraries like d3.js or paper.js, or perhaps WebGL.

BhaktiDwivedi commented 3 years ago

I have about ~200,000 cells for the largest data set. I will check these libraries. Thank you for your help!

exaexa commented 3 years ago

that should be doable with some basic WebGL even interactively.

I've once implemented one similar thing (a faster remake of sleepwalk), here: http://e-x-a.org/stuff/sleepwalk.html (hover the cells and use the mousewheel) Feel free to download the code and extract the rendering javascript, or build on it, it's in public domain now.

BhaktiDwivedi commented 3 years ago

Thank you so much for sharing! Appreciate it.

exaexa commented 3 years ago

We're likely not going to have interactive scattermore, hope the links above helped a bit. Closing this for now :]

BhaktiDwivedi commented 3 years ago

It was helpful and motivating. Thank you! I just have to learn javascript/ds.js to improve my code and visualization :)

exaexa commented 3 years ago

Great! Feel free to reopen/share if you find some easy way to make it work