diff --git a/DESCRIPTION b/DESCRIPTION index b4e6a8d..ca4041a 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -16,12 +16,14 @@ Encoding: UTF-8 Roxygen: list(markdown = TRUE) RoxygenNote: 7.2.1 Imports: - ggplot2, + ggplot2 (>= 3.3.6.9000), glue, grid, gridpattern (>= 1.0.1), rlang, scales, +Remotes: + tidyverse/ggplot2 Suggests: ambient, dplyr, diff --git a/NEWS.md b/NEWS.md index 603a7ce..aaab8d9 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,11 @@ +# ggpattern (development) + +## Deprecated features + +* In line with upstream changes in `{ggplot2}` a `linewidth` aesthetic has been introduced and supersedes the `size` aesthetic for scaling the width of patterned area border lines (#82). + `size` will remain functioning but deprecated for these geoms and it is recommended to update all code to reflect the new aesthetic. + Continue to use the `size` aesthetic to adjust the size of any *points* in `geom_boxplot_pattern()` and `geom_sf_pattern()`. + # ggpattern 1.0.0 ## Bug fixes and minor improvements diff --git a/R/geom-.R b/R/geom-.R index 2525127..ba6ed63 100644 --- a/R/geom-.R +++ b/R/geom-.R @@ -111,7 +111,7 @@ create_key_pattern_grob <- function(data, params, size, aspect_ratio, boundary_d #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # From 'draw_key_polygon', this sets default sizes if none given. #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - data$size <- data$size %||% 0.5 + data$size <- data$linewidth %||% data$size %||% 0.5 lwd <- min(data$size, min(size) / 4) * .pt #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -216,7 +216,7 @@ create_key_pattern_grob <- function(data, params, size, aspect_ratio, boundary_d draw_key_polygon_pattern <- function(data, params, size, aspect_ratio = 1) { # message("draw_key_polygon_pattern(): aspect_ratio = ", aspect_ratio) - lwd <- min(data$size, min(size) / 4) #* .pt + lwd <- min(data$linewidth %||% data$size, min(size) / 4) #* .pt #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # Currently not sure, why, but the key_grob is drawn slightly undersized @@ -294,7 +294,7 @@ draw_key_boxplot_pattern <- function(data, params, size, aspect_ratio = 1) { gp = gpar( col = data$colour %||% "grey20", fill = scales::alpha(data$fill %||% "white", data$alpha), - lwd = (data$size %||% 0.5) * .pt, + lwd = (data$linewidth %||% data$size %||% 0.5) * .pt, lty = data$linetype %||% 1 ) ) @@ -307,7 +307,7 @@ draw_key_boxplot_pattern <- function(data, params, size, aspect_ratio = 1) { gp = gpar( col = data$colour %||% "grey20", fill = scales::alpha(data$fill %||% "white", data$alpha), - lwd = (data$size %||% 0.5) * .pt, + lwd = (data$linewidth %||% data$size %||% 0.5) * .pt, lty = data$linetype %||% 1 ) ) @@ -353,7 +353,7 @@ draw_key_crossbar_pattern <- function(data, params, size, aspect_ratio = 1) { gp = gpar( col = data$colour %||% "grey20", fill = scales::alpha(data$fill %||% "white", data$alpha), - lwd = (data$size %||% 0.5) * .pt, + lwd = (data$linewidth %||% data$size %||% 0.5) * .pt, lty = data$linetype %||% 1 ) ) @@ -363,7 +363,7 @@ draw_key_crossbar_pattern <- function(data, params, size, aspect_ratio = 1) { gp = gpar( col = data$colour %||% "grey20", fill = scales::alpha(data$fill %||% "white", data$alpha), - lwd = (data$size %||% 0.5) * .pt, + lwd = (data$linewidth %||% data$size %||% 0.5) * .pt, lty = data$linetype %||% 1 ) ) diff --git a/R/geom-boxplot.R b/R/geom-boxplot.R index 12b2647..931fe04 100644 --- a/R/geom-boxplot.R +++ b/R/geom-boxplot.R @@ -82,7 +82,7 @@ GeomBoxplotPattern <- ggproto( common <- list( colour = data$colour, - size = data$size, + linewidth = data$linewidth, linetype = data$linetype, fill = scales::alpha(data$fill, data$alpha), group = data$group @@ -157,10 +157,11 @@ GeomBoxplotPattern <- ggproto( weight = 1, colour = "grey20", fill = "white", - size = 0.5, + size = NULL, alpha = NA, shape = 19, - linetype = "solid" + linetype = "solid", + linewidth = 0.5 ) ), @@ -171,6 +172,8 @@ GeomBoxplotPattern <- ggproto( draw_key = function(self, ...) { draw_key_boxplot_pattern(..., aspect_ratio = self$aspect_ratio) - } + }, + + rename_size = TRUE ) diff --git a/R/geom-crossbar.R b/R/geom-crossbar.R index 931cd63..b15f2e6 100644 --- a/R/geom-crossbar.R +++ b/R/geom-crossbar.R @@ -40,7 +40,7 @@ GeomCrossbarPattern <- ggproto( aes( colour = "black", fill = NA, - size = 0.5, + linewidth= 0.5, linetype = 1, alpha = NA ) @@ -58,7 +58,7 @@ GeomCrossbarPattern <- ggproto( data <- ggplot2::flip_data(data, flipped_aes) - middle <- transform(data, x = xmin, xend = xmax, yend = y, size = size * fatten, alpha = NA) + middle <- transform(data, x = xmin, xend = xmax, yend = y, linewidth = linewidth * fatten, alpha = NA) has_notch <- !is.null(data$ynotchlower) && !is.null(data$ynotchupper) && !is.na(data$ynotchlower) && !is.na(data$ynotchupper) @@ -85,7 +85,7 @@ GeomCrossbarPattern <- ggproto( ), alpha = rep(data$alpha, 11), colour = rep(data$colour, 11), - size = rep(data$size, 11), + linewidth = rep(data$linewidth, 11), linetype = rep(data$linetype, 11), fill = rep(data$fill, 11), group = rep(seq_len(nrow(data)), 11) @@ -103,7 +103,7 @@ GeomCrossbarPattern <- ggproto( y = c(data$ymax, data$ymin, data$ymin, data$ymax, data$ymax), alpha = rep(data$alpha, 5), colour = rep(data$colour, 5), - size = rep(data$size, 5), + linewidth = rep(data$linewidth, 5), linetype = rep(data$linetype, 5), fill = rep(data$fill, 5), group = rep(seq_len(nrow(data)), 5) @@ -121,7 +121,8 @@ GeomCrossbarPattern <- ggproto( GeomPolygonPattern$draw_panel(box, panel_params, coord), GeomSegment$draw_panel(middle, panel_params, coord) ))) - } + }, + rename_size = TRUE ) diff --git a/R/geom-density.R b/R/geom-density.R index 0d8207e..d10aceb 100644 --- a/R/geom-density.R +++ b/R/geom-density.R @@ -39,12 +39,13 @@ GeomDensityPattern <- ggproto( ggplot2::aes( colour = 'black', fill = "NA", - size = 0.5, + linewidth= 0.5, linetype = 1, weight = 1, alpha = NA ) - ) + ), + rename_size = TRUE ) diff --git a/R/geom-map.R b/R/geom-map.R index 4b424ca..9a55d9c 100644 --- a/R/geom-map.R +++ b/R/geom-map.R @@ -95,7 +95,7 @@ GeomMapPattern <- ggproto( col <- data$colour fill <- scales::alpha(data$fill, data$alpha) - lwd <- data$size * .pt + lwd <- data$linewidth * .pt base_grob_fn <- function(col, fill, lwd) { grid::polygonGrob( diff --git a/R/geom-polygon.R b/R/geom-polygon.R index 0ef5879..880a5af 100644 --- a/R/geom-polygon.R +++ b/R/geom-polygon.R @@ -25,7 +25,6 @@ geom_polygon_pattern <- function(mapping = NULL, data = NULL, ) } - #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #' @rdname ggpattern-ggproto #' @format NULL @@ -82,7 +81,7 @@ GeomPolygonPattern <- ggproto("GeomPolygonPattern", GeomPolygon, col <- first_rows$colour fill <- scales::alpha(first_rows$fill, first_rows$alpha) - lwd <- first_rows$size * .pt + lwd <- first_rows$linewidth * .pt polygon_grob_fn <- function(col, fill, lwd) { grid::polygonGrob( @@ -145,13 +144,13 @@ GeomPolygonPattern <- ggproto("GeomPolygonPattern", GeomPolygon, gp_fill <- grid::gpar( col = NA, fill = scales::alpha(first_rows$fill, first_rows$alpha), - lwd = first_rows$size * .pt, + lwd = first_rows$linewidth * .pt, lty = first_rows$linetype ) gp_border <- grid::gpar( col = first_rows$colour, fill = NA, - lwd = first_rows$size * .pt, + lwd = first_rows$linewidth * .pt, lty = first_rows$linetype ) path_grob_fn <- function(gp = gpar()) { @@ -191,15 +190,15 @@ GeomPolygonPattern <- ggproto("GeomPolygonPattern", GeomPolygon, ggplot2::aes( colour = "NA", fill = "grey20", - size = 0.5, + linewidth = 0.5, linetype = 1, alpha = NA, subgroup = NULL, ) - ) -) - + ), + rename_size = TRUE +) if (FALSE) { library(ggplot2) diff --git a/R/geom-rect.R b/R/geom-rect.R index 04cb81d..ea0474a 100644 --- a/R/geom-rect.R +++ b/R/geom-rect.R @@ -39,7 +39,7 @@ GeomRectPattern <- ggplot2::ggproto( aes( colour = NA, fill = "grey35", - size = 0.5, + linewidth = 0.5, linetype = 1, alpha = NA ) @@ -112,7 +112,7 @@ GeomRectPattern <- ggplot2::ggproto( gp = grid::gpar( col = NA, fill = scales::alpha(coords$fill, coords$alpha), - lwd = coords$size * .pt, + lwd = coords$linewidth * .pt, lty = coords$linetype, linejoin = linejoin, lineend = if (identical(linejoin, "round")) "round" else "square" @@ -136,7 +136,7 @@ GeomRectPattern <- ggplot2::ggproto( gp = grid::gpar( col = coords$colour, fill = NA, - lwd = coords$size * .pt, + lwd = coords$linewidth * .pt, lty = coords$linetype, linejoin = linejoin, lineend = if (identical(linejoin, "round")) "round" else "square" @@ -145,7 +145,9 @@ GeomRectPattern <- ggplot2::ggproto( ) ) } - } + }, + + rename_size = TRUE ) diff --git a/R/geom-ribbon.R b/R/geom-ribbon.R index eafad3d..0035d5b 100644 --- a/R/geom-ribbon.R +++ b/R/geom-ribbon.R @@ -42,7 +42,7 @@ GeomRibbonPattern <- ggproto( ggplot2::aes( colour = NA, fill = "grey20", - size = 0.5, + linewidth = 0.5, linetype = 1, alpha = NA ) @@ -64,7 +64,7 @@ GeomRibbonPattern <- ggproto( # Check that aesthetics are constant aes_names <- c( - "colour", "fill", "size", "linetype", "alpha", + "colour", "fill", "linewidth", "linetype", "alpha", names(pattern_aesthetics) ) @@ -154,14 +154,16 @@ GeomRibbonPattern <- ggproto( default.units = "native", gp = gpar( col = aes$colour, - lwd = aes$size * .pt, + lwd = aes$linewidth * .pt, lty = aes$linetype) ) ggname("geom_ribbon", grobTree(g_poly, pattern_grobs, g_lines)) - } + }, + + rename_size = TRUE ) @@ -202,11 +204,11 @@ GeomAreaPattern <- ggproto( default_aes = augment_aes( pattern_aesthetics, aes( - colour = NA, - fill = "grey20", - size = 0.5, - linetype = 1, - alpha = NA + colour = NA, + fill = "grey20", + linewidth = 0.5, + linetype = 1, + alpha = NA ) ), @@ -222,7 +224,7 @@ GeomAreaPattern <- ggproto( data <- ggplot2::flip_data(data, params$flipped_aes) data <- transform(data[order(data$PANEL, data$group, data$x), ], ymin = 0, ymax = y) ggplot2::flip_data(data, params$flipped_aes) - } -) - + }, + rename_size = TRUE +) diff --git a/R/geom-sf.R b/R/geom-sf.R index 4046459..3a24ef3 100644 --- a/R/geom-sf.R +++ b/R/geom-sf.R @@ -40,6 +40,7 @@ GeomSfPattern <- ggproto( fill = NULL, size = NULL, linetype = 1, + linewidth = NULL, alpha = NA, stroke = 0.5 ) @@ -66,7 +67,8 @@ GeomSfPattern <- ggproto( } else { draw_key_polygon_pattern(data, params, size) } - } + }, + rename_size = TRUE ) default_aesthetics <- function(type) { @@ -124,10 +126,13 @@ sf_grob <- function(x, lineend = "butt", linejoin = "round", linemitre = 10, na. fill <- x$fill %||% defaults$fill[type_ind] fill <- scales::alpha(fill, alpha) size <- x$size %||% defaults$size[type_ind] - point_size <- ifelse(is_collection, x$size %||% defaults$point_size[type_ind], size) + linewidth <- x$linewidth %||% defaults$linewidth[type_ind] + point_size <- ifelse(is_collection, + x$size %||% defaults$point_size[type_ind], + ifelse(is_point, size, linewidth)) stroke <- (x$stroke %||% defaults$stroke[1]) * .stroke / 2 fontsize <- point_size * .pt + stroke - lwd <- ifelse(is_point, stroke, size * .pt) + lwd <- ifelse(is_point, stroke, linewidth * .pt) pch <- x$shape %||% defaults$shape[type_ind] lty <- x$linetype %||% defaults$linetype[type_ind] diff --git a/R/geom-tile.R b/R/geom-tile.R index 523430a..b09be1c 100644 --- a/R/geom-tile.R +++ b/R/geom-tile.R @@ -49,13 +49,13 @@ GeomTilePattern <- ggproto( default_aes = augment_aes( pattern_aesthetics, aes( - fill = "grey20", - colour = NA, - size = 0.1, - linetype = 1, - alpha = NA, - width = NA, - height = NA + fill = "grey20", + colour = NA, + linewidth = 0.1, + linetype = 1, + alpha = NA, + width = NA, + height = NA ) ), @@ -79,5 +79,3 @@ if (FALSE) { labs(title = "ggpattern::geom_tile_pattern()") } - - diff --git a/R/geom-violin.R b/R/geom-violin.R index 7f616cd..06e3958 100644 --- a/R/geom-violin.R +++ b/R/geom-violin.R @@ -101,18 +101,19 @@ GeomViolinPattern <- ggproto( weight = 1, colour = "grey20", fill = "white", - size = 0.5, + linewidth = 0.5, alpha = NA, linetype = "solid" ) ), - aspect_ratio = 1, draw_key = function(self, ...) { draw_key_polygon_pattern(..., aspect_ratio = self$aspect_ratio) - } + }, + + rename_size = TRUE ) diff --git a/man/geom-docs.Rd b/man/geom-docs.Rd index c845516..5c5409a 100644 --- a/man/geom-docs.Rd +++ b/man/geom-docs.Rd @@ -241,8 +241,10 @@ from a \code{formula} (e.g. \code{~ head(.x, 10)}).} \item{stat}{The statistical transformation to use on the data for this layer, as a string.} -\item{position}{Position adjustment, either as a string, or the result of -a call to a position adjustment function.} +\item{position}{Position adjustment, either as a string naming the adjustment +(e.g. \code{"jitter"} to use \code{position_jitter}), or the result of a call to a +position adjustment function. Use the latter if you need to change the +settings of the adjustment.} \item{...}{Other arguments passed on to \code{\link[ggplot2:layer]{layer()}}. These are often aesthetics, used to set an aesthetic to a fixed value, like diff --git a/tests/testthat/test-geom.R b/tests/testthat/test-geom.R index 5806a83..ed9b5db 100644 --- a/tests/testthat/test-geom.R +++ b/tests/testthat/test-geom.R @@ -27,7 +27,7 @@ test_that("geometry patterns work as expected", { ggplot(ggplot2::diamonds, aes(x, y)) + xlim(4, 10) + ylim(4, 10) + geom_bin2d_pattern(aes(pattern_spacing = ..density..), - fill = 'white', bins = 6, colour = 'black', size = 1) + + fill = 'white', bins = 6, colour = 'black', linewidth = 1) + theme_bw(18) + theme(legend.position = 'none') + labs(title = "ggpattern::geom_bin2d_pattern()")