gpt4 book ai didi

r - 如何从 R 绘图图中获取坐标

转载 作者:行者123 更新时间:2023-12-04 09:17:15 33 4
gpt4 key购买 nike

我一直在拼命地解决一个看似基本的问题。
想象一下,您有一个散点图,例如…… 10 个标记。
我想这个图是在 Shiny 环境中使用 plotly 生成的。

可以使用 event_data("plotly_click") 轻松获得这些标记的坐标。代码。

现在假设您不需要这些标记的坐标,而是通过鼠标单击生成的坐标,但恰好在不存在标记的地方(例如,因为您想在那里设置一个新标记,并且您想重新使用来自鼠标点击的信息)。

我无法使用 onclick() 获得这样的行为, 管他呢。

任何的想法 ?

最佳答案

您可以在 plotly 中添加 D3 事件监听器

Plotly.d3.select('.plotly').on('click', function(d, i) {})

进而
  • 根据点击位置检索相对的 x 和 y 值( d3.event.layerXlayerY )
  • 调整相对图形位置 ( document.getElementsByClassName('bg')[0].attributes['x'] )
  • 最后根据轴范围 ( myPlot.layout.xaxis.range[0] )
  • 计算新值

    新款 xy然后将值推送到现有图形
    Plotly.extendTraces(myPlot, {
    x: [[x]],
    y: [[y]]
    }, [1]);

    完整的R代码
    library("plotly")
    library("htmlwidgets")

    p <- plot_ly(x = c( -2, 0, 2 ),y = c( -2, 1, 2), type = 'scatter' ,mode = 'lines+markers') %>%
    add_trace(x=c(-1,0.4,2),y=c(2, 0, -1),type='scatter',mode='lines+markers') %>%
    layout(hovermode='closest')
    javascript <- "
    var myPlot = document.getElementsByClassName('plotly')[0];
    Number.prototype.between = function (min, max) {
    return this >= min && this <= max;
    };

    Plotly.d3.select('.plotly').on('click', function(d, i) {
    var e = Plotly.d3.event;
    var bg = document.getElementsByClassName('bg')[0];
    var x = ((e.layerX - bg.attributes['x'].value + 4) / (bg.attributes['width'].value)) * (myPlot.layout.xaxis.range[1] - myPlot.layout.xaxis.range[0]) + myPlot.layout.xaxis.range[0];
    var y =((e.layerY - bg.attributes['y'].value + 4) / (bg.attributes['height'].value)) * (myPlot.layout.yaxis.range[0] - myPlot.layout.yaxis.range[1]) + myPlot.layout.yaxis.range[1]
    if (x.between(myPlot.layout.xaxis.range[0], myPlot.layout.xaxis.range[1]) &&
    y.between(myPlot.layout.yaxis.range[0], myPlot.layout.yaxis.range[1])) {
    Plotly.extendTraces(myPlot, {
    x: [[x]],
    y: [[y]]
    }, [1]);
    }
    });"
    p <- htmlwidgets::prependContent(p, onStaticRenderComplete(javascript), data=list(''))
    p

    交互式 Javascript 示例

    var traces = [{
    x: [1, 2, 3, 4],
    y: [10, 15, 13, 17],
    mode: 'markers',
    type: 'scatter'
    }];

    traces.push({
    x: [2, 3, 4, 5],
    y: [16, 5, 11, 9],
    mode: 'markers',
    type: 'scatter'
    });

    traces.push({
    x: [1, 2, 3, 4],
    y: [12, 9, 15, 12],
    mode: 'markers',
    type: 'scatter'
    });

    traces.push({
    x: [],
    y: [],
    mode: 'lines+markers',
    type: 'scatter'
    });

    var myPlot = document.getElementById('myPlot')
    Plotly.newPlot('myPlot', traces, {hovermode: 'closest'});

    Number.prototype.between = function(min, max) {
    return this >= min && this <= max;
    };

    Plotly.d3.select(".plotly").on('click', function(d, i) {
    var e = Plotly.d3.event;
    var bg = document.getElementsByClassName('bg')[0];
    var x = ((e.layerX - bg.attributes['x'].value + 4) / (bg.attributes['width'].value)) * (myPlot.layout.xaxis.range[1] - myPlot.layout.xaxis.range[0]) + myPlot.layout.xaxis.range[0];
    var y = ((e.layerY - bg.attributes['y'].value + 4) / (bg.attributes['height'].value)) * (myPlot.layout.yaxis.range[0] - myPlot.layout.yaxis.range[1]) + myPlot.layout.yaxis.range[1]
    if (x.between(myPlot.layout.xaxis.range[0], myPlot.layout.xaxis.range[1]) &&
    y.between(myPlot.layout.yaxis.range[0], myPlot.layout.yaxis.range[1])) {
    Plotly.extendTraces(myPlot, {
    x: [
    [x]
    ],
    y: [
    [y]
    ]
    }, [3]);
    }
    });
    <script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
    <div id="myPlot" style="width:100%;height:100%"></div>



    Shiny 的例子
    library(shiny)
    library("plotly")
    library("htmlwidgets")

    ui <- fluidPage(
    plotlyOutput("plot")
    )

    server <- function(input, output) {
    javascript <- "
    function(el, x){
    Number.prototype.between = function (min, max) {
    return this >= min && this <= max;
    };

    Plotly.d3.select('.plotly').on('click', function(d, i) {
    var e = Plotly.d3.event;
    var bg = document.getElementsByClassName('bg')[0];
    var x = ((e.layerX - bg.attributes['x'].value + 4) / (bg.attributes['width'].value)) * (el.layout.xaxis.range[1] - el.layout.xaxis.range[0]) + el.layout.xaxis.range[0];
    var y =((e.layerY - bg.attributes['y'].value + 4) / (bg.attributes['height'].value)) * (el.layout.yaxis.range[0] - el.layout.yaxis.range[1]) + el.layout.yaxis.range[1]
    if (x.between(el.layout.xaxis.range[0], el.layout.xaxis.range[1]) && y.between(el.layout.yaxis.range[0], el.layout.yaxis.range[1])) {
    Plotly.extendTraces(el, {
    x: [[x]],
    y: [[y]]
    }, [1]);
    }
    });
    }"
    output$plot <- renderPlotly({
    plot_ly(x = c( -2, 0, 2 ),y = c( -2, 1, 2), type = 'scatter' ,mode = 'lines+markers') %>%
    add_trace(x=c(-1,0.4,2),y=c(2, 0, -1),type='scatter',mode='lines+markers') %>%
    layout(hovermode='closest') %>% onRender(javascript)
    })
    }

    shinyApp(ui = ui, server = server)

    关于r - 如何从 R 绘图图中获取坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46157790/

    33 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com