4

I created a plotly scatterplot from a ggplot2 using ggplotly.

I would like to obtain a table of the datapoints selected / zoomed into, but I can't find a way to do this.

library(ggplot2)
library(plotly)

p <- ggplotly(plot.rel.kinship)
htmlwidgets::saveWidget(as_widget(p), "scatterplot.html")

There seems to be a similar unanswered question on the plotly forum:

https://community.plot.ly/t/get-datapoints-in-currently-zoomed-view/259

This question also seems to be related:

Using Plotly with DT via crosstalk in Shiny

Thank you for your help.

enter image description here

UPDATE:

library(crosstalk)

a <- datatable(king.kin.subset)

kinship.plotly.table <- bscols(widths = c(6, 4), p, a)

htmltools::save_html(kinship.plotly.table, "scatterplot_table.html")

Still cannot manage to update the DataTable based on the selection of points on the scatterplot.

enter image description here

Carmen Sandoval
  • 2,266
  • 5
  • 30
  • 46

1 Answers1

7

In the plotlydocumentation it says it possible to link views without shiny, using crosstalk. You did not provide a reproducible example so here is an example using the iris dataset. You could try:

library(plotly)
library(crosstalk)
library(DT)


sd <- SharedData$new(iris)

a <- plot_ly(sd, x = ~Sepal.Width, y = ~Petal.Width) %>% 
  add_markers(alpha = 0.5) %>%
  highlight("plotly_selected", dynamic = TRUE)


options(persistent = TRUE)

p <- datatable(sd)

bscols(widths = c(6, 4), a, p)

plotlyhas in the development version a tablebut I could not figure out how to use it with the example above. DTwas easier but you might be able to make it work. Hope it helps.

enter image description here

EDIT: With ggplotly, you can try this:

d <- highlight_unit(iris)
a <- ggplotly(ggplot(data = d, aes(x = Sepal.Width, y = Petal.Width)) + geom_point()) %>%
  highlight("plotly_selected", dynamic = TRUE)

options(persistent = TRUE)

p <- datatable(d)

bscols(widths = c(6, 4), a, p)
MLavoie
  • 9,671
  • 41
  • 36
  • 56
  • Thanks @MLavoie — Been trying your suggestion using `crosstalk`, but it appears my dataset is too large (~15K datapoints) and I get an error message asking me to do server-side processing for DT. I think this necessarily involves using `shiny`... – Carmen Sandoval Jun 10 '18 at 05:02
  • Is this (https://github.com/plotly/dash-table-experiments) the `table` feature you refer to in your answer? Usage with Graphs "This example demonstrates the user's ability to select data points either in the table which updates the plot, or in the reverse, select points on the graph which updates the selection on the table" – Carmen Sandoval Jun 10 '18 at 05:28
  • see here https://rstudio-pubs-static.s3.amazonaws.com/327675_c673c4e54c7a4efd96e72f295854c32b.html. DT works without shiny. Have you tried: `datatable(yourdataset)`? – MLavoie Jun 10 '18 at 09:27
  • 1
    I have — doing so returns the same error: `Warning message: In instance$preRenderHook(instance) : It seems your data is too big for client-side DataTables. You may consider server-side processing: https://rstudio.github.io/DT/server.html` . The Viewer simply goes blank. – Carmen Sandoval Jun 11 '18 at 02:47
  • @gaelgarcia, 15,000 points that is big. I just tried with a 40,000-rows dataset and it worked. I got the same warning message, but you have to wait a few seconds to get the plot. – MLavoie Jun 11 '18 at 10:06
  • Thanks @MLavoie — I've been trying with a subset of the data, and the DataTable now works and is placed alongside the plot using your suggestion. However, I can't get the DataTable to update based on what is selected on the plot. I think I am missing something simple ... ? Please see update Thanks again. – Carmen Sandoval Jun 11 '18 at 13:14
  • @gaelgarcia, You don't really provide a reproducible example, so it is hard to tell. But have you run this `sd <- SharedData$new(iris)` from my example. `a` and `p` need to share the same dataset. – MLavoie Jun 11 '18 at 15:32
  • I see. Yeah, sorry about the reproducible data... it's hard to provide it when working with clinical samples :( — Do you think it is possible to implement the `sharedData` line if I'm actually making the plotly object via `ggplotly`? – Carmen Sandoval Jun 11 '18 at 15:42
  • Is `highlight_unit` a function in the dev version of plotly? I'm getting an error message saying `Error in highlight_unit() : could not find function "highlight_unit"`. – Carmen Sandoval Jun 13 '18 at 19:34
  • @gaelgarcia, I am working with the development version of plotly. – MLavoie Jun 13 '18 at 19:39
  • How can the selected data be saved (not just displayed)? – user2955884 Jan 25 '23 at 16:16