diff --git a/NAMESPACE b/NAMESPACE index a4ddbd86..40732283 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -12,6 +12,7 @@ S3method(interleave,default) S3method(interleave,unit) S3method(makeContent,arrow2) S3method(makeContent,contourTextGrob) +S3method(makeContent,metR_labelgrob) S3method(makeContent,vector) S3method(predict,eof) S3method(print,eof) diff --git a/R/geom_arrow.R b/R/geom_arrow.R index 8772beeb..18d6af76 100644 --- a/R/geom_arrow.R +++ b/R/geom_arrow.R @@ -358,7 +358,8 @@ StatArrow <- ggplot2::ggproto("StatArrow", ggplot2::Stat, warningf("The use of preserve.dir = FALSE with discrete scales is not recommended.", call. = FALSE) } - if (scales$x$scale_name == "date" | scales$x$scale_name == "date") { + trans_name <- scales$x$trans$name + if (trans_name == "date") { warningf("The use of preserve.dir = FALSE with date scales is not recommended.", call. = FALSE) } } diff --git a/R/guide_colorstrip.R b/R/guide_colorstrip.R index 4b29830f..d5958426 100644 --- a/R/guide_colorstrip.R +++ b/R/guide_colorstrip.R @@ -138,12 +138,13 @@ guide_train.colorstrip <- function(guide, scale, aesthetic = NULL) { if (guide$inside) { breaks <- scale$get_breaks() - breaks <- breaks[!is.na(breaks)] - guide$nbin <- length(breaks) - .bar <- breaks + keep <- breaks >= .limits[1] & breaks <= .limits[2] & is.finite(breaks) + guide$nbin <- sum(keep) + .bar <- breaks[keep] } else { breaks <- .get_breaks(scale) - .bar <- .inside(breaks[!is.na(breaks)]) + keep <- is.finite(breaks) + .bar <- .inside(breaks[keep]) guide$nbin <- length(.bar) } if (length(breaks) == 0 || all(is.na(breaks))) @@ -160,7 +161,7 @@ guide_train.colorstrip <- function(guide, scale, aesthetic = NULL) { ticks$.value <- breaks ticks$.label <- scale$get_labels(breaks) - guide$key <- ticks + guide$key <- ticks[keep, ,drop = FALSE] if (guide$reverse) { guide$key <- guide$key[nrow(guide$key):1, ] diff --git a/R/guide_vector.R b/R/guide_vector.R index be3d931e..23985d44 100644 --- a/R/guide_vector.R +++ b/R/guide_vector.R @@ -118,7 +118,7 @@ guide_geom.vector <- function(guide, layers, ...) { return(NextMethod()) } legend <- ggplot2::guide_legend() - legend$get_layer_key(guide, layers) + legend$get_layer_key(guide, layers, rep(list(NULL), length(layers))) } #' @export @@ -127,8 +127,13 @@ guide_gengrob.vector <- function(guide, theme) { if (!inherits(ggplot2::guide_none(), "Guide")) { return(NextMethod()) } + position <- theme$legend.position %||% "right" + direction <- theme$legend.direction %||% switch( + position, top = , bottom = "horizontal", "vertical" + ) + theme$legend.key.width <- guide$keywidth legend <- ggplot2::guide_legend() - legend$draw(theme, guide) + legend$draw(theme, position = position, direction = direction, params = guide) } diff --git a/R/textContourGrob.R b/R/textContourGrob.R index ed8433d2..88af7ca1 100644 --- a/R/textContourGrob.R +++ b/R/textContourGrob.R @@ -235,7 +235,34 @@ labelGrob <- function (label, x = grid::unit(0.5, "npc"), y = grid::unit(0.5, "n y <- grid::unit(y, default.units) grid::gTree(label = label, x = x, y = y, just = just, padding = padding, r = r, name = name, text.gp = text.gp, rect.gp = rect.gp, - vp = vp, cl = "labelgrob") + vp = vp, cl = "metR_labelgrob") +} + +#' @export +makeContent.metR_labelgrob <- function(x) { + hj <- grid::resolveHJust(x$just, NULL) + vj <- grid::resolveVJust(x$just, NULL) + + t <- grid::textGrob( + x$label, + x$x + 2 * (0.5 - hj) * x$padding, + x$y + 2 * (0.5 - vj) * x$padding, + just = c(hj, vj), + gp = x$text.gp, + name = "text" + ) + + r <- grid::roundrectGrob( + x$x, x$y, default.units = "native", + width = grid::grobWidth(t) + 2 * x$padding, + height = grid::grobHeight(t) + 2 * x$padding, + just = c(hj, vj), + r = x$r, + gp = x$rect.gp, + name = "box" + ) + + grid::setChildren(x, grid::gList(r, t)) } ggname <- function (prefix, grob) {