From a7a06524d3952950d198825c658a6fad4d898dec Mon Sep 17 00:00:00 2001 From: Teun van den Brand <49372158+teunbrand@users.noreply.github.com> Date: Tue, 3 Sep 2024 18:01:25 +0200 Subject: [PATCH] Compatibility with incoming ggplot2 changes (#2368) * account for changes in label setup * defaults are only computed conditionally * Use `Geom$use_defaults()` to extract defaults --- R/ggplotly.R | 5 +++ R/layers2traces.R | 49 +++++++++++++++------------ tests/testthat/test-ggplot-area.R | 5 +-- tests/testthat/test-ggplot-point.R | 2 +- tests/testthat/test-ggplot-quantile.R | 2 +- 5 files changed, 38 insertions(+), 25 deletions(-) diff --git a/R/ggplotly.R b/R/ggplotly.R index 1c2073f9b6..e358bce17a 100644 --- a/R/ggplotly.R +++ b/R/ggplotly.R @@ -277,6 +277,11 @@ gg2list <- function(p, width = NULL, height = NULL, # Compute aesthetics to produce data with generalised variable names data <- by_layer(function(l, d) l$compute_aesthetics(d, plot)) + if (exists("setup_plot_labels", envir = asNamespace("ggplot2"))) { + # Mirror ggplot2/#5879 + plot$labels <- ggfun("setup_plot_labels")(plot, layers, data) + } + # add frame to group if it exists data <- lapply(data, function(d) { diff --git a/R/layers2traces.R b/R/layers2traces.R index 240087563f..df78b0c62e 100644 --- a/R/layers2traces.R +++ b/R/layers2traces.R @@ -766,7 +766,7 @@ geom2trace.GeomPoint <- function(data, params, p) { hoveron = hover_on(data) ) # fill is only relevant for pch %in% 21:25 - pch <- uniq(data$shape) %||% params$shape %||% GeomPoint$default_aes$shape + pch <- uniq(data$shape) %||% params$shape %||% GeomPoint$use_defaults(NULL)$shape if (any(idx <- pch %in% 21:25) || any(idx <- !is.null(data[["fill_plotlyDomain"]]))) { fill_value <- aes2plotly(data, params, "fill") if (length(idx) == 1) { @@ -866,6 +866,9 @@ geom2trace.GeomPolygon <- function(data, params, p) { #' @export geom2trace.GeomBoxplot <- function(data, params, p) { + # marker styling must inherit from GeomPoint$default_aes + # https://github.com/hadley/ggplot2/blob/ab42c2ca81458b0cf78e3ba47ed5db21f4d0fc30/NEWS#L73-L7 + point_defaults <- GeomPoint$use_defaults(NULL) compact(list( x = data[["x"]], y = data[["y"]], @@ -879,16 +882,15 @@ geom2trace.GeomBoxplot <- function(data, params, p) { aes2plotly(data, params, "fill"), aes2plotly(data, params, "alpha") ), - # marker styling must inherit from GeomPoint$default_aes - # https://github.com/hadley/ggplot2/blob/ab42c2ca81458b0cf78e3ba47ed5db21f4d0fc30/NEWS#L73-L77 + # markers/points marker = list( - opacity = GeomPoint$default_aes$alpha, - outliercolor = toRGB(GeomPoint$default_aes$colour), + opacity = point_defaults$alpha, + outliercolor = toRGB(point_defaults$colour), line = list( - width = mm2pixels(GeomPoint$default_aes$stroke), - color = toRGB(GeomPoint$default_aes$colour) + width = mm2pixels(point_defaults$stroke), + color = toRGB(point_defaults$colour) ), - size = mm2pixels(GeomPoint$default_aes$size) + size = mm2pixels(point_defaults$size) ), line = list( color = aes2plotly(data, params, "colour"), @@ -1096,21 +1098,26 @@ ribbon_dat <- function(dat) { aes2plotly <- function(data, params, aes = "size") { geom <- class(data)[1] - # Hack to support this geom_sf hack - # https://github.com/tidyverse/ggplot2/blob/505e4bfb/R/sf.R#L179-L187 - defaults <- if (inherits(data, "GeomSf")) { - type <- if (any(grepl("[P-p]oint", class(data)))) "point" else if (any(grepl("[L-l]ine", class(data)))) "line" else "" - ggfun("default_aesthetics")(type) - } else { - geom_obj <- ggfun(geom) - # If the first class of `data` is a data.frame, - # ggfun() returns a function because ggplot2 now - # defines data.frame in it's namespace - # https://github.com/ropensci/plotly/pull/1481 - if ("default_aes" %in% names(geom_obj)) geom_obj$default_aes else NULL + vals <- uniq(data[[aes]]) %||% params[[aes]] + + if (is.null(vals)) { + # Hack to support this geom_sf hack + # https://github.com/tidyverse/ggplot2/blob/505e4bfb/R/sf.R#L179-L187 + defaults <- if (inherits(data, "GeomSf") && exists("default_aesthetics", envir = asNamespace("ggplot2"))) { + type <- if (any(grepl("[P-p]oint", class(data)))) "point" else if (any(grepl("[L-l]ine", class(data)))) "line" else "" + ggfun("default_aesthetics")(type) + } else { + geom_obj <- ggfun(geom) + # If the first class of `data` is a data.frame, + # ggfun() returns a function because ggplot2 now + # defines data.frame in it's namespace + # https://github.com/ropensci/plotly/pull/1481 + if ("default_aes" %in% names(geom_obj)) geom_obj$use_defaults(NULL) else NULL + } + vals <- defaults[[aes]] } + vals <- vals %||% NA - vals <- uniq(data[[aes]]) %||% params[[aes]] %||% defaults[[aes]] %||% NA converter <- switch( aes, size = mm2pixels, diff --git a/tests/testthat/test-ggplot-area.R b/tests/testthat/test-ggplot-area.R index 6b0fea190e..5ab2d84b15 100644 --- a/tests/testthat/test-ggplot-area.R +++ b/tests/testthat/test-ggplot-area.R @@ -26,9 +26,10 @@ test_that("sanity check for geom_area", { expect_identical(L$data[[1]]$type, "scatter") expect_identical(L$data[[1]]$mode, "lines") expect_identical(L$data[[1]]$fill, "toself") + area_defaults <- GeomArea$use_defaults(NULL) expect_true( L$data[[1]]$fillcolor == - toRGB(GeomArea$default_aes$fill, GeomArea$default_aes$alpha) + toRGB(area_defaults$fill, area_defaults$alpha) ) }) @@ -40,7 +41,7 @@ test_that("transparency alpha in geom_area is converted", { expect_true(L$data[[1]]$line$color == "transparent") expect_true( L$data[[1]]$fillcolor == - toRGB(GeomArea$default_aes$fill, 0.4) + toRGB(GeomArea$use_defaults(NULL)$fill, 0.4) ) }) diff --git a/tests/testthat/test-ggplot-point.R b/tests/testthat/test-ggplot-point.R index ad50cf10fd..4a090e7c05 100644 --- a/tests/testthat/test-ggplot-point.R +++ b/tests/testthat/test-ggplot-point.R @@ -29,7 +29,7 @@ test_that("marker color is non-transparent for open shapes", { grepl("open$", info$data[[1]]$marker$symbol) ) expect_true( - info$data[[1]]$marker$color == toRGB(GeomPoint$default_aes$colour) + info$data[[1]]$marker$color == toRGB(GeomPoint$use_defaults(NULL)$colour) ) }) diff --git a/tests/testthat/test-ggplot-quantile.R b/tests/testthat/test-ggplot-quantile.R index 483980dac3..16b1e8b4fd 100644 --- a/tests/testthat/test-ggplot-quantile.R +++ b/tests/testthat/test-ggplot-quantile.R @@ -20,7 +20,7 @@ test_that("Basic geom_quantile() works", { expect_equivalent(tr$type, "scatter") expect_equivalent(tr$mode, "lines") expect_equivalent( - tr$line$color, toRGB(GeomQuantile$default_aes[["colour"]]) + tr$line$color, toRGB(GeomQuantile$use_defaults(NULL)[["colour"]]) ) }