怎样获取在Plotly中绘制图形的坐标。

huangapple go评论98阅读模式
英文:

how to get the coordinates of the drawn figure in plotly

问题

我想要将绘制的矩形的坐标存储在一个单独的变量中,同时如果我调整矩形的大小,也希望坐标能够随之改变。

  1. library(plotly)
  2. x <- 1:50
  3. y <- rnorm(50)
  4. fig <- plot_ly(x = ~x, y = ~y, type = 'scatter', mode = 'lines')
  5. fig <- config(fig, modeBarButtonsToAdd = list('drawrect'))
  6. fig

这里有一个非常相似的问题,但是那些解决方案对我无效,这些解决方案可能已经过时了。

英文:

I would like to get the coordinates of the drawn rectangle in a separate variable, also I would like the coordinates to change if I resize the rectangle.

  1. library(plotly)
  2. x &lt;- 1:50
  3. y &lt;- rnorm(50)
  4. fig &lt;- plot_ly(x = ~x, y = ~y, type = &#39;scatter&#39;, mode = &#39;lines&#39;)
  5. fig &lt;- config(fig,modeBarButtonsToAdd = list(&#39;drawrect&#39;))
  6. fig

怎样获取在Plotly中绘制图形的坐标。

There is a very similar question, but none of the solutions worked for me, those solutions must be outdated by now

答案1

得分: 2

你必须在Shiny应用程序中使用plotly_relayout事件。

  1. 库(shiny)
  2. 库(plotly)
  3. ui <- fluidPage(
  4. radioButtons("plotType", "绘图类型:", choices = c("ggplotly", "plotly")),
  5. plotlyOutput("plot"),
  6. verbatimTextOutput("relayout")
  7. )
  8. 服务器 <- function(input, output, session) {
  9. nms <- row.names(mtcars)
  10. output$plot <- renderPlotly({
  11. p <- if (identical(input$plotType, "ggplotly")) {
  12. ggplotly(
  13. ggplot(mtcars, aes(x = mpg, y = wt, customdata = nms)) + geom_point()
  14. )
  15. } else {
  16. plot_ly(mtcars, x = ~mpg, y = ~wt, customdata = nms)
  17. }
  18. p %>%
  19. layout(dragmode = "select") %>%
  20. config(modeBarButtonsToAdd = list("drawrect"))
  21. })
  22. output$relayout <- renderPrint({
  23. d <- event_data("plotly_relayout")
  24. if (is.null(d)) "矩形坐标将出现在这里" else d
  25. })
  26. }
  27. shinyApp(ui, 服务器)
英文:

You have to use the plotly_relayout event in a Shiny app.

  1. library(shiny)
  2. library(plotly)
  3. ui &lt;- fluidPage(
  4. radioButtons(&quot;plotType&quot;, &quot;Plot Type:&quot;, choices = c(&quot;ggplotly&quot;, &quot;plotly&quot;)),
  5. plotlyOutput(&quot;plot&quot;),
  6. verbatimTextOutput(&quot;relayout&quot;)
  7. )
  8. server &lt;- function(input, output, session) {
  9. nms &lt;- row.names(mtcars)
  10. output$plot &lt;- renderPlotly({
  11. p &lt;- if (identical(input$plotType, &quot;ggplotly&quot;)) {
  12. ggplotly(
  13. ggplot(mtcars, aes(x = mpg, y = wt, customdata = nms)) + geom_point()
  14. )
  15. } else {
  16. plot_ly(mtcars, x = ~mpg, y = ~wt, customdata = nms)
  17. }
  18. p %&gt;%
  19. layout(dragmode = &quot;select&quot;) %&gt;%
  20. config(modeBarButtonsToAdd = list(&quot;drawrect&quot;))
  21. })
  22. output$relayout &lt;- renderPrint({
  23. d &lt;- event_data(&quot;plotly_relayout&quot;)
  24. if (is.null(d)) &quot;Rectangle coordinates will appear here&quot; else d
  25. })
  26. }
  27. shinyApp(ui, server)

怎样获取在Plotly中绘制图形的坐标。

huangapple
  • 本文由 发表于 2023年5月22日 18:30:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76305271.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定