如何在R中的Plotly中的每个子图中保留额外的轨迹?它们都在顶部子图中

cygmwpex  于 6个月前  发布在  其他
关注(0)|答案(1)|浏览(75)

我尝试在R中使用plotly和子图功能来绘制流量和浓度的时间序列。我有几个水质参数,我想有,例如,三个子图堆叠在一起,在每个子图中有流量的时间序列,以及感兴趣的水质参数的时间序列。为此,我写了一个简单的绘图函数,它同时接受流量和浓度。当我使用下面的代码时,它看起来像每个子图中的第一个轨迹(在我的情况下,流量)正确地绘制在每个子图中,但所有额外的轨迹都最终出现在顶部子图中,以及它们的次轴参数.显然,必须有一种方法来使用layout()将额外的轨迹分配给适当的子图。2如果你有任何建议,我很感兴趣!
此外,如果你有建议,有一个传说为每个子情节,我也很感兴趣.我已经看到的帖子,但它仍然不清楚如何为我做到这一点.谢谢!

time <- c(1:7)
flow <- c(10,9,8,6,6,7,8.5)

NO3 <- c(5,6,6,5,4,3,4)

NH4 <- c(0.2,0.3,0.4,0.6,1,0.9,0.8)

TSS <- c(50,45,40,43,42,41,40)

data <- as.data.frame(cbind(time,flow,NO3,NH4,TSS))

Special_plot <- function(data, parameter, rangeQ, ConcRange){ 
  
    fig <- plot_ly(data, x = ~time)
  
    fig <- fig %>% add_trace(type = 'scatter',
 
                                 y = ~flow,
 
                                 name = 'Flow',
 
                                 mode = 'lines',
 
                                 line = list (color = "#1f77b4"),
 
                                 showlegend = TRUE)
  
    fig <- fig %>% add_trace(type = 'scatter',
                           y = ~data[,parameter],
 
                              name = parameter,
 
                              mode = 'lines' , yaxis = 'y2',
  
                                 showlegend = TRUE)
  
    fig <- fig %>% layout( yaxis = list(title = 'Flow rate', range = rangeQ),
  
                       yaxis2 = list(title = 'Concentration', 
                        range = ConcRange, overlaying = 'y', 
                        side = 'right', automargin = TRUE))

return(fig)

}

rangeQ = c(2,12)



fig1 <- Special_plot(data, "NO3",rangeQ, c(2,7)) 
fig2 <- Special_plot(data, "NH4",rangeQ, c(0,1.5))  

fig3 <- Special_plot(data, "TSS",rangeQ, c(0,50)) 

fig <- subplot(fig1,fig2,fig3,nrows = 3, shareX = TRUE)
fig

字符串

kcrjzv8t

kcrjzv8t1#

我在这篇文章中找到了我问题的答案
所以,在上面的代码中,我在函数中添加了另一个变量Yaxis。根据上面的帖子,所有需要的就是将overlaying = "y"更改为overlaying = Yaxis。两个子图的变量,顶部的变量将为Yaxis = 'y',第二个变量为Yaxis = 'y3'。对于三个子图,第三个子图将有Yaxis = 'y5'。列表继续下去,每一个额外的子图Yaxis将采取y7y9等。我不明白为什么它的工作,我找不到in this ebook
所以看起来有效的代码是:

time <- c(1:7)
flow <- c(10,9,8,6,6,7,8.5)

NO3 <- c(5,6,6,5,4,3,4)

NH4 <- c(0.2,0.3,0.4,0.6,1,0.9,0.8)

TSS <- c(50,45,40,43,42,41,40)

data <- as.data.frame(cbind(time,flow,NO3,NH4,TSS))

Special_plot <- function(data, parameter, rangeQ, ConcRange, Yaxis){ 
  
  fig <- plot_ly(data, x = ~time)
  
  fig <- fig %>% add_trace(type = 'scatter',
                           
                           y = ~flow,
                           
                           name = 'Flow',
                           
                           mode = 'lines',
                           
                           line = list (color = "#1f77b4"),
                           
                           showlegend = TRUE)
  
  fig <- fig %>% add_trace(type = 'scatter',
                           y = ~data[,parameter],
                           
                           name = parameter,
                           
                           mode = 'lines' , yaxis = 'y2',
                           
                           showlegend = TRUE)
  
  fig <- fig %>% layout( yaxis = list(title = 'Flow rate', range = rangeQ),
                         
                         yaxis2 = list(title = 'Concentration', 
                                       range = ConcRange, overlaying = Yaxis, 
                                       side = 'right', automargin = TRUE))
  
  return(fig)
  
}

rangeQ = c(2,12)



fig1 <- Special_plot(data, "NO3",rangeQ, c(2,7),'y') 
fig2 <- Special_plot(data, "NH4",rangeQ, c(0,1.5),'y3')  

fig3 <- Special_plot(data, "TSS",rangeQ, c(0,50),'y5') 

fig <- subplot(fig1,fig2,fig3,nrows = 3, shareX = TRUE)
fig

字符串
我还没有解决传奇的问题。

相关问题