4

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?

4

1 回答 1

3

您正在寻找subplot. 查看此页面了解更多信息

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)

输出

在此处输入图像描述

于 2016-07-25T22:04:00.987 回答