Skip to content

Commit 4c999f2

Browse files
authored
Fix coord_map to allow switching axis position (#3069)
* Fix scale position for coord_map() * Add news bullet * Add visual test for top/right scale positions in coord_map() * Better defaults for arrange when x.arrange is NULL * Fix test
1 parent f0da3d0 commit 4c999f2

File tree

4 files changed

+90
-7
lines changed

4 files changed

+90
-7
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
text from running out of the strip and borders from looking thicker than for
77
other strips (@karawoo, #2772 and #3061).
88

9+
* `coord_map()` now can have axes on the top and right (@karawoo, #3042).
10+
911
* `geom_rug()` now works with `coord_flip()` (@has2k1, #2987).
1012

1113
* Layers now have a new member function `setup_layer()` which is called at the

R/coord-map.r

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,8 @@ CoordMap <- ggproto("CoordMap", Coord,
189189
x.range = ret$x$range, y.range = ret$y$range,
190190
x.proj = ret$x$proj, y.proj = ret$y$proj,
191191
x.major = ret$x$major, x.minor = ret$x$minor, x.labels = ret$x$labels,
192-
y.major = ret$y$major, y.minor = ret$y$minor, y.labels = ret$y$labels
192+
y.major = ret$y$major, y.minor = ret$y$minor, y.labels = ret$y$labels,
193+
x.arrange = scale_x$axis_order(), y.arrange = scale_y$axis_order()
193194
)
194195
details
195196
},
@@ -243,7 +244,7 @@ CoordMap <- ggproto("CoordMap", Coord,
243244
},
244245

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

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

261262
axes <- list(
262-
bottom = guide_axis(pos$x, panel_params$x.labels, "bottom", theme),
263-
top = guide_axis(pos$x, panel_params$x.labels, "top", theme)
263+
top = guide_axis(pos$x, panel_params$x.labels, "top", theme),
264+
bottom = guide_axis(pos$x, panel_params$x.labels, "bottom", theme)
264265
)
265266
axes[[which(arrange == "secondary")]] <- zeroGrob()
266267
axes

tests/figs/coord-map/coord-map-switched-scale-position.svg

Lines changed: 69 additions & 0 deletions
Loading

tests/testthat/test-coord-map.R

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
context("coord_map")
22

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

6+
test_that("USA state map drawn", {
77
expect_doppelganger(
88
"USA mercator",
99
p_us +
@@ -12,6 +12,17 @@ test_that("USA state map drawn", {
1212
)
1313
})
1414

15+
test_that("coord_map scale position can be switched", {
16+
expect_doppelganger(
17+
"coord_map switched scale position",
18+
p_us +
19+
geom_polygon(fill = NA, colour = "grey50") +
20+
coord_map("mercator") +
21+
scale_y_continuous(position = "right") +
22+
scale_x_continuous(position = "top")
23+
)
24+
})
25+
1526
test_that("Inf is squished to range", {
1627
d <- cdata(
1728
ggplot(data_frame(x = 0, y = 0)) +

0 commit comments

Comments
 (0)