问题
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 plotly
documentation 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)
plotly
has in the development version a table
but I could not figure out how to use it with the example above. DT
was 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