I am trying to assemble a set of vertically stacked plots sharing a common x-axis. Some of the panels have a two y-axes, and this seems to be complicating my use of subplot
. For example:
library(dplyr)
library(plotly)
p1 <- plot_ly() %>%
add_lines(x = ~1:4, y = ~rep(1:2, 2))
ay <- list(
tickfont = list(color = "red"),
overlaying = "y",
side = "right",
title = "second y axis"
)
p2 <- 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")
)
p1
p2
Results in p1
and p2
I’d like to have exactly those two plots vertically aligned. However subplot(p1, p2, nrows = 2, shareX = TRUE)
results in
After scouring documentation, I believe the problem is my lack of understanding how to specify the domain for the axes. I think I need something like
subplot(p1, p2, nrows = 2, shareX = TRUE) %>%
layout(yaxis = list(domain = list(y = c(0.5, 1))),
yaxis2 = list(domain = list(y = c(0, 0.5))))
But that stab in the dark isn’t quite right (and as new user here, I’m not allowed to post a 3rd image). Could someone help me understand how this is supposed to work?