-
Notifications
You must be signed in to change notification settings - Fork 634
Add support for alternative axis sides in ggplotly #813
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
1ae04af
bb7e07b
b5bbc2a
369b760
94ce05b
4edf8e8
21c5712
4379260
cbd3077
304a1ab
ebb135c
b82036d
92ba0eb
31c21c4
f08fc47
347cd3e
00f5bda
d3527db
1a3941e
1f6a55e
523a860
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
context("Axis moving") | ||
|
||
expect_traces <- function(gg, n.traces, name){ | ||
stopifnot(is.numeric(n.traces)) | ||
L <- save_outputs(gg, paste0("axis-", name)) | ||
all.traces <- L$data | ||
no.data <- sapply(all.traces, function(tr) { | ||
is.null(tr[["x"]]) && is.null(tr[["y"]]) | ||
}) | ||
has.data <- all.traces[!no.data] | ||
expect_equal(length(has.data), n.traces) | ||
list(data = has.data, layout = L$layout) | ||
} | ||
|
||
p <- ggplot(mtcars, aes(x=mpg, y=wt)) + | ||
geom_point() | ||
|
||
test_that("Axis position moves to top", { | ||
p <- p + scale_x_continuous(position="top") | ||
|
||
info <- save_outputs(p, "axis_move_top") | ||
expect_equal(length(info$data), 1) | ||
expect_identical(info$layout$xaxis$side, "top") | ||
}) | ||
|
||
test_that("Axis position moves to right", { | ||
p <- p + scale_y_continuous(position="right") | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The axis currently isn't shown because this PR hasn't yet addressed the issue of adding space for axis ticks/text in the right margin (and removing from the left) as is done here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed, thanks for the tip |
||
info <- save_outputs(p, "axis_move_right") | ||
expect_equal(length(info$data), 1) | ||
expect_identical(info$layout$yaxis$side, "right") | ||
}) | ||
|
||
test_that("Axis position moves to top (facets)", { | ||
p <- p + scale_x_continuous(position="top") + facet_wrap(~carb) | ||
|
||
info <- save_outputs(p, "axis_move_top_facet") | ||
expect_equal(length(info$data), 6) | ||
expect_equal(info$layout$xaxis$anchor, "y") | ||
expect_identical(info$layout$xaxis$side, "top") | ||
}) | ||
|
||
test_that("Axis position moves to top (facets)", { | ||
p <- p + scale_y_continuous(position="right") + facet_wrap(~carb) | ||
|
||
info <- save_outputs(p, "axis_move_right_facet") | ||
|
||
expect_equal(length(info$data), 6) | ||
|
||
expect_equal(info$layout$yaxis$anchor, "x3") | ||
expect_identical(info$layout$yaxis$side, "right") | ||
}) | ||
|
||
test_that("Axis positions stay at bottom and left", { | ||
info <- save_outputs(p, "axis_stay") | ||
|
||
expect_equal(length(info$data), 1) | ||
|
||
expect_identical(info$layout$xaxis$side, "bottom") | ||
expect_identical(info$layout$yaxis$side, "left") | ||
|
||
expect_equal(info$layout$xaxis$anchor, "y") | ||
expect_equal(info$layout$yaxis$anchor, "x") | ||
}) | ||
|
||
|
||
test_that("Axis positions stay at bottom and left (facet)", { | ||
p <- p + facet_wrap(~carb) | ||
info <- save_outputs(p, "axis_stay_facet") | ||
|
||
expect_equal(length(info$data), 6) | ||
|
||
expect_identical(info$layout$xaxis$side, "bottom") | ||
expect_identical(info$layout$yaxis$side, "left") | ||
|
||
expect_equal(info$layout$xaxis$anchor, "y2") | ||
expect_equal(info$layout$yaxis$anchor, "x") | ||
}) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a magic number. I don't think ticklen can vary dynamically so it has to be large enough to accomodate large and small window sizes. It'll be too long for small window sizes but if it's extreme the user can adjust it using
p %>% layout(xaxis=list(ticklen=10), xaxis2=list(ticklen=10), ...)
.I'll log an issue on the plotly.js github to see if it would be possible to have ticklen optionally be a proportion of the plot rather than strictly pixels, but otherwise it's a necessary evil