Skip to content

Commit a0205df

Browse files
authored
Better warnings for old coords and new guides (#5708)
* throw warnings for `coord_map()` and `coord_polar()` * add tests * add news bullet
1 parent 7a393da commit a0205df

File tree

7 files changed

+50
-0
lines changed

7 files changed

+50
-0
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11

22
# ggplot2 (development version)
33

4+
* `coord_map()` and `coord_polar()` throw informative warnings when used
5+
with the guide system (#5707).
46
* When passing a function to `stat_contour(breaks)`, that function is used to
57
calculate the breaks even if `bins` and `binwidth` are missing
68
(@teunbrand, #5686).

R/coord-map.R

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,24 @@ CoordMap <- ggproto("CoordMap", Coord,
235235
details
236236
},
237237

238+
setup_panel_guides = function(self, panel_params, guides, params = list()) {
239+
guide_names <- intersect(
240+
names(guides$guides),
241+
c("x", "x.sec", "y", "y.sec", "r", "r.sec", "theta", "theta.sec")
242+
)
243+
if (length(guide_names) > 0) {
244+
cli::cli_warn(
245+
"{.fn {snake_class(self)}} cannot render {cli::qty(guide_names)} \\
246+
guide{?s} for the aesthetic{?s}: {.and {.field {guide_names}}}."
247+
)
248+
}
249+
panel_params
250+
},
251+
252+
train_panel_guides = function(self, panel_params, layers, params = list()) {
253+
panel_params
254+
},
255+
238256
render_bg = function(self, panel_params, theme) {
239257
xrange <- expand_range(panel_params$x.range, 0.2)
240258
yrange <- expand_range(panel_params$y.range, 0.2)

R/coord-polar.R

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,16 @@ CoordPolar <- ggproto("CoordPolar", Coord,
162162
},
163163

164164
setup_panel_guides = function(self, panel_params, guides, params = list()) {
165+
guide_names <- intersect(
166+
names(guides$guides),
167+
c("x", "x.sec", "y", "y.sec", "r", "r.sec", "theta", "theta.sec")
168+
)
169+
if (length(guide_names) > 0) {
170+
cli::cli_warn(
171+
"{.fn {snake_class(self)}} cannot render {cli::qty(guide_names)} \\
172+
guide{?s} for the aesthetic{?s}: {.and {.field {guide_names}}}."
173+
)
174+
}
165175
panel_params
166176
},
167177

tests/testthat/_snaps/coord-map.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,7 @@
66

77
`ylim` must be a vector of length 2, not an integer vector of length 3.
88

9+
# coord_map throws informative warning about guides
10+
11+
`coord_map()` cannot render guide for the aesthetic: x.
12+

tests/testthat/_snaps/coord-polar.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# coord_polar throws informative warning about guides
2+
3+
`coord_polar()` cannot render guide for the aesthetic: theta.
4+
15
# coord_radial warns about axes
26

37
`guide_axis()` cannot be used for theta.

tests/testthat/test-coord-map.R

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,9 @@ test_that("coord map throws error when limits are badly specified", {
5050
# throws error when limit's length is different than two
5151
expect_snapshot_error(ggplot() + coord_cartesian(ylim=1:3))
5252
})
53+
54+
test_that("coord_map throws informative warning about guides", {
55+
expect_snapshot_warning(
56+
ggplot_build(ggplot() + coord_map() + guides(x = guide_axis()))
57+
)
58+
})

tests/testthat/test-coord-polar.R

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,12 @@ test_that("coord_polar can have free scales in facets", {
9898
expect_equal(sc$y$get_limits(), c(0, 1))
9999
})
100100

101+
test_that("coord_polar throws informative warning about guides", {
102+
expect_snapshot_warning(
103+
ggplot_build(ggplot() + coord_polar() + guides(theta = guide_axis()))
104+
)
105+
})
106+
101107
test_that("coord_radial warns about axes", {
102108

103109
p <- ggplot(mtcars, aes(disp, mpg)) +

0 commit comments

Comments
 (0)