plotly multiple plot facet

13,643

You are looking for subplot. Check this page for more

library(plotly)
ay <- list(
    tickfont = list(color = "green"),
    overlaying = "y",
    side = "right", title = "y2 axis title"
)

ax <-list(title = "x axis title")
ay1 <-list(title = "y1 axds title")   

 subplot(
        plot_ly(x = 1:3, y = 10*(1:3), name = "slope of 10") %>%
            add_trace(x = 2:4, y = 1:3, name = "slope of 1", yaxis = "y2") %>%
            layout(title = "Double Y Axis", yaxis2 = ay, xaxis = ax, yaxis = ay1), 
        plot_ly(x = 1:3, y = 10*(1:3), name = "slope of 10") %>%
            add_trace(x = 2:4, y = 1:3, name = "slope of 1", yaxis = "y2") %>%
            layout(title = "Double Y Axis", yaxis2 = ay, xaxis = ax, yaxis = ay1), nrows = 2)

Output

enter image description here

Share:
13,643
user3022875
Author by

user3022875

Updated on August 23, 2022

Comments

  • user3022875
    user3022875 about 1 year

    I'd like to do multi plots with two axes on each plot like this

    library(plotly)
    ay <- list(
      tickfont = list(color = "green"),
      overlaying = "y",
      side = "right", title = "y2 axis title"
    )
    
    
    par(mfrow=c(2,1))
    ax <-list(title = "x axis title")
    ay1 <-list(title = "y1 axds title")
    plot_ly(x = 1:3, y = 10*(1:3), name = "slope of 10") %>%
      add_trace(x = 2:4, y = 1:3, name = "slope of 1", yaxis = "y2") %>%
      layout(title = "Double Y Axis", yaxis2 = ay, xaxis = ax, yaxis = ay1) 
    
    
     ax <-list(title = "x axis title")
     ay1 <-list(title = "y1 axds title")
     plot_ly(x = 1:3, y = 10*(1:3), name = "slope of 10") %>%
       add_trace(x = 2:4, y = 1:3, name = "slope of 1", yaxis = "y2") %>%
       layout(title = "Double Y Axis", yaxis2 = ay, xaxis = ax, yaxis = ay1) 
    

    but when you run that code you still only see one plot. Can plotly do multi plots? Can it do faceting with two axes?

  • user3022875
    user3022875 over 7 years
    when I run the above code the plot does not appear. I also removed the ")" after "ay1" in the last line
  • Sumedh
    Sumedh over 7 years
    I posted the output I get when I run the exact same code. I am using plotly_3.6.0 and ggplot2_2.1.0
  • Sumedh
    Sumedh over 7 years
    Try this: assign your first plot_ly graph to a, and the second to b. Then run subplot(a,b, nrow= 2). Does the plot show up now?
  • Sumedh
    Sumedh over 7 years
    @user3022875, are you checking in the Viewer Pane? plotly plots won't show up in the Plots pane