返回在绘图散点图中选择的数据点

时间:2018-06-08 17:23:56

标签: r datatables shiny plotly r-plotly

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

我想获得一个选择/放大的数据点表,但我找不到办法做到这一点。

library(ggplot2)
library(plotly)

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

在剧情论坛上似乎有一个类似的悬而未决的问题:

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

这个问题似乎也有关系:

Using Plotly with DT via crosstalk in Shiny

感谢您的帮助。

enter image description here

更新

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。

enter image description here

1 个答案:

答案 0 :(得分:3)

plotly文档中,它表示可以使用shiny链接不使用crosstalk的视图。您没有提供可重现的示例,因此这里是使用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更容易,但您可以使其工作。希望它有所帮助。

enter image description here

编辑: 使用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)