diff --git a/NAMESPACE b/NAMESPACE index 69b807a3..5e10fb82 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -626,3 +626,4 @@ importFrom(utils,getFromNamespace) importFrom(utils,getS3method) importFrom(utils,modifyList) importFrom(utils,packageName) +importFrom(utils,packageVersion) diff --git a/R/ggplot-helper.R b/R/ggplot-helper.R index 238c65c7..62e86c66 100644 --- a/R/ggplot-helper.R +++ b/R/ggplot-helper.R @@ -84,6 +84,7 @@ element_polygon <- function(fill = NULL, colour = NULL, linewidth = NULL, ) } +#' @importFrom utils packageVersion #' @importFrom grid gpar #' @importFrom ggplot2 element_grob #' @export @@ -94,21 +95,27 @@ element_grob.ggalign_element_polygon <- function(element, colour = NULL, linewidth = NULL, linetype = NULL, ...) { - gp <- try_fetch( - gpar( + if (packageVersion("ggplot2") > "3.5.1") { + gp <- ggfun("gg_par")( + lwd = linewidth, col = colour, fill = fill, lty = linetype + ) + element_gp <- ggfun("gg_par")( + lwd = element$linewidth, + col = element$colour, + fill = fill_alpha(element$fill, element$alpha %||% NA), + lty = element$linetype, + lineend = element$lineend, + linejoin = element$linejoin, + linemitre = element$linemitre + ) + } else { + gp <- gpar( lwd = ggfun("len0_null")(linewidth * .pt), col = colour, fill = fill, lty = linetype - ), - error = function(cnd) { - ggplot2::gg_par( - lwd = linewidth, col = colour, fill = fill, lty = linetype - ) - } - ) - element_gp <- try_fetch( - gpar( + ) + element_gp <- gpar( lwd = ggfun("len0_null")(element$linewidth * .pt), col = element$colour, fill = fill_alpha(element$fill, element$alpha %||% NA), @@ -116,19 +123,8 @@ element_grob.ggalign_element_polygon <- function(element, lineend = element$lineend, linejoin = element$linejoin, linemitre = element$linemitre - ), - error = function(cnd) { - ggplot2::gg_par( - lwd = element$linewidth, - col = element$colour, - fill = fill_alpha(element$fill, element$alpha %||% NA), - lty = element$linetype, - lineend = element$lineend, - linejoin = element$linejoin, - linemitre = element$linemitre - ) - } - ) + ) + } grid::polygonGrob( x = x, y = y, gp = ggfun("modify_list")(element_gp, gp), ... diff --git a/R/ggplot-theme.R b/R/ggplot-theme.R index 882ae2ef..92bb8543 100644 --- a/R/ggplot-theme.R +++ b/R/ggplot-theme.R @@ -28,12 +28,14 @@ theme_no_strip <- function() { ) } +#' @importFrom utils packageVersion #' @importFrom rlang try_fetch complete_theme <- function(theme) { - try_fetch( - ggfun("complete_theme")(theme), - error = function(cnd) ggfun("plot_theme")(list(theme = theme)) - ) + if (packageVersion("ggplot2") > "3.5.1") { + ggfun("complete_theme")(theme) + } else { + ggfun("plot_theme")(list(theme = theme)) + } } #' @importFrom ggplot2 register_theme_elements el_def element_line diff --git a/R/ggplot-utils.R b/R/ggplot-utils.R index 2bb4cca6..b4d3f622 100644 --- a/R/ggplot-utils.R +++ b/R/ggplot-utils.R @@ -13,16 +13,14 @@ allow_lambda <- function(x) { if (rlang::is_formula(x)) rlang::as_function(x) else x } +#' @importFrom utils packageVersion #' @importFrom rlang try_fetch fill_alpha <- function(colour, alpha = NA) { - try_fetch( - # for version >= 3.5.0 - ggplot2::fill_alpha(colour, alpha), - error = function(cnd) { - # for version < 3.5.0 - ggplot2::alpha(colour, alpha) - } - ) + if (packageVersion("ggplot2") >= "3.5.0") { + ggplot2::fill_alpha(colour, alpha) + } else { + ggplot2::alpha(colour, alpha) + } } is.waive <- function(x) inherits(x, "waiver") diff --git a/R/layout-circle-build.R b/R/layout-circle-build.R index dc69afdc..4cde63e2 100644 --- a/R/layout-circle-build.R +++ b/R/layout-circle-build.R @@ -4,6 +4,7 @@ ggalign_build.CircleLayout <- function(x) { circle_build(x) } +#' @importFrom utils packageVersion #' @importFrom ggplot2 find_panel calc_element ggproto ggplotGrob theme #' @importFrom gtable gtable_add_grob gtable_add_padding #' @importFrom grid unit viewport editGrob @@ -170,29 +171,35 @@ circle_build <- function(circle, schemes = NULL, theme = NULL) { } # build legends - default_position <- plot_theme$legend.position %||% "right" - if (length(default_position) == 2) { - default_position <- "inside" - } - if (!identical(default_position, "none")) { - plot_theme$legend.key.width <- calc_element( - "legend.key.width", - plot_theme - ) - plot_theme$legend.key.height <- calc_element( - "legend.key.height", - plot_theme - ) - guides[[i]] <- plot$guides$draw( - plot_theme, - default_position, - plot_theme$legend.direction - ) + if (packageVersion("ggplot2") > "3.5.1") { + # ggplot2 development version > 3.5.1 + guides[[i]] <- plot$guides$assemble(plot_theme) + } else { + default_position <- plot_theme$legend.position %||% "right" + if (length(default_position) == 2) { + default_position <- "inside" + } + if (!identical(default_position, "none")) { + plot_theme$legend.key.width <- calc_element( + "legend.key.width", + plot_theme + ) + plot_theme$legend.key.height <- calc_element( + "legend.key.height", + plot_theme + ) + guides[[i]] <- plot$guides$draw( + plot_theme, + default_position, + plot_theme$legend.direction + ) + } } origin <- just last_plot_size <- plot_size # the last plot panel size last_spacing <- spacing } + # attach the guide legends guides <- lapply(c(.TLBR, "inside"), function(guide_pos) { unlist(lapply(guides, .subset2, guide_pos), FALSE, FALSE)