Skip to content

Commit b391a55

Browse files
committed
refactor: Use linewidth aesthetic for border line widths
* 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
1 parent 3ab038a commit b391a55

15 files changed

+84
-61
lines changed

DESCRIPTION

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@ Encoding: UTF-8
1616
Roxygen: list(markdown = TRUE)
1717
RoxygenNote: 7.2.1
1818
Imports:
19-
ggplot2,
19+
ggplot2 (>= 3.3.6.9000),
2020
glue,
2121
grid,
2222
gridpattern (>= 1.0.1),
2323
rlang,
2424
scales,
25+
Remotes:
26+
tidyverse/ggplot2
2527
Suggests:
2628
ambient,
2729
dplyr,

NEWS.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# ggpattern 1.0.0
22

3+
## Deprecated features
4+
5+
* 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).
6+
`size` will remain functioning but deprecated for these geoms and it is recommended to update all code to reflect the new aesthetic.
7+
Continue to use the `size` aesthetic to adjust the size of any *points* in `geom_boxplot_pattern()` and `geom_sf_pattern()`.
8+
>>>>>>> refactor: Use `linewidth` aesthetic for border line widths
9+
310
## Bug fixes and minor improvements
411

512
* Fixes bug in "image" pattern when `pattern_type = "tile"` (#37).

R/geom-.R

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ create_key_pattern_grob <- function(data, params, size, aspect_ratio, boundary_d
111111
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
112112
# From 'draw_key_polygon', this sets default sizes if none given.
113113
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
114-
data$size <- data$size %||% 0.5
114+
data$size <- data$linewidth %||% data$size %||% 0.5
115115
lwd <- min(data$size, min(size) / 4) * .pt
116116

117117
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -216,7 +216,7 @@ create_key_pattern_grob <- function(data, params, size, aspect_ratio, boundary_d
216216
draw_key_polygon_pattern <- function(data, params, size, aspect_ratio = 1) {
217217

218218
# message("draw_key_polygon_pattern(): aspect_ratio = ", aspect_ratio)
219-
lwd <- min(data$size, min(size) / 4) #* .pt
219+
lwd <- min(data$linewidth %||% data$size, min(size) / 4) #* .pt
220220

221221
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
222222
# 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) {
294294
gp = gpar(
295295
col = data$colour %||% "grey20",
296296
fill = scales::alpha(data$fill %||% "white", data$alpha),
297-
lwd = (data$size %||% 0.5) * .pt,
297+
lwd = (data$linewidth %||% data$size %||% 0.5) * .pt,
298298
lty = data$linetype %||% 1
299299
)
300300
)
@@ -307,7 +307,7 @@ draw_key_boxplot_pattern <- function(data, params, size, aspect_ratio = 1) {
307307
gp = gpar(
308308
col = data$colour %||% "grey20",
309309
fill = scales::alpha(data$fill %||% "white", data$alpha),
310-
lwd = (data$size %||% 0.5) * .pt,
310+
lwd = (data$linewidth %||% data$size %||% 0.5) * .pt,
311311
lty = data$linetype %||% 1
312312
)
313313
)
@@ -353,7 +353,7 @@ draw_key_crossbar_pattern <- function(data, params, size, aspect_ratio = 1) {
353353
gp = gpar(
354354
col = data$colour %||% "grey20",
355355
fill = scales::alpha(data$fill %||% "white", data$alpha),
356-
lwd = (data$size %||% 0.5) * .pt,
356+
lwd = (data$linewidth %||% data$size %||% 0.5) * .pt,
357357
lty = data$linetype %||% 1
358358
)
359359
)
@@ -363,7 +363,7 @@ draw_key_crossbar_pattern <- function(data, params, size, aspect_ratio = 1) {
363363
gp = gpar(
364364
col = data$colour %||% "grey20",
365365
fill = scales::alpha(data$fill %||% "white", data$alpha),
366-
lwd = (data$size %||% 0.5) * .pt,
366+
lwd = (data$linewidth %||% data$size %||% 0.5) * .pt,
367367
lty = data$linetype %||% 1
368368
)
369369
)

R/geom-boxplot.R

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ GeomBoxplotPattern <- ggproto(
8282

8383
common <- list(
8484
colour = data$colour,
85-
size = data$size,
85+
linewidth = data$linewidth,
8686
linetype = data$linetype,
8787
fill = scales::alpha(data$fill, data$alpha),
8888
group = data$group
@@ -157,10 +157,11 @@ GeomBoxplotPattern <- ggproto(
157157
weight = 1,
158158
colour = "grey20",
159159
fill = "white",
160-
size = 0.5,
160+
size = NULL,
161161
alpha = NA,
162162
shape = 19,
163-
linetype = "solid"
163+
linetype = "solid",
164+
linewidth = 0.5
164165
)
165166
),
166167

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

172173
draw_key = function(self, ...) {
173174
draw_key_boxplot_pattern(..., aspect_ratio = self$aspect_ratio)
174-
}
175+
},
176+
177+
rename_size = TRUE
175178
)
176179

R/geom-crossbar.R

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ GeomCrossbarPattern <- ggproto(
4040
aes(
4141
colour = "black",
4242
fill = NA,
43-
size = 0.5,
43+
linewidth= 0.5,
4444
linetype = 1,
4545
alpha = NA
4646
)
@@ -58,7 +58,7 @@ GeomCrossbarPattern <- ggproto(
5858

5959
data <- ggplot2::flip_data(data, flipped_aes)
6060

61-
middle <- transform(data, x = xmin, xend = xmax, yend = y, size = size * fatten, alpha = NA)
61+
middle <- transform(data, x = xmin, xend = xmax, yend = y, linewidth = linewidth * fatten, alpha = NA)
6262

6363
has_notch <- !is.null(data$ynotchlower) && !is.null(data$ynotchupper) &&
6464
!is.na(data$ynotchlower) && !is.na(data$ynotchupper)
@@ -85,7 +85,7 @@ GeomCrossbarPattern <- ggproto(
8585
),
8686
alpha = rep(data$alpha, 11),
8787
colour = rep(data$colour, 11),
88-
size = rep(data$size, 11),
88+
linewidth = rep(data$linewidth, 11),
8989
linetype = rep(data$linetype, 11),
9090
fill = rep(data$fill, 11),
9191
group = rep(seq_len(nrow(data)), 11)
@@ -103,7 +103,7 @@ GeomCrossbarPattern <- ggproto(
103103
y = c(data$ymax, data$ymin, data$ymin, data$ymax, data$ymax),
104104
alpha = rep(data$alpha, 5),
105105
colour = rep(data$colour, 5),
106-
size = rep(data$size, 5),
106+
linewidth = rep(data$linewidth, 5),
107107
linetype = rep(data$linetype, 5),
108108
fill = rep(data$fill, 5),
109109
group = rep(seq_len(nrow(data)), 5)
@@ -121,7 +121,8 @@ GeomCrossbarPattern <- ggproto(
121121
GeomPolygonPattern$draw_panel(box, panel_params, coord),
122122
GeomSegment$draw_panel(middle, panel_params, coord)
123123
)))
124-
}
124+
},
125+
rename_size = TRUE
125126
)
126127

