根据坐标值(而不是像素)设置标记大小,以R为单位 [英] Set marker size based on coordinate values, not pixels, in plotly R

查看:91
本文介绍了根据坐标值(而不是像素)设置标记大小,以R为单位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此帖子-在绘图中设置标记大小-不幸的是,这并没有帮助我一直在寻找,并且是该主题中我唯一能找到的帖子.根据散点图的size参数的可打印文档:

This post - Set marker size in plotly - unfortunately did not help with what I was looking for, and is the only post on the topic I could find. Per the plotly documentation on the size parameter for scatter plots:

大小(大于或等于0的数字或数字数组),默认值:6,设置标记大小(以px为单位)"

"size (number or array of numbers greater than or equal to 0), default: 6, Sets the marker size (in px)"

(以像素为单位)对我来说是个问题.我想创建一个绘图,其中标记的大小是基于坐标值而不是像素的,这样我就可以增加图形的大小(例如,通过完全屏蔽它),并且点的大小也要增加.当前我的代码示例:

the (in px) is a problem for me. I'd like to create a plot where the marker sizes are based on the coordinate values, not pixels, that way I can increase the size of a graph (by full screening it, for example) and the points increase in size as well. An example of my code currently:

library(plotly)
mydf <- data.frame(x = rep(1:20, times = 20), y = rep(1:20, each = 20),
                   thesize = 10)
plot_ly(mydf) %>%
  add_trace(x = ~x, y = ~y, type = 'scatter', mode = 'markers', 
            marker = list(symbol = 'hexagon', size = ~thesize, opacity = 0.6))

如果您在R中创建此图,然后通过拖动Rstudio查看器窗口或通过其他方式使图变大或变小,您会注意到标记保持完全相同(10像素),这令人沮丧.如果我可以让这些标记的直径== 1(在x轴上)而不是设置为多个像素,我会很喜欢.这可能吗?

If you create this plot in R, and then make the plot either larger or smaller by dragging the Rstudio viewer window, or in some other way, you'll notice that the markers stay exactly the same size (10 pixels), which is frustrating. I would love it if I could have these markers have diameter == 1 (on the x axis), rather than be set to a number of pixels. Is this possible?

我们非常感谢您的帮助!

Any help is super appreciated!!!

