Skip to content
Open
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
6 changes: 6 additions & 0 deletions R/plot-construction.R
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,12 @@ S7::method(update_ggplot, list(class_theme, class_ggplot)) <-
S7::method(update_ggplot, list(class_coord, class_ggplot)) <-
function(object, plot, ...) {
if (!isTRUE(plot@coordinates$default)) {

if (isTRUE(object$default)) {
# We don't let default coords override non-default coords (#6572)
return(plot)
}

cli::cli_inform(c(
"Coordinate system already present.",
i = "Adding new coordinate system, which will replace the existing one."
Expand Down
8 changes: 8 additions & 0 deletions tests/testthat/_snaps/coord-.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,11 @@
Error:
! `1:3` must be a vector of length 2, not length 3.

# adding default coords works correctly

Code
test <- test + coord_cartesian(xlim = c(-2, 2))
Message
Coordinate system already present.
i Adding new coordinate system, which will replace the existing one.

28 changes: 28 additions & 0 deletions tests/testthat/test-coord-.R
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,31 @@ test_that("coord expand takes a vector", {

})

test_that("adding default coords works correctly", {

base <- ggplot() + coord_cartesian(default = TRUE, xlim = c(0, 1))

# default + user = user
expect_no_message(
test <- base + coord_cartesian(xlim = c(-1, 1))
)
expect_equal(test@coordinates$limits$x, c(-1, 1))

# user1 + user2 = user2 + message
expect_snapshot(
test <- test + coord_cartesian(xlim = c(-2, 2))
)
expect_equal(test@coordinates$limits$x, c(-2, 2))

# user + default = user
expect_no_message(
test <- test + coord_cartesian(xlim = c(-3, 3), default = TRUE)
)
expect_equal(test@coordinates$limits$x, c(-2, 2))

# default1 + default2 = default2 (silent)
expect_no_message(
test <- base + coord_cartesian(xlim = c(-4, 4), default = TRUE)
)
expect_equal(test@coordinates$limits$x, c(-4, 4))
})
Loading