plotly multiple plot facet

user3022875 picture user3022875 · Jul 25, 2016 · Viewed 12.2k times · Source

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?

Answer

Sumedh picture Sumedh · Jul 26, 2016

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