情节多重情节

时间:2016-07-25 21:50:04

标签: r ggplot2 plotly r-plotly

我想在每个情节上做两个轴的多图,如下所示

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) 

但是当你运行该代码时,你仍然只看到一个情节。 plotly可以做多个情节吗?它可以用两个轴进行刻面吗?

1 个答案:

答案 0 :(得分:2)

您正在寻找subplot。查看此page了解更多

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)

<强>输出

enter image description here