Skip to content

Commit

Permalink
remove old compat code for ggplot < 3.5
Browse files Browse the repository at this point in the history
  • Loading branch information
mjskay committed Feb 23, 2024
1 parent 3ccfa50 commit 6e2dd8f
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 84 deletions.
2 changes: 0 additions & 2 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ S3method(format,ggdist_partial_colour_ramp)
S3method(format,ggdist_thickness)
S3method(generate,ggdist__weighted_sample)
S3method(generate,ggdist__wrapped_categorical)
S3method(guide_train,rampbar)
S3method(hdci_,distribution)
S3method(hdci_,numeric)
S3method(hdci_,rvar)
Expand Down Expand Up @@ -362,5 +361,4 @@ importFrom(stats,weighted.mean)
importFrom(stats,weights)
importFrom(tibble,as_tibble)
importFrom(tibble,tibble)
importFrom(utils,packageVersion)
useDynLib(ggdist, .registration = TRUE)
31 changes: 3 additions & 28 deletions R/geom.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ add_default_computed_aesthetics = function(l, default_mapping) {
setup_layer = function(self, data, plot) {
data = ggproto_parent(l, self)$setup_layer(data, plot)

mapping = computed_mapping(self)

for (aesthetic in names(default_mapping)) {
# we don't use exact matching here because if someone is using ggnewscale
# then aesthetic "x" will be replaced with "x_new" and we don't want to
Expand All @@ -31,8 +29,8 @@ add_default_computed_aesthetics = function(l, default_mapping) {
if (
# only add the aesthetic if it isn't already set and if the variables it uses
# are in the provided data and none of them are NA
is.null(mapping[[aesthetic, exact = FALSE]]) &&
(!isTRUE(self$inherit.aes) || is.null(computed_mapping(plot)[[aesthetic, exact = FALSE]])) &&
is.null(self$computed_mapping[[aesthetic, exact = FALSE]]) &&
(!isTRUE(self$inherit.aes) || is.null(plot$computed_mapping[[aesthetic, exact = FALSE]])) &&
all(vars_in_mapping %in% names(data)) &&
!anyNA(data[, vars_in_mapping])
) {
Expand All @@ -41,41 +39,18 @@ add_default_computed_aesthetics = function(l, default_mapping) {
# gets mangled. So we need to recreate it from the underlying expression
# and the environment (which in this case should be the package
# environment, which is the same as environment(add_default_computed_aesthetics))
mapping[[aesthetic]] = as_quosure(
self$computed_mapping[[aesthetic]] = as_quosure(
default_aes_mapping,
env = environment(add_default_computed_aesthetics)
)
}
}

computed_mapping(self) = mapping

data
}
)
}

#' the mapping property of layers changed to computed_mapping in ggplot 3.3.4
#' to avoid statefulness; this function encapsulates that change
#' see https://github.com/tidyverse/ggplot2/pull/4475
#' @importFrom utils packageVersion
#' @noRd
computed_mapping = function(x) {
mapping = if (packageVersion("ggplot2") >= "3.3.3.9000") {
x$computed_mapping
} else {
x$mapping # nocov
}
mapping %||% list()
}
`computed_mapping<-` = function(x, value) {
if (packageVersion("ggplot2") >= "3.3.3.9000") {
x$computed_mapping = value
} else {
x$mapping = value # nocov
}
x
}

# orientation detection ---------------------------------------------------

Expand Down
62 changes: 24 additions & 38 deletions R/guide_rampbar.R
Original file line number Diff line number Diff line change
Expand Up @@ -58,43 +58,29 @@
#' @export
guide_rampbar = function(..., to = "gray65", available_aes = c("fill_ramp", "colour_ramp")) {
guide = guide_colourbar(..., available_aes = available_aes)
if (inherits(guide, "GuideColourbar")) {
# If ggplot2 >3.4.2, guides are written ggproto, so here we inherit from
# the colourbar guide
new_guide = ggproto(
"GuideRampbar", guide,
params = c(list(to = to), guide$params),
extract_decor = function(
scale, aesthetic, nbin = 300, reverse = FALSE, alpha = NA,
to = "gray65", ...
) {
limits <- scale$get_limits()
bar <- seq(limits[1], limits[2], length.out = nbin)
if (length(bar) == 0) {
bar <- unique(limits)
}
bar <- data_frame(
colour = scale$map(bar),
value = bar,
.size = length(bar)
)
if (reverse) {
bar <- bar[nrow(bar):1, , drop = FALSE]
}
bar$colour = alpha(apply_colour_ramp(to, bar$colour), alpha)
bar
}
)
return(new_guide)
}
guide$to = to
class(guide) = c("guide", "rampbar", "colorbar")
guide
}

#' @export
guide_train.rampbar = function(guide, scale, aesthetic = NULL) {
guide = NextMethod()
guide$bar$colour = apply_colour_ramp(guide$to, guide$bar$colour)
guide
ggproto(
"GuideRampbar", guide,
params = c(list(to = to), guide$params),
extract_decor = function(
scale, aesthetic, nbin = 300, reverse = FALSE, alpha = NA,
to = "gray65", ...
) {
limits <- scale$get_limits()
bar <- seq(limits[1], limits[2], length.out = nbin)
if (length(bar) == 0) {
bar <- unique(limits)
}
bar <- data_frame(
colour = scale$map(bar),
value = bar,
.size = length(bar)
)
if (reverse) {
bar <- bar[nrow(bar):1, , drop = FALSE]
}
bar$colour = alpha(apply_colour_ramp(to, bar$colour), alpha)
bar
}
)
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions tests/testthat/test.guide_rampbar.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ test_that("rampbar works with fill_ramp", {
test_that("rampbar works with color_ramp", {
skip_if_no_vdiffr()

vdiffr::expect_doppelganger("color_ramp without `to`",
vdiffr::expect_doppelganger("reversed color_ramp without `to`",
tibble(d = dist_uniform(0, 1)) %>%
ggplot(aes(y = 0, dist = d)) +
stat_dist_slab(aes(color_ramp = after_stat(x)), n = 20, color = "red", size = 5) +
scale_color_ramp_continuous(from = "blue", guide = guide_rampbar())
scale_color_ramp_continuous(from = "blue", guide = guide_rampbar(reverse = TRUE))
)

vdiffr::expect_doppelganger("color_ramp with `to`",
Expand Down

0 comments on commit 6e2dd8f

Please sign in to comment.