Skip to content
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

Better warnings for old coords and new guides #5708

Merged
merged 4 commits into from
Mar 18, 2024
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,6 +1,8 @@

# ggplot2 (development version)

* `coord_map()` and `coord_polar()` throw informative warnings when used
with the guide system (#5707).
* When passing a function to `stat_contour(breaks)`, that function is used to
calculate the breaks even if `bins` and `binwidth` are missing
(@teunbrand, #5686).
Expand Down
18 changes: 18 additions & 0 deletions R/coord-map.R
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,24 @@ CoordMap <- ggproto("CoordMap", Coord,
details
},

setup_panel_guides = function(self, panel_params, guides, params = list()) {
guide_names <- intersect(
names(guides$guides),
c("x", "x.sec", "y", "y.sec", "r", "r.sec", "theta", "theta.sec")
)
if (length(guide_names) > 0) {
cli::cli_warn(
"{.fn {snake_class(self)}} cannot render {cli::qty(guide_names)} \\
guide{?s} for the aesthetic{?s}: {.and {.field {guide_names}}}."
)
}
panel_params
},

train_panel_guides = function(self, panel_params, layers, params = list()) {
panel_params
},

render_bg = function(self, panel_params, theme) {
xrange <- expand_range(panel_params$x.range, 0.2)
yrange <- expand_range(panel_params$y.range, 0.2)
Expand Down
10 changes: 10 additions & 0 deletions R/coord-polar.R
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,16 @@ CoordPolar <- ggproto("CoordPolar", Coord,
},

setup_panel_guides = function(self, panel_params, guides, params = list()) {
guide_names <- intersect(
names(guides$guides),
c("x", "x.sec", "y", "y.sec", "r", "r.sec", "theta", "theta.sec")
)
if (length(guide_names) > 0) {
cli::cli_warn(
"{.fn {snake_class(self)}} cannot render {cli::qty(guide_names)} \\
guide{?s} for the aesthetic{?s}: {.and {.field {guide_names}}}."
)
}
panel_params
},

Expand Down
4 changes: 4 additions & 0 deletions tests/testthat/_snaps/coord-map.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,7 @@

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

# coord_map throws informative warning about guides

`coord_map()` cannot render guide for the aesthetic: x.

4 changes: 4 additions & 0 deletions tests/testthat/_snaps/coord-polar.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# coord_polar throws informative warning about guides

`coord_polar()` cannot render guide for the aesthetic: theta.

# coord_radial warns about axes

`guide_axis()` cannot be used for theta.
Expand Down
6 changes: 6 additions & 0 deletions tests/testthat/test-coord-map.R
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,9 @@ test_that("coord map throws error when limits are badly specified", {
# throws error when limit's length is different than two
expect_snapshot_error(ggplot() + coord_cartesian(ylim=1:3))
})

test_that("coord_map throws informative warning about guides", {
expect_snapshot_warning(
ggplot_build(ggplot() + coord_map() + guides(x = guide_axis()))
)
})
6 changes: 6 additions & 0 deletions tests/testthat/test-coord-polar.R
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ test_that("coord_polar can have free scales in facets", {
expect_equal(sc$y$get_limits(), c(0, 1))
})

test_that("coord_polar throws informative warning about guides", {
expect_snapshot_warning(
ggplot_build(ggplot() + coord_polar() + guides(theta = guide_axis()))
)
})

test_that("coord_radial warns about axes", {

p <- ggplot(mtcars, aes(disp, mpg)) +
Expand Down
Loading