Skip to content

Commit 4e99036

Browse files
committed
Fixing secondary X and Y axis on subplots
1 parent cc49ee5 commit 4e99036

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

R/subplots.R

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,11 +201,19 @@ subplot <- function(..., nrows = 1, widths = NULL, heights = NULL, margin = 0.02
201201
if (grepl("^geo|^mapbox", names(xAxes[[i]][j]))) next
202202
map <- yMap[yMap %in% sub("y", "yaxis", xAxes[[i]][[j]]$anchor %||% "y")]
203203
xAxes[[i]][[j]]$anchor <- sub("axis", "", names(map))
204+
if(!is.null(xAxes[[i]][[j]][['overlaying']])) {
205+
map <- xMap[xMap %in% sub("x", "xaxis", xAxes[[i]][[j]]$overlaying)]
206+
xAxes[[i]][[j]]$overlaying <- sub("axis", "", names(map))
207+
}
204208
}
205209
for (j in seq_along(yAxes[[i]])) {
206210
if (grepl("^geo|^mapbox", names(yAxes[[i]][j]))) next
207211
map <- xMap[xMap %in% sub("x", "xaxis", yAxes[[i]][[j]]$anchor %||% "x")]
208212
yAxes[[i]][[j]]$anchor <- sub("axis", "", names(map))
213+
if(!is.null(yAxes[[i]][[j]][['overlaying']])) {
214+
map <- yMap[yMap %in% sub("y", "yaxis", yAxes[[i]][[j]]$overlaying)]
215+
yAxes[[i]][[j]]$overlaying <- sub("axis", "", names(map))
216+
}
209217
}
210218

211219
for (key in c("geo", "subplot", "xaxis", "yaxis")) {

tests/testthat/test-plotly-subplot.R

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,3 +557,36 @@ test_that("May specify legendgroup with through a vector of values", {
557557

558558
})
559559

560+
test_that("dual y axis subplot works", {
561+
p1 <- plot_ly() %>%
562+
add_trace(x = 1:3, y = 10*(1:3)) %>%
563+
add_trace(x = 2:4, y = 1:3, yaxis = "y2") %>%
564+
layout(yaxis2 = list(overlaying = "y", side="right"))
565+
p2 <- plot_ly() %>%
566+
add_trace(x = 3:5, y = (1:3) ^ 2) %>%
567+
add_trace(x = 4:6, y = (1:3) ^ 3, yaxis = "y2") %>%
568+
layout(yaxis2 = list(overlaying = "y", side="right"))
569+
s <- expect_traces(subplot(p1, p2, nrows = 2, shareX = T), 4, "dual-y-axis")
570+
yAxes <- sapply(s$data, `[[`, 'yaxis')
571+
names(yAxes) <- sub("y", "yaxis", yAxes)
572+
573+
expect_identical(yAxes[[1]], s$layout[[names(yAxes)[[2]]]]$overlaying)
574+
expect_identical(yAxes[[3]], s$layout[[names(yAxes)[[4]]]]$overlaying)
575+
})
576+
577+
test_that("dual x axis subplot works", {
578+
p1 <- plot_ly() %>%
579+
add_trace(x = 1:3, y = 10*(1:3)) %>%
580+
add_trace(x = 2:4, y = 1:3, name = "slope of 1", xaxis = "x2") %>%
581+
layout(xaxis2 = list(overlaying = "x", side="top"))
582+
p2 <- plot_ly() %>%
583+
add_trace(x = 3:5, y = (1:3) ^ 2, name = "quadratic") %>%
584+
add_trace(x = 4:6, y = (1:3) ^ 3, name = "cubic", xaxis = "x2") %>%
585+
layout(xaxis2 = list(overlaying = "x", side="top"))
586+
s <- expect_traces(subplot(p1, p2), 4, "dual-x-axis")
587+
xAxes <- sapply(s$data, `[[`, 'xaxis')
588+
names(xAxes) <- sub("x", "xaxis", xAxes)
589+
590+
expect_identical(xAxes[[1]], s$layout[[names(xAxes)[[2]]]]$overlaying)
591+
expect_identical(xAxes[[3]], s$layout[[names(xAxes)[[4]]]]$overlaying)
592+
})

0 commit comments

Comments
 (0)