添加轨迹以绘图对象R

时间:2018-09-05 18:07:33

标签: r plotly r-plotly

我有一些要使用plotly进行可视化的数据。我想定义多个y轴,并以编程方式将traces添加到绘图中。以下是我尝试过的方法(没有成功):

数据

> dput(dat)
structure(list(degc = c(0L, 10L, 20L, 25L, 30L, 40L, 50L, 60L, 
80L, 100L, 150L, 200L, 300L), pws_actual = c(0.61165, 1.2282, 
2.3393, 3.1699, 4.247, 7.3849, 12.352, 19.946, 47.414, 101.42, 
476.16, 1554.9, 8587.9), dyn_visc_actual = c(17.15, 17.64, 18.13, 
18.37, 18.6, 19.07, 19.53, 19.99, 20.88, 21.74, 23.8, 25.73, 
29.28), dyn_visc_calc = c(17.1254823894873, 17.6152140778318, 
18.0967459432547, 18.3345391853259, 18.5704029343286, 19.0364924666271, 
19.4953054096797, 19.9471170509602, 20.8307652057709, 21.6893618676326, 
23.7371944195549, 25.661567822805, 29.2075514776162), pws_calc = c(0.6116441, 
1.22821559241996, 2.33789420572142, 3.16745026377266, 4.24349046258064, 
7.38204536687166, 12.350327068321, 19.9462602568167, 47.4084299840454, 
101.415077908686, 476.125891382197, 1549.05402010988, 8620.94215288757
), rho_50 = c(1.29223717774702, 1.24659784266849, 1.20407111901879, 
1.18387691551745, 1.16434832227544, 1.12715991530121, 1.09226939924235, 
1.05946827318264, 0.999416038927741, 0.945754297809277, 0.833419607762647, 
0.743855209301603, 0.60594777741193)), .Names = c("degc", "pws_actual", 
"dyn_visc_actual", "dyn_visc_calc", "pws_calc", "rho_50"), row.names = c(NA, 
-13L), class = c("data.table", "data.frame"), sorted = "degc")

代码

library(data.table)
library(plotly)

# Define plotly object and the axes I want
plt <- plot_ly(dat) %>%
  layout(
    yaxis = list(
      showline = F,
      side = 'left',
      title = 'kPa',
      color = 'black'
    ),
    yaxis2 = list(
      showline = F,
      overlaying = 'right',
      side = 'right',
      title = 'kg/m3',
      color = 'black', 
      position = 1
    )
  )

# Function to add traces to the plot
traceFunc <- function(p, varname, axisname){
  if(missing(axisname)){
    return(
      p %>% 
             add_trace(x= ~degc, y = ~get(varname), 
                       type = 'scatter', mode = 'lines+markers', 
                       marker = list(size = 3), name = varname)
      )
  }else{
    return(
      p %>% 
        add_trace(x= ~degc, y = ~get(varname), 
                  type = 'scatter', mode = 'lines+markers', 
                  marker = list(size = 3), name = varname, yaxis = axisname)
    )
  }

}

我尝试通过以下方式调用该函数:

  1. plt %>% add_fun(traceFunc, 'pws_actual') %>% add_fun(traceFunc, 'rho_50', 'y2')
  2. traceFunc(p = plt, varname = 'pws_actual') %>% traceFunc(varname = 'rho_50', axisname = 'y2')
上面的两个方法均返回一个绘图,其中只有第二个参数rho_50实际可见(如下图所示)。仅使用呼叫的第一部分不起作用,并且得到了一个空白图。

enter image description here

这只是一个代表性的例子。我的实际数据集非常庞大,理想情况下,我想使用如下所示的内容:

cols_to_plot <- c('col1', 'col2', ... ) # 10 colums, say
col_axis <- c('y2', 'y2', ..., 'y3', ...) # axis name for each column that is plotted 
lapply(1:length(cols_to_plot), function(z){
  plt <<- plt %>% add_fun(traceFunc, cols_to_plot[z], col_axis[z])
})

我还如何在add_trace(yaxis = ...)调用中引用主y轴?

0 个答案:

没有答案