127128

R/geom-density.R

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,13 @@ GeomDensityPattern <- ggproto(
3939
ggplot2::aes(
4040
colour = 'black',
4141
fill = "NA",
42-
size = 0.5,
42+
linewidth= 0.5,
4343
linetype = 1,
4444
weight = 1,
4545
alpha = NA
4646
)
47-
)
47+
),
48+
rename_size = TRUE
4849
)
4950

5051

R/geom-map.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ GeomMapPattern <- ggproto(
9595

9696
col <- data$colour
9797
fill <- scales::alpha(data$fill, data$alpha)
98-
lwd <- data$size * .pt
98+
lwd <- data$linewidth * .pt
9999

100100
base_grob_fn <- function(col, fill, lwd) {
101101
grid::polygonGrob(

R/geom-polygon.R

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ geom_polygon_pattern <- function(mapping = NULL, data = NULL,
2525
)
2626
}
2727

28-
2928
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3029
#' @rdname ggpattern-ggproto
3130
#' @format NULL
@@ -82,7 +81,7 @@ GeomPolygonPattern <- ggproto("GeomPolygonPattern", GeomPolygon,
8281

8382
col <- first_rows$colour
8483
fill <- scales::alpha(first_rows$fill, first_rows$alpha)
85-
lwd <- first_rows$size * .pt
84+
lwd <- first_rows$linewidth * .pt
8685

8786
polygon_grob_fn <- function(col, fill, lwd) {
8887
grid::polygonGrob(
@@ -145,13 +144,13 @@ GeomPolygonPattern <- ggproto("GeomPolygonPattern", GeomPolygon,
145144
gp_fill <- grid::gpar(
146145
col = NA,
147146
fill = scales::alpha(first_rows$fill, first_rows$alpha),
148-
lwd = first_rows$size * .pt,
147+
lwd = first_rows$linewidth * .pt,
149148
lty = first_rows$linetype
150149
)
151150
gp_border <- grid::gpar(
152151
col = first_rows$colour,
153152
fill = NA,
154-
lwd = first_rows$size * .pt,
153+
lwd = first_rows$linewidth * .pt,
155154
lty = first_rows$linetype
156155
)
157156
path_grob_fn <- function(gp = gpar()) {
@@ -191,15 +190,15 @@ GeomPolygonPattern <- ggproto("GeomPolygonPattern", GeomPolygon,
191190
ggplot2::aes(
192191
colour = "NA",
193192
fill = "grey20",
194-
size = 0.5,
193+
linewidth = 0.5,
195194
linetype = 1,
196195
alpha = NA,
197196
subgroup = NULL,
198197
)
199-
)
200-
)
201-
198+
),
202199

200+
rename_size = TRUE
201+
)
203202

204203
if (FALSE) {
205204
library(ggplot2)

R/geom-rect.R

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ GeomRectPattern <- ggplot2::ggproto(
3939
aes(
4040
colour = NA,
4141
fill = "grey35",
42-
size = 0.5,
42+
linewidth = 0.5,
4343
linetype = 1,
4444
alpha = NA
4545
)
@@ -112,7 +112,7 @@ GeomRectPattern <- ggplot2::ggproto(
112112
gp = grid::gpar(
113113
col = NA,
114114
fill = scales::alpha(coords$fill, coords$alpha),
115-
lwd = coords$size * .pt,
115+
lwd = coords$linewidth * .pt,
116116
lty = coords$linetype,
117117
linejoin = linejoin,
118118
lineend = if (identical(linejoin, "round")) "round" else "square"
@@ -136,7 +136,7 @@ GeomRectPattern <- ggplot2::ggproto(
136136
gp = grid::gpar(
137137
col = coords$colour,
138138
fill = NA,
139-
lwd = coords$size * .pt,
139+
lwd = coords$linewidth * .pt,
140140
lty = coords$linetype,
141141
linejoin = linejoin,
142142
lineend = if (identical(linejoin, "round")) "round" else "square"
@@ -145,7 +145,9 @@ GeomRectPattern <- ggplot2::ggproto(
145145
)
146146
)
147147
}
148-
}
148+
},
149+
150+
rename_size = TRUE
149151
)
150152

151153

R/geom-ribbon.R

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ GeomRibbonPattern <- ggproto(
4242
ggplot2::aes(
4343
colour = NA,
4444
fill = "grey20",
45-
size = 0.5,
45+
linewidth = 0.5,
4646
linetype = 1,
4747
alpha = NA
4848
)
@@ -64,7 +64,7 @@ GeomRibbonPattern <- ggproto(
6464

6565
# Check that aesthetics are constant
6666
aes_names <- c(
67-
"colour", "fill", "size", "linetype", "alpha",
67+
"colour", "fill", "linewidth", "linetype", "alpha",
6868
names(pattern_aesthetics)
6969
)
7070

@@ -154,14 +154,16 @@ GeomRibbonPattern <- ggproto(
154154
default.units = "native",
155155
gp = gpar(
156156
col = aes$colour,
157-
lwd = aes$size * .pt,
157+
lwd = aes$linewidth * .pt,
158158
lty = aes$linetype)
159159
)
160160

161161

162162

163163
ggname("geom_ribbon", grobTree(g_poly, pattern_grobs, g_lines))
164-
}
164+
},
165+
166+
rename_size = TRUE
165167

166168
)
167169

@@ -202,11 +204,11 @@ GeomAreaPattern <- ggproto(
202204
default_aes = augment_aes(
203205
pattern_aesthetics,
204206
aes(
205-
colour = NA,
206-
fill = "grey20",
207-
size = 0.5,
208-
linetype = 1,
209-
alpha = NA
207+
colour = NA,
208+
fill = "grey20",
209+
linewidth = 0.5,
210+
linetype = 1,
211+
alpha = NA
210212
)
211213
),
212214

@@ -222,7 +224,7 @@ GeomAreaPattern <- ggproto(
222224
data <- ggplot2::flip_data(data, params$flipped_aes)
223225
data <- transform(data[order(data$PANEL, data$group, data$x), ], ymin = 0, ymax = y)
224226
ggplot2::flip_data(data, params$flipped_aes)
225-
}
226-
)
227-
227+
},
228228

229+
rename_size = TRUE
230+
)

R/geom-sf.R

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ GeomSfPattern <- ggproto(
4040
fill = NULL,
4141
size = NULL,
4242
linetype = 1,
43+
linewidth = NULL,
4344
alpha = NA,
4445
stroke = 0.5
4546
)
@@ -66,7 +67,8 @@ GeomSfPattern <- ggproto(
6667
} else {
6768
draw_key_polygon_pattern(data, params, size)
6869
}
69-
}
70+
},
71+
rename_size = TRUE
7072
)
7173

7274
default_aesthetics <- function(type) {
@@ -124,10 +126,13 @@ sf_grob <- function(x, lineend = "butt", linejoin = "round", linemitre = 10, na.
124126
fill <- x$fill %||% defaults$fill[type_ind]
125127
fill <- scales::alpha(fill, alpha)
126128
size <- x$size %||% defaults$size[type_ind]
127-
point_size <- ifelse(is_collection, x$size %||% defaults$point_size[type_ind], size)
129+
linewidth <- x$linewidth %||% defaults$linewidth[type_ind]
130+
point_size <- ifelse(is_collection,
131+
x$size %||% defaults$point_size[type_ind],
132+
ifelse(is_point, size, linewidth))
128133
stroke <- (x$stroke %||% defaults$stroke[1]) * .stroke / 2
129134
fontsize <- point_size * .pt + stroke
130-
lwd <- ifelse(is_point, stroke, size * .pt)
135+
lwd <- ifelse(is_point, stroke, linewidth * .pt)
131136
pch <- x$shape %||% defaults$shape[type_ind]
132137
lty <- x$linetype %||% defaults$linetype[type_ind]
133138

0 commit comments

Comments
 (0)