绘制R中的跟踪层

时间:2019-01-04 16:46:30

标签: r plotly layer

我将plotly用于混合散点图和条形图。

示例代码

sample_tme <- seq.POSIXt(from = Sys.time(), to = Sys.time() + days(7), by = 'hour')
df <- data.frame(tme = sample_tme,
                 temp1 = runif(n = length(sample_tme), min = -15,max = 10 ),
                 temp2 = runif(n = length(sample_tme), min = -20,max = 12 ),
                 bar1 = runif(n = length(sample_tme), min = 0, max = 15 ))
plt1 <- plot_ly(data = df) %>% 
  layout(
    title = 'sample plot',
    xaxis = list(title = 'Time',
                 tickfont = list(color = 'black')), 
    margin = list(pad = 50, b = 90, l = 150, r = 90), 
    yaxis = list(
      showline = F,
      side = 'left',
      title = 'Temperature (C)',
      color = 'black'
    ),
    yaxis2 = list(
      tickfont = list(color = "black"),
      showline = F,
      overlaying = "y",
      side = "right",
      title = "Heat to be added", zeroline = F,
      anchor = 'free'
    )
  ) %>% 
  add_trace(x = ~sample_tme, y = ~bar1, type = 'bar', name = 'Heat to be added',
            marker = list(color = '#C9EFF9', opacity = 0.8), yaxis = 'y2') %>%
  add_trace(x = ~sample_tme, y = ~temp1, name = 'Ambient temperature', mode = 'lines', type = 'scatter', visible = 'legendonly') %>%
  add_trace(x = ~sample_tme, y = ~temp2, name = 'Dew-point temperature', mode = 'lines', type = 'scatter')

输出图具有隐藏线图的条形图。有没有一种方法可以使条形图向后移动轨迹,从而使线图更清晰可见?

enter image description here

类似于在密谋网站上发布的example的内容。我尝试了不同的顺序来定义traces,而结果没有差异。这可能与overlaying声明中的yaxis2参数有关吗?在我的实际绘图中,有7条曲线,我似乎无法修复。

0 个答案:

没有答案