Skip to content

Fix coord_map to allow switching axis position #3069

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

Merged
merged 5 commits into from
Jan 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# ggplot2 3.1.0.9000

* `coord_map()` now can have axes on the top and right (@karawoo, #3042).

* `geom_rug()` now works with `coord_flip()` (@has2k1, #2987).

* Layers now have a new member function `setup_layer()` which is called at the
Expand Down
9 changes: 5 additions & 4 deletions R/coord-map.r
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,8 @@ CoordMap <- ggproto("CoordMap", Coord,
x.range = ret$x$range, y.range = ret$y$range,
x.proj = ret$x$proj, y.proj = ret$y$proj,
x.major = ret$x$major, x.minor = ret$x$minor, x.labels = ret$x$labels,
y.major = ret$y$major, y.minor = ret$y$minor, y.labels = ret$y$labels
y.major = ret$y$major, y.minor = ret$y$minor, y.labels = ret$y$labels,
x.arrange = scale_x$axis_order(), y.arrange = scale_y$axis_order()
)
details
},
Expand Down Expand Up @@ -243,7 +244,7 @@ CoordMap <- ggproto("CoordMap", Coord,
},

render_axis_h = function(self, panel_params, theme) {
arrange <- panel_params$x.arrange %||% c("primary", "secondary")
arrange <- panel_params$x.arrange %||% c("secondary", "primary")

if (is.null(panel_params$x.major)) {
return(list(
Expand All @@ -259,8 +260,8 @@ CoordMap <- ggproto("CoordMap", Coord,
pos <- self$transform(x_intercept, panel_params)

axes <- list(
bottom = guide_axis(pos$x, panel_params$x.labels, "bottom", theme),
top = guide_axis(pos$x, panel_params$x.labels, "top", theme)
top = guide_axis(pos$x, panel_params$x.labels, "top", theme),
bottom = guide_axis(pos$x, panel_params$x.labels, "bottom", theme)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just wondering: Is there anything happening here other than changing the order of the two lines? Is there a reason to switch the order?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, scale_x$axis_order() returns which axis is primary and which is secondary in this order (e.g. "secondary" "primary" meaning the top axis is secondary and the bottom is primary), so for the subsetting in the next line to work, this list needs to follow the same order (which is also why I had to reorder the values in arrange <- panel_params$x.arrange %||% c("secondary", "primary"))

)
axes[[which(arrange == "secondary")]] <- zeroGrob()
axes
Expand Down
69 changes: 69 additions & 0 deletions tests/figs/coord-map/coord-map-switched-scale-position.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 14 additions & 3 deletions tests/testthat/test-coord-map.R
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
context("coord_map")

test_that("USA state map drawn", {
us_map <- map_data("usa")
p_us <- ggplot(us_map, aes(x = long, y = lat, group = group))
us_map <- map_data("usa")
p_us <- ggplot(us_map, aes(x = long, y = lat, group = group))

test_that("USA state map drawn", {
expect_doppelganger(
"USA mercator",
p_us +
Expand All @@ -12,6 +12,17 @@ test_that("USA state map drawn", {
)
})

test_that("coord_map scale position can be switched", {
expect_doppelganger(
"coord_map switched scale position",
p_us +
geom_polygon(fill = NA, colour = "grey50") +
coord_map("mercator") +
scale_y_continuous(position = "right") +
scale_x_continuous(position = "top")
)
})

test_that("Inf is squished to range", {
d <- cdata(
ggplot(data_frame(x = 0, y = 0)) +
Expand Down