【问题标题】:Return datapoints selected in a plotly scatterplot返回在散点图中选择的数据点
【发布时间】:2018-11-18 19:30:12
【问题描述】:

我使用 ggplotly 从 ggplot2 创建了一个散点图。

我想获得一张包含选定/放大的数据点的表格,但我找不到这样做的方法。

library(ggplot2)
library(plotly)

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

在 plotly 论坛上似乎有一个类似的未回答的问题:

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

这个问题似乎也有关系:

Using Plotly with DT via crosstalk in Shiny

感谢您的帮助。

更新:

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")

仍然无法根据散点图上的点选择来更新 DataTable。

【问题讨论】:

  • shiny 可以帮助您。
  • 闪亮是实现这一目标的唯一方法吗?即是否可以只调用一个绘图函数?

标签: r datatables shiny plotly r-plotly


【解决方案1】:

plotlydocumentation 中说可以使用crosstalk 链接没有shiny 的视图。您没有提供可重现的示例,因此这里是使用 iris 数据集的示例。你可以试试:

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 更容易,但你也许可以让它工作。希望对您有所帮助。

编辑: 使用ggplotly,你可以试试这个:

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 — 一直在尝试使用 crosstalk 提出您的建议,但我的数据集似乎太大(约 15K 数据点)并且我收到一条错误消息,要求我为 DT 进行服务器端处理.我认为这必然涉及使用shiny...
  • 这是 (github.com/plotly/dash-table-experiments) 您在回答中提到的 table 功能吗?图表的使用“这个例子展示了用户在更新图表的表格中选择数据点的能力,或者反过来,在图表上选择更新表格选择的点”
  • 见这里rstudio-pubs-static.s3.amazonaws.com/…。 DT 工作时没有光泽。你试过吗:datatable(yourdataset)
  • 我有 - 这样做会返回相同的错误: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。 Viewer 只是一片空白。
  • @gaelgarcia,15,000 分是很大的。我刚刚尝试了一个 40,000 行的数据集并且它有效。我得到了同样的警告信息,但你必须等待几秒钟才能得到情节。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-08-13
  • 1970-01-01
  • 1970-01-01
  • 2011-06-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多