在 plot_ly 中覆盖散点区域

Bar*_*nik 5 r shiny plotly

我想在一个图上绘制图表并绘制填充区域形状和条形图。然而,区域形状覆盖条。我无法更改元素的顺序。可以把酒吧带在前面吗?

在此处输入图片说明

data <- data.frame(years = c(2005,2006,2007), values1 = c(1,2,3), values2 = c(3,3,2))

plot_ly(data, x = data$years, y=data$values1, type = 'bar') %>% 
  add_trace(x=data$years, y=data$values2, type = 'scatter', mode = 'lines', fill = 'tozeroy')
Run Code Online (Sandbox Code Playgroud)

YBS*_*YBS 1

这是改编自@Maximilian Peters 的答案。这段代码

data <- data.frame(years = c(2005,2006,2007), values1 = c(1,2,3), values2 = c(3,3,2))
  
  plot_ly(data) %>%
    add_trace(x=~years, y=~values1, type = 'bar') %>%
    add_trace( x = ~years, y=~values2, type = 'scatter', mode = 'lines', fill = 'tozeroy', yaxis='y2'
    ) %>%
    layout(title = 'Trace order Plotly R',
           xaxis = list(title = ""),
           yaxis = list(side = 'left', title = 'Y - Axis', overlaying = "y2"),
           yaxis2 = list(side = 'right', title = "" )
    )
Run Code Online (Sandbox Code Playgroud)

生成此输出:

输出