返回在散点图中选择的数据点 [英] Return datapoints selected in a plotly scatterplot
问题描述
我使用ggplotly
从ggplot2创建了一个散点图.
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:
谢谢您的帮助.
更新:
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.
推荐答案
在plotly
文档中,它说可以使用crosstalk
链接不带shiny
的视图.您没有提供可重复的示例,因此这里是使用iris
数据集的示例.您可以尝试:
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
在开发版本中具有table
,但我无法在上面的示例中弄清楚如何使用它. DT
比较容易,但是您可以使它工作.希望对您有所帮助.
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.
使用ggplotly
,您可以尝试以下操作:
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)
这篇关于返回在散点图中选择的数据点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!