推荐答案

  • 让我们从定义轴范围的图开始.

    • Let's start with a plot with a defined axis range.

      p <- plot_ly() %>% layout(xaxis = list(range = c(1, 5)))
      

    • 现在添加一个JavaScript事件监听器,以捕获绘图布局中的任何更改

    • Now add a JavaScript eventlistener which captures any changes in the plot's layout

      javascript <- "
      var myPlot = document.getElementsByClassName('plotly')[0];
      
      function resize(eventdata) {
        // magic happens here
      }
      
      myPlot.on('plotly_relayout', function(eventdata) {
        resize(eventdata);
      });
      "
      p <- htmlwidgets::prependContent(p, 
                                       onStaticRenderComplete(javascript))
      p
      

    • 该事件传递了eventdata,我们可以从中获取新的轴范围.

    • The event passes eventdata from where we can get the new axis range.

      eventdata['xaxis.range[1]']
      

    • 由于我们事先不知道绘图的大小,因此我们通过轴线的大小来确定

    • Since we don't know the size of the plot in advance, we determine it via the size of the axis line

      var plot_width = Plotly.d3.select('.xlines-above').node().getBBox()['width'];
      

    • 我们需要手动调用一次该事件,以确保正确初始化绘图

    • We need to call the event once manually to make sure that plot is correctly initialized

      初始图

      放大

      完整的R代码

      library("plotly")
      library("htmlwidgets")
      
      p <- plot_ly() %>%
        add_trace(x = c(1.5, 3, 4.2),
                  y = c(-2, 1, 2), 
                  type = 'scatter',
                  mode = 'lines+markers',
                  showlegend = F) %>% 
        add_trace(x = c(2, 3, 4.2),
                  y = c(2, 0, -1),
                  type = 'scatter',
                  mode = 'lines+markers',
                  showlegend = F) %>%
        add_trace(x = c(1.5, 3, 4.2),
                  y = c(1, 0.5, 1),
                  type = 'scatter',
                  mode = 'markers',
                  showlegend = F) %>% 
        layout(xaxis = list(range = c(1, 5)))
      javascript <- "
      marker_size = 0.5; // x-axis units
      var myPlot = document.getElementsByClassName('plotly')[0];
      
      function resize(eventdata) {
        var xaxis_stop = 5;
        var xaxis_start = 1;
        var plot_width = Plotly.d3.select('.xlines-above').node().getBBox()['width'];
        if (eventdata['xaxis.range[1]'] !== undefined) {
          var update = {'marker.size': marker_size * plot_width / (eventdata['xaxis.range[1]'] - eventdata['xaxis.range[0]'])};
        } else {
          var update = {'marker.size': marker_size * plot_width / (xaxis_stop - xaxis_start)};
        }
        Plotly.restyle(myPlot, update, 2);
      }
      resize({eventdata: {}});
      
      myPlot.on('plotly_relayout', function(eventdata) {
        resize(eventdata);
      });
      "
      p <- htmlwidgets::prependContent(p, 
                                       onStaticRenderComplete(javascript))
      p
      

      交互式JavaScript示例

      • 您可以从图形的定义的高度/宽度和定义的轴范围开始,这样您将知道一个轴单位有多少像素.
      • 标记的大小最初将是您想要的大小.
      • 每当用户更改轴范围时,都会触发一个plotly_relayout事件,您可以从xaxis.range[1](开始)和xaxis.range[1](结束)中的eventdata检索新范围
      • 根据新范围,您可以relayout标记大小.
      • You could start with a defined height/width for your graph and a defined axis range, this way you would know how many pixels is one axis unit.
      • The size of your markers would be initially in the size you want.
      • Whenever a user changes the axis range, a plotly_relayout event will be triggered and you can retrieve the new ranges from the eventdata in xaxis.range[1] (start) and xaxis.range[1] (end)
      • Based on the new ranges you can relayout your marker sizes.

      var plot_width = 500;
      var plot_height = 500;
      var margin_l = 100;
      var margin_r = 100;
      marker_size = 0.5; // x-axis units
      xaxis_start = 1;
      xaxis_stop = 5;
      
      var trace1 = {
        x: [1.5, 2, 3, 4],
        y: [10, 15, 13, 17],
        mode: "markers",
        marker: {
          size: marker_size *
            (plot_width - margin_l - margin_r) /
            (xaxis_stop - xaxis_start)
        },
        showlegend: false
      };
      
      var trace2 = {
        x: [2, 3, 4, 4.5],
        y: [16, 5, 11, 10],
        mode: "lines"
      };
      
      var trace3 = {
        x: [1.5, 2, 3, 4],
        y: [12, 9, 15, 12],
        mode: "lines+markers",
        showlegend: false
      };
      
      var data = [trace1, trace2, trace3];
      
      var layout = {
        width: plot_width,
        height: plot_height,
        margin: {
          l: margin_l,
          r: margin_r
        },
        xaxis: {
          range: [1, 5]
        },
        showlegend: false
      };
      
      Plotly.newPlot("myDiv", data, layout);
      
      var refplot = document.getElementById("myDiv");
      refplot.on("plotly_relayout", function(eventdata) {
        if (eventdata["xaxis.range[1]"] !== undefined) {
          var update = {
            "marker.size": marker_size *
              (plot_width - margin_l - margin_r) /
              (eventdata["xaxis.range[1]"] - eventdata["xaxis.range[0]"])
          };
        } else {
          var update = {
            "marker.size": marker_size *
              (plot_width - margin_l - margin_r) /
              (xaxis_stop - xaxis_start)
          };
        }
        Plotly.restyle("myDiv", update, 0);
      });

      <head>
        <script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
      </head>
      
      <body>
        <div id="myDiv"></div>
      </body>

      这篇关于根据坐标值(而不是像素)设置标记大小,以R为单位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