Skip to content

Commit

Permalink
refactor: Use linewidth aesthetic for border line widths
Browse files Browse the repository at this point in the history
* 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_sf_pattern()` (and any other geoms which also have points).

closes #82
  • Loading branch information
trevorld committed Aug 30, 2022
1 parent 3ab038a commit 0bff53f
Show file tree
Hide file tree
Showing 15 changed files with 85 additions and 61 deletions.
4 changes: 3 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 8 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
12 changes: 6 additions & 6 deletions R/geom-.R
Original file line number Diff line number Diff line change
Expand Up @@ -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

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
)
)
Expand All @@ -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
)
)
Expand Down Expand Up @@ -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
)
)
Expand All @@ -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
)
)
Expand Down
11 changes: 7 additions & 4 deletions R/geom-boxplot.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
)
),

Expand All @@ -171,6 +172,8 @@ GeomBoxplotPattern <- ggproto(

draw_key = function(self, ...) {
draw_key_boxplot_pattern(..., aspect_ratio = self$aspect_ratio)
}
},

rename_size = TRUE
)

11 changes: 6 additions & 5 deletions R/geom-crossbar.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ GeomCrossbarPattern <- ggproto(
aes(
colour = "black",
fill = NA,
size = 0.5,
linewidth= 0.5,
linetype = 1,
alpha = NA
)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -121,7 +121,8 @@ GeomCrossbarPattern <- ggproto(
GeomPolygonPattern$draw_panel(box, panel_params, coord),
GeomSegment$draw_panel(middle, panel_params, coord)
)))
}
},
rename_size = TRUE
)


Expand Down
5 changes: 3 additions & 2 deletions R/geom-density.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
)


Expand Down
2 changes: 1 addition & 1 deletion R/geom-map.R
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
15 changes: 7 additions & 8 deletions R/geom-polygon.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ geom_polygon_pattern <- function(mapping = NULL, data = NULL,
)
}


#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' @rdname ggpattern-ggproto
#' @format NULL
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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()) {
Expand Down Expand Up @@ -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)
Expand Down
10 changes: 6 additions & 4 deletions R/geom-rect.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ GeomRectPattern <- ggplot2::ggproto(
aes(
colour = NA,
fill = "grey35",
size = 0.5,
linewidth = 0.5,
linetype = 1,
alpha = NA
)
Expand Down Expand Up @@ -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"
Expand All @@ -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"
Expand All @@ -145,7 +145,9 @@ GeomRectPattern <- ggplot2::ggproto(
)
)
}
}
},

rename_size = TRUE
)


Expand Down
26 changes: 14 additions & 12 deletions R/geom-ribbon.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ GeomRibbonPattern <- ggproto(
ggplot2::aes(
colour = NA,
fill = "grey20",
size = 0.5,
linewidth = 0.5,
linetype = 1,
alpha = NA
)
Expand All @@ -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)
)

Expand Down Expand Up @@ -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

)

Expand Down Expand Up @@ -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
)
),

Expand All @@ -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
)
11 changes: 8 additions & 3 deletions R/geom-sf.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ GeomSfPattern <- ggproto(
fill = NULL,
size = NULL,
linetype = 1,
linewidth = NULL,
alpha = NA,
stroke = 0.5
)
Expand All @@ -66,7 +67,8 @@ GeomSfPattern <- ggproto(
} else {
draw_key_polygon_pattern(data, params, size)
}
}
},
rename_size = TRUE
)

default_aesthetics <- function(type) {
Expand Down Expand Up @@ -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]

Expand Down
Loading

0 comments on commit 0bff53f

Please sign in to comment.