Skip to content

Commit 29ecfa8

Browse files
authored
Default coords do not replace user coords (#6573)
* return plot as-is when default coords are added to user coords * add tests
1 parent 6094a23 commit 29ecfa8

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed

R/plot-construction.R

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,12 @@ S7::method(update_ggplot, list(class_theme, class_ggplot)) <-
200200
S7::method(update_ggplot, list(class_coord, class_ggplot)) <-
201201
function(object, plot, ...) {
202202
if (!isTRUE(plot@coordinates$default)) {
203+
204+
if (isTRUE(object$default)) {
205+
# We don't let default coords override non-default coords (#6572)
206+
return(plot)
207+
}
208+
203209
cli::cli_inform(c(
204210
"Coordinate system already present.",
205211
i = "Adding new coordinate system, which will replace the existing one."

tests/testthat/_snaps/coord-.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,11 @@
3434
Error:
3535
! `1:3` must be a vector of length 2, not length 3.
3636

37+
# adding default coords works correctly
38+
39+
Code
40+
test <- test + coord_cartesian(xlim = c(-2, 2))
41+
Message
42+
Coordinate system already present.
43+
i Adding new coordinate system, which will replace the existing one.
44+

tests/testthat/test-coord-.R

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,35 @@ test_that("coord expand takes a vector", {
108108

109109
})
110110

111+
test_that("adding default coords works correctly", {
112+
113+
base <- ggplot() + coord_cartesian(default = TRUE, xlim = c(0, 1))
114+
115+
# default + user = user
116+
expect_no_message(
117+
test <- base + coord_cartesian(xlim = c(-1, 1))
118+
)
119+
expect_equal(test@coordinates$limits$x, c(-1, 1))
120+
121+
# user1 + user2 = user2 + message
122+
expect_snapshot(
123+
test <- test + coord_cartesian(xlim = c(-2, 2))
124+
)
125+
expect_equal(test@coordinates$limits$x, c(-2, 2))
126+
127+
# user + default = user
128+
expect_no_message(
129+
test <- test + coord_cartesian(xlim = c(-3, 3), default = TRUE)
130+
)
131+
expect_equal(test@coordinates$limits$x, c(-2, 2))
132+
133+
# default1 + default2 = default2 (silent)
134+
expect_no_message(
135+
test <- base + coord_cartesian(xlim = c(-4, 4), default = TRUE)
136+
)
137+
expect_equal(test@coordinates$limits$x, c(-4, 4))
138+
})
139+
111140
test_that("NA's don't appear in breaks", {
112141

113142
# Returns true if any major/minor breaks have an NA
@@ -141,5 +170,3 @@ test_that("NA's don't appear in breaks", {
141170
skip_if_not_installed("mapproj")
142171
expect_false(any_NA_major_minor(coord_map()$setup_panel_params(scale_x, scale_y)))
143172
})
144-
145-

0 commit comments

Comments
 (0)