Subplots with 2 y axes not working

I’m using the subplot function to plot multiple graphs according to some by variables. I would like to have an additional yaxis (right side) for my subplots. The subplots without the 2nd yaxis works for me:

However, when I add the additional yaxis, the result is not what I expected. I’m using plotly with shiny, where users are allowed to change the columns they would like to chart. But for this example, I’ll hard code the columns used to make it simpler. Here’s my code:

plotChart <- data.frame(FISCAL_WEEK = c('2016-48','2016-49','2016-50','2016-48','2016-49','2016-50','2016-48','2016- 49','2016-50','2016-48','2016-49','2016-50'), MOTOR_VEND = c('8','8','8','P','P','P','8','8','8','P','P','P'), DRIVE_HSA_REV = c('R','R','R','R','R','R','P','P','P','P','P','P'),RTPAD_P1 = c(24.94,23,31,3.84,24.4,17,6.74,8,7,3,6.68,4), RTPAD_P5 = c(45,45,34.25,31,44,31,12,15,16,11.4,14,10))

plotChart$ID <- as.integer(interaction(plotChart$MOTOR_VEND,plotChart$DRIVE_HSA_REV))
plotChart <- as.data.table(plotChart)

ax <- list( showticklabels = TRUE,
            title="FISCAL_WEEK"
            ticks = 'outside', 
            showgrid = TRUE)

ay1 <- list(showticklabels = TRUE,
            title="RTPAD_P1",
            type = 'linear',
            ticks = 'outside',
            showgrid = TRUE)

ay2 <- list(
            type = 'linear',
            showticklabels = TRUE,
            overlaying = "y",
            anchor="x",
            autorange = FALSE,
            rangemode = "normal",
            range = c(min(plotChart$RTPAD_P5),max(plotChart$RTPAD_P5)),
            title="RTPAD_P5",
            side = "right"
)

plot_ly(plotChart,
        x = FISCAL_WEEK,
        y = RTPAD_P1,
        type = 'scatter',
        group = interaction(MOTOR_VEND,DRIVE_HSA_REV),
        xaxis = paste0('x', ID)
        ) %>%

add_trace(x = plotChart$FISCAL_WEEK, 
          y = plotChart$RTPAD_P5,
          type = 'scatter',
          yaxis = "y2") %>%
  
  
layout(autosize = T,
       legend = list(x = 1.1, y = 1),
       showlegend = TRUE,
       xaxis = ax,
       yaxis = ay1,
       yaxis2 = ay2
       ) %>% 

subplot(nrows=4, margin = 0.025)

The above code produces a jumbled up plot with the overlays not done properly. But the subplots work if we remove the 2nd y axis (need to remove the list ay, the add_trace() code and the yaxis2 option in layout().

Interestingly, if I’m plotting only 1 chart, I managed to get the yaxis2 up:

plotChart1 <- plotChart[plotChart$ID == 1,]

ax <- list( showticklabels = TRUE,
            title="FISCAL_WEEK"
            ticks = 'outside', 
            showgrid = TRUE)

ay1 <- list(showticklabels = TRUE,
            title="RTPAD_P1",
            type = 'linear',
            ticks = 'outside',
            showgrid = TRUE)

ay2 <- list(
            type = 'linear',
            showticklabels = TRUE,
            overlaying = "y",
            anchor="x",
            autorange = FALSE,
            rangemode = "normal",
            range = c(min(plotChart1$RTPAD_P5),max(plotChart1$RTPAD_P5)),
            title="RTPAD_P5",
            side = "right"
)

plot_ly(plotChart1,
        x = FISCAL_WEEK,
        y = RTPAD_P1,
        type = 'scatter'
        ) %>%

add_trace(x = plotChart1$FISCAL_WEEK, 
          y = plotChart1$RTPAD_P5,
          type = 'scatter',
          yaxis = "y2") %>%
  
  
layout(autosize = T,
       legend = list(x = 1.1, y = 1),
       showlegend = TRUE,
       xaxis = ax,
       yaxis = ay1,
       yaxis2 = ay2
       )

Is there a way to get both functionalities working together? That is using subplots to have multiplots according to the by variables, plus having an additional trace with a second y axis.

Hello,
I have the same problem.
Did you find a solution ?
Thanks
Rebecca