Adding second axis to plotly plot without simultaneously adding trace

后端 未结 1 981
[愿得一人]
[愿得一人] 2021-01-27 17:31

Since ggplotly does not supportggplot2\'s sec.axis (Adding second Y axis on ggplotly), I want to add a second axis to the plotly-object in

相关标签:
1条回答
  • 2021-01-27 18:27

    One way to achieve this is to do what you have done and change the color of whatever you add for the second axis to "transparent", and turn off the hoverinfo and legend entry for the line :

    library(plotly)
    ay <- list(
      tickfont = list(color = "red"),
      overlaying = "y",
      side = "right",
      title = "second y axis"
    )
    p <- plot_ly() %>%
      add_lines(x = ~1:3, y = ~10*(1:3), name = "slope of 10") %>%
      add_lines(x = ~2:4, y = ~1:3, color = I("transparent"), name = "", yaxis = "y2", hoverinfo='skip', showlegend=FALSE) %>%
      layout(
        title = "Double Y Axis", yaxis2 = ay,
        xaxis = list(title="x")
      )
    p
    

    0 讨论(0)
提交回复
热议问题