0

I am trying to make a dual axis plot using plotly in R. However, I am unable to name the first y-axis using the following code whicI i found here: https://plot.ly/r/multiple-axes/

    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, name = "slope of 1", yaxis = "y2") %>%
    layout(
    title = "Double Y Axis", yaxis2 = ay,
    xaxis = list(title="x")
    )

I would really appreciate it if someone could offer some advice.

MLavoie
  • 9,671
  • 41
  • 36
  • 56
Sate Ahmad
  • 27
  • 8

1 Answers1

1

Are you looking for this:

library(plotly)
library(dplyr)

ay2 <- list(
    tickfont = list(color = "red"),
    side = "left",
    title = "first y axis"
)

ay <- list(
tickfont = list(color = "red"),
overlaying = "y",
side = "right",
title = "second y axis"
)

plot_ly() %>%
    add_lines(x = ~1:3, y = ~10*(1:3), name = "slope of 10", yaxis = "y1") %>%
    add_lines(x = ~2:4, y = ~1:3, name = "slope of 1", yaxis = "y2") %>%
    layout(
        title = "Double Y Axis", yaxis2 = ay, yaxis = ay2,
        xaxis = list(title="x")
    )
MLavoie
  • 9,671
  • 41
  • 36
  • 56