Skip to content

Commit 4e1ebcd

Browse files
authored
Merge branch 'main' into check_subclass_fallback
2 parents 1473af0 + 5fe3c2a commit 4e1ebcd

File tree

4 files changed

+43
-6
lines changed

4 files changed

+43
-6
lines changed

NEWS.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# ggplot2 (development version)
22

3-
* When `check_subclass()` fails to find a class directly, it tries to retrieve
4-
the class via constructor functions.
3+
* Date scales silently coerce <POSIXct> to <Date> and datetime scales silently
4+
coerce <Date> to <POSIXct> (@laurabrianna, #3533)
55
* New parameters for `geom_label()` (@teunbrand and @steveharoz, #5365):
66
* The `linewidth` aesthetic is now applied and replaces the `label.size`
77
argument.
@@ -310,6 +310,10 @@
310310
(@teunbrand, #3669).
311311
* Added `scale_{x/y}_time(date_breaks, date_minor_breaks, date_labels)`
312312
(@teunbrand, #4335).
313+
* `ggsave()` can write a multi-page pdf file when provided with a list of plots
314+
(@teunbrand, #5093).
315+
* (internal) When `validate_subclass()` fails to find a class directly, it tries
316+
to retrieve the class via constructor functions (@teunbrand).
313317

314318
# ggplot2 3.5.1
315319

R/save.R

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,17 +101,18 @@ ggsave <- function(filename, plot = get_last_plot(),
101101
dev <- validate_device(device, filename, dpi = dpi)
102102
dim <- plot_dim(c(width, height), scale = scale, units = units,
103103
limitsize = limitsize, dpi = dpi)
104+
bg <- get_plot_background(plot, bg)
104105

105-
if (is_null(bg)) {
106-
bg <- calc_element("plot.background", plot_theme(plot))$fill %||% "transparent"
107-
}
108106
old_dev <- grDevices::dev.cur()
109107
dev(filename = filename, width = dim[1], height = dim[2], bg = bg, ...)
110108
on.exit(utils::capture.output({
111109
grDevices::dev.off()
112110
if (old_dev > 1) grDevices::dev.set(old_dev) # restore old device unless null device
113111
}))
114-
grid.draw(plot)
112+
if (!is_bare_list(plot)) {
113+
plot <- list(plot)
114+
}
115+
lapply(plot, grid.draw)
115116

116117
invisible(filename)
117118
}
@@ -235,6 +236,17 @@ plot_dim <- function(dim = c(NA, NA), scale = 1, units = "in",
235236
dim
236237
}
237238

239+
get_plot_background <- function(plot, bg = NULL, default = "transparent") {
240+
if (!is.null(bg)) {
241+
return(bg)
242+
}
243+
plot <- if (is_bare_list(plot)) plot[[1]] else plot
244+
if (!is.ggplot(plot)) {
245+
return(default)
246+
}
247+
calc_element("plot.background", plot_theme(plot))$fill %||% default
248+
}
249+
238250
validate_device <- function(device, filename = NULL, dpi = 300, call = caller_env()) {
239251
force(filename)
240252
force(dpi)

R/scale-date.R

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,9 @@ ScaleContinuousDatetime <- ggproto("ScaleContinuousDatetime", ScaleContinuous,
394394
i = "The value was converted to {obj_type_friendly(x)}."
395395
), call = self$call)
396396
}
397+
if (inherits(x, "Date")) {
398+
x <- as.POSIXct(x)
399+
}
397400
ggproto_parent(ScaleContinuous, self)$transform(x)
398401
},
399402
map = function(self, x, limits = self$get_limits()) {
@@ -441,6 +444,9 @@ ScaleContinuousDate <- ggproto("ScaleContinuousDate", ScaleContinuous,
441444
i = "The value was converted to {obj_type_friendly(x)}."
442445
), call = self$call)
443446
}
447+
if (inherits(x, "POSIXct")) {
448+
x <- as.Date(x)
449+
}
444450
ggproto_parent(ScaleContinuous, self)$transform(x)
445451
},
446452
get_breaks = function(self, limits = self$get_limits()) {

tests/testthat/test-scale_date.R

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,19 @@
11

2+
test_that("date(time) scales coerce data types", {
3+
4+
date <- as.Date("2024-11-11")
5+
datetime <- as.POSIXct(date)
6+
7+
sc <- scale_x_datetime()
8+
df <- sc$transform_df(data_frame0(x = date))
9+
expect_equal(df$x, as.numeric(datetime))
10+
11+
sc <- scale_x_date()
12+
df <- sc$transform_df(data_frame0(x = datetime))
13+
expect_equal(df$x, as.numeric(date))
14+
15+
})
16+
217
# Visual tests ------------------------------------------------------------
318

419
test_that("date scale draws correctly", {

0 commit comments

Comments
 (0)