简体   繁体   English

Plotly R中的活动线

[英]Movable lines in Plotly R

I have a scatter plot with numeric values on both the axis. 我有一个散点图,两个轴上都有数值。 I want to add two draggable lines, one horizontal and one vertical. 我想添加两条可拖动线,一条水平线和一条垂直线。 Also, I would like to change the color of points in the top-right quadrant formed by the two lines. 另外,我想更改由两条线形成的右上象限中的点的颜色。 I could not find any way to do this in R. 我在R中找不到任何方法可以做到这一点。

My question is similar to Horizontal/Vertical Line in plotly 我的问题在情节上类似于水平/垂直线

Two things I want to add is 我要添加的两件事是

  1. Ability to drag vertical and horizontal lines 能够拖动垂直和水平线

  2. Return values on which the two lines intersect the x and y axis, so that I can use those values as an input for another UI. 返回两条线与x和y轴相交的值,以便我可以将这些值用作另一个UI的输入。

My code sample 我的代码示例

data <- data.frame(y = sample(0:50, 100, replace = TRUE),
                  x = round(runif(100),2)
                  )
plot_ly(data, x = ~x, y = ~y)

(1) You can add draggable lines as follows: (1)您可以如下添加可拖动的行:

plot_ly(data, x = ~x, y = ~y, type = 'scatter') %>%
    layout(shapes = list(
                    #vertical line
                    list(type = "line", x0 = 0.4, x1 = 0.4, 
                            y0 = 0, y1 = 1, yref = "paper"),
                    #horizontal line
                    list(type = "line", x0 = 0, x1 = 1, 
                            y0 = 30, y1 = 30, xref = "paper"))) %>%
    # allow to edit plot by dragging lines
    config(edits = list(shapePosition = TRUE))

Similar to https://stackoverflow.com/a/54777145/5840900 类似于https://stackoverflow.com/a/54777145/5840900

(2) In an interactive environment, you can extract the values of the last dragged line with: (2)在交互式环境中,可以使用以下命令提取最后拖动的线的值:

        # Only within reactive shiny context
        newData <- plotly::event_data("plotly_relayout")

Hopefully this is still helpful! 希望这仍然有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM