Skip to content

Commit

Permalink
Use if condition to do special for development version of ggplot2 ins…
Browse files Browse the repository at this point in the history
…tead of try_fetch
  • Loading branch information
Yunuuuu committed Jan 8, 2025
1 parent 240e7d2 commit ab39bd2
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 54 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -626,3 +626,4 @@ importFrom(utils,getFromNamespace)
importFrom(utils,getS3method)
importFrom(utils,modifyList)
importFrom(utils,packageName)
importFrom(utils,packageVersion)
44 changes: 20 additions & 24 deletions R/ggplot-helper.R
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ element_polygon <- function(fill = NULL, colour = NULL, linewidth = NULL,
)
}

#' @importFrom utils packageVersion
#' @importFrom grid gpar
#' @importFrom ggplot2 element_grob
#' @export
Expand All @@ -94,41 +95,36 @@ 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),
lty = element$linetype,
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), ...
Expand Down
10 changes: 6 additions & 4 deletions R/ggplot-theme.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 6 additions & 8 deletions R/ggplot-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
43 changes: 25 additions & 18 deletions R/layout-circle-build.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit ab39bd2

Please sign in to comment.