Return datapoints selected in a plotly scatterplot

帅比萌擦擦* 提交于 2021-02-07 04:07:52

问题


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.

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.


回答1:


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.

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)


来源:https://stackoverflow.com/questions/50765687/return-datapoints-selected-in-a-plotly-scatterplot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!