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

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

how to get the coordinates of the drawn figure in plotly

问题

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

library(plotly)

x <- 1:50
y <- rnorm(50)

fig <- plot_ly(x = ~x, y = ~y, type = 'scatter', mode = 'lines')
fig <- config(fig, modeBarButtonsToAdd = list('drawrect'))

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.

library(plotly)

x &lt;- 1:50
y &lt;- rnorm(50)

fig &lt;- plot_ly(x = ~x, y = ~y, type = &#39;scatter&#39;, mode = &#39;lines&#39;)
fig &lt;- config(fig,modeBarButtonsToAdd = list(&#39;drawrect&#39;))

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事件。

库(shiny)
库(plotly)

ui <- fluidPage(
  radioButtons("plotType", "绘图类型:", choices = c("ggplotly", "plotly")),
  plotlyOutput("plot"),
  verbatimTextOutput("relayout")
)

服务器 <- function(input, output, session) {
  
  nms <- row.names(mtcars)
  
  output$plot <- renderPlotly({
    p <- if (identical(input$plotType, "ggplotly")) {
      ggplotly(
        ggplot(mtcars, aes(x = mpg, y = wt, customdata = nms)) + geom_point()
      )
    } else {
      plot_ly(mtcars, x = ~mpg, y = ~wt, customdata = nms)
    }
    p %>% 
      layout(dragmode = "select") %>%
      config(modeBarButtonsToAdd = list("drawrect"))
  })
  
  output$relayout <- renderPrint({
    d <- event_data("plotly_relayout")
    if (is.null(d)) "矩形坐标将出现在这里" else d
  })
  
}

shinyApp(ui, 服务器)
英文:

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

library(shiny)
library(plotly)

ui &lt;- fluidPage(
  radioButtons(&quot;plotType&quot;, &quot;Plot Type:&quot;, choices = c(&quot;ggplotly&quot;, &quot;plotly&quot;)),
  plotlyOutput(&quot;plot&quot;),
  verbatimTextOutput(&quot;relayout&quot;)
)

server &lt;- function(input, output, session) {
  
  nms &lt;- row.names(mtcars)
  
  output$plot &lt;- renderPlotly({
    p &lt;- if (identical(input$plotType, &quot;ggplotly&quot;)) {
      ggplotly(
        ggplot(mtcars, aes(x = mpg, y = wt, customdata = nms)) + geom_point()
      )
    } else {
      plot_ly(mtcars, x = ~mpg, y = ~wt, customdata = nms)
    }
    p %&gt;% 
      layout(dragmode = &quot;select&quot;) %&gt;%
      config(modeBarButtonsToAdd = list(&quot;drawrect&quot;))
  })
  
  output$relayout &lt;- renderPrint({
    d &lt;- event_data(&quot;plotly_relayout&quot;)
    if (is.null(d)) &quot;Rectangle coordinates will appear here&quot; else d
  })
  
}

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:

确定