Plotly/R:创建多个矩形

Plotly/R:创建多个矩形,r,plotly,microbenchmark,R,Plotly,Microbenchmark,我有两个相当复杂的对象。对于这两者,我需要为布局和标题创建矩形。我有点困惑地发现,在plotly中没有像在ggplot2中那样的函数 我可以想出两种方法来创建这些矩形: 使用线条和线条 使用布局选项和 使用第一个解决方案,并使用split-参数分组 library(plotly) ### 0. Create and order mock-data data_line=data.frame(x=rep(1:2,2),y=rep(1:2,each=2)) data_fill=data.frame(

我有两个相当复杂的对象。对于这两者,我需要为布局和标题创建矩形。我有点困惑地发现,在
plotly
中没有像在
ggplot2
中那样的函数

我可以想出两种方法来创建这些矩形:

  • 使用线条和线条
  • 使用布局选项和
  • 使用第一个解决方案,并使用
    split
    -参数分组

    library(plotly)
    
    ### 0. Create and order mock-data
    data_line=data.frame(x=rep(1:2,2),y=rep(1:2,each=2))
    data_fill=data.frame(x=rep(1:2,2),y=rep(0.5:1.5,each=2))
    mydata=data.frame(x=rep(1:2,4),y=rep(c(1:2,0.5:1.5),each=2),
                      id=rep(letters[1:2],each=4))
    mydata<-mydata[order(mydata$y,decreasing = TRUE),] # I tried if ordering helps
    
    ### 1. Solution using lines
    plines<-function(){
      p<-plot_ly() %>% 
        add_trace(data=data_line[data_line$y==2,],x=~x,y=~y,type='scatter',mode='lines',
                  split=~y,line=list(color='rgb(0,53,153)')) %>% 
        add_trace(data=data_fill[data_fill$y==1.5,],x=~x,y=~y,type='scatter',
                  mode='lines',split=~y,line=list(color='rgb(0,53,153)'),
                  fill = 'tonexty', fillcolor='rgb(0,53,152)') %>% 
        add_trace(data=data_line[data_line$y==1,],x=~x,y=~y,type='scatter',mode='lines',
                  split=~y,line=list(color='rgb(0,53,153)')) %>% 
        add_trace(data=data_fill[data_fill$y==0.5,],x=~x,y=~y,type='scatter',mode='lines',
                  split=~y,line=list(color='rgb(0,53,153)'),fill = 'tonexty',
                  fillcolor='rgb(0,53,152)')
      return(p)
    }
    
    ### 2. solution using layout options
    playout<-function(){
      p<-plot_ly() %>% 
        add_trace(data=data_line[data_line$y==2,],x=~x,y=~y,type='scatter',mode='lines',
                  split=~y,line=list(color='rgb(0,53,153)')) %>% 
        layout(shapes=list(list(type = "rect", fillcolor = 'rgb(0,53,153)',
                                line = list(color = 'rgb(0,53,153)'),
                                x0 = 1, x1 = 2, xref = "x",
                                y0 = 0.5, y1 = 1, yref = "y"),
                           list(type = "rect",fillcolor = 'rgb(0,53,153)',
                                line = list(color = 'rgb(0,53,153)'), 
                                x0 = 1, x1 = 2, xref = "x",
                                y0 = 1.5, y1 = 2, yref = "y")))
      return(p)
    }
    
    ### 3. Solution: Grouping with split does not work
    pwish<-function(){
      p<-plot_ly() %>% 
        add_trace(data=mydata[mydata$id=='a',],x=~x,y=~y,type='scatter',mode='lines',
                  split=~y,line=list(color='rgb(0,53,153)')) %>% 
        add_trace(data=mydata[mydata$id=='b',],x=~x,y=~y,type='scatter',mode='lines',
                  split=~y,line=list(color='rgb(0,53,153)'),fill = 'tonexty',
                  fillcolor='rgb(0,53,152)')
      return(p)
    }
    
    基本上,关于基准的解释,我有两个问题:

  • 还有第四种比我没有想到的
    playout()更快的解决方案吗?
    简单的HTML或Javascript解决方案也受欢迎
  • 忽略
    pwish()
    不起作用的事实:对于较长的数据,
    playout()
    应该是最快的吗
  • pwish()
    这样的解决方案可能吗(只有当您对2.的回答为“否”时才相关)
  • 更新: 现在,我已经用更大的模拟数据集再次运行了
    microbenchmark
    ,而
    playout
    花费了大约相同的时间,而
    pwish()
    的使用时间增加了一点。因此,我现在非常确定,
    playout()
    是更有效的方法

    microbenchmark::microbenchmark(plines(),playout(),pwish(),times=1000L)
    Unit: milliseconds
         expr      min       lq      mean   median        uq      max neval
     plines() 7.294808 7.924909 10.064783 9.187790 11.836938 21.23335  1000
    playout() 6.345640 6.911258  8.818893 7.600264  9.845335 18.80933  1000
      pwish() 6.724504 7.316451  9.381053 7.990730 11.063591 25.15229  1000