Skip to content

Commit 3375667

Browse files
authored
Rename size aesthetic for line-based geoms (#3672)
1 parent aaa2d44 commit 3375667

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+436
-118
lines changed

DESCRIPTION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ Collate:
221221
'scale-hue.r'
222222
'scale-identity.r'
223223
'scale-linetype.r'
224+
'scale-linewidth.R'
224225
'scale-manual.r'
225226
'scale-shape.r'
226227
'scale-size.r'

NAMESPACE

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,13 @@ export(scale_linetype_continuous)
557557
export(scale_linetype_discrete)
558558
export(scale_linetype_identity)
559559
export(scale_linetype_manual)
560+
export(scale_linewidth)
561+
export(scale_linewidth_binned)
562+
export(scale_linewidth_continuous)
563+
export(scale_linewidth_date)
564+
export(scale_linewidth_datetime)
565+
export(scale_linewidth_discrete)
566+
export(scale_linewidth_ordinal)
560567
export(scale_radius)
561568
export(scale_shape)
562569
export(scale_shape_binned)

NEWS.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# ggplot2 (development version)
22

3+
* A `linewidth` aesthetic has been introduced and supersedes the `size`
4+
aesthetic for scaling the width of lines in line based geoms. `size` will
5+
remain functioning but deprecated for these geoms and it is recommended to
6+
update all code to reflect the new aesthetic (@thomasp85, #3672)
7+
38
* Secondary axis ticks are now positioned more precisely, removing small visual
49
artefacts with alignment between grid and ticks (@thomasp85, #3576)
510

R/geom-.r

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,11 @@ Geom <- ggproto("Geom",
108108

109109
# Combine data with defaults and set aesthetics from parameters
110110
use_defaults = function(self, data, params = list(), modifiers = aes()) {
111+
# Inherit size as linewidth if no linewidth aesthetic and param exist
112+
if (self$rename_size && is.null(data$linewidth) && is.null(params$linewidth)) {
113+
data$linewidth <- data$size
114+
params$linewidth <- params$size
115+
}
111116
# Fill in missing aesthetics with their defaults
112117
missing_aes <- setdiff(names(self$default_aes), names(data))
113118

@@ -187,7 +192,10 @@ Geom <- ggproto("Geom",
187192
required_aes <- unlist(strsplit(self$required_aes, '|', fixed = TRUE))
188193
}
189194
c(union(required_aes, names(self$default_aes)), self$optional_aes, "group")
190-
}
195+
},
196+
197+
# Should the geom rename size to linewidth?
198+
rename_size = FALSE
191199

192200
)
193201

R/geom-abline.r

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ NULL
2222
#' @section Aesthetics:
2323
#' These geoms are drawn using with [geom_line()] so support the
2424
#' same aesthetics: `alpha`, `colour`, `linetype` and
25-
#' `size`. They also each have aesthetics that control the position of
25+
#' `linewidth`. They also each have aesthetics that control the position of
2626
#' the line:
2727
#'
2828
#' - `geom_vline()`: `xintercept`
@@ -141,8 +141,10 @@ GeomAbline <- ggproto("GeomAbline", Geom,
141141
GeomSegment$draw_panel(unique(data), panel_params, coord, lineend = lineend)
142142
},
143143

144-
default_aes = aes(colour = "black", size = 0.5, linetype = 1, alpha = NA),
144+
default_aes = aes(colour = "black", linewidth = 0.5, linetype = 1, alpha = NA),
145145
required_aes = c("slope", "intercept"),
146146

147-
draw_key = draw_key_abline
147+
draw_key = draw_key_abline,
148+
149+
rename_size = TRUE
148150
)

R/geom-boxplot.r

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ GeomBoxplot <- ggproto("GeomBoxplot", Geom,
223223

224224
common <- list(
225225
colour = data$colour,
226-
size = data$size,
226+
linewidth = data$linewidth,
227227
linetype = data$linetype,
228228
fill = alpha(data$fill, data$alpha),
229229
group = data$group
@@ -293,8 +293,10 @@ GeomBoxplot <- ggproto("GeomBoxplot", Geom,
293293

294294
draw_key = draw_key_boxplot,
295295

296-
default_aes = aes(weight = 1, colour = "grey20", fill = "white", size = 0.5,
297-
alpha = NA, shape = 19, linetype = "solid"),
296+
default_aes = aes(weight = 1, colour = "grey20", fill = "white", size = NULL,
297+
alpha = NA, shape = 19, linetype = "solid", linewidth = 0.5),
298298

299-
required_aes = c("x|y", "lower|xlower", "upper|xupper", "middle|xmiddle", "ymin|xmin", "ymax|xmax")
299+
required_aes = c("x|y", "lower|xlower", "upper|xupper", "middle|xmiddle", "ymin|xmin", "ymax|xmax"),
300+
301+
rename_size = TRUE
300302
)

R/geom-col.r

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,5 +66,7 @@ GeomCol <- ggproto("GeomCol", GeomRect,
6666
lineend = lineend,
6767
linejoin = linejoin
6868
)
69-
}
69+
},
70+
71+
rename_size = TRUE
7072
)

R/geom-contour.r

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ GeomContour <- ggproto("GeomContour", GeomPath,
127127
default_aes = aes(
128128
weight = 1,
129129
colour = "#3366FF",
130-
size = 0.5,
130+
linewidth = 0.5,
131131
linetype = 1,
132132
alpha = NA
133133
)

R/geom-crossbar.r

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ GeomCrossbar <- ggproto("GeomCrossbar", Geom,
4040
GeomErrorbar$setup_data(data, params)
4141
},
4242

43-
default_aes = aes(colour = "black", fill = NA, size = 0.5, linetype = 1,
43+
default_aes = aes(colour = "black", fill = NA, linewidth = 0.5, linetype = 1,
4444
alpha = NA),
4545

4646
required_aes = c("x", "y", "ymin|xmin", "ymax|xmax"),
@@ -52,7 +52,7 @@ GeomCrossbar <- ggproto("GeomCrossbar", Geom,
5252
flipped_aes = FALSE) {
5353
data <- flip_data(data, flipped_aes)
5454

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

5757
has_notch <- !is.null(data$ynotchlower) && !is.null(data$ynotchupper) &&
5858
!is.na(data$ynotchlower) && !is.na(data$ynotchupper)
@@ -82,7 +82,7 @@ GeomCrossbar <- ggproto("GeomCrossbar", Geom,
8282
),
8383
alpha = rep(data$alpha, 11),
8484
colour = rep(data$colour, 11),
85-
size = rep(data$size, 11),
85+
linewidth = rep(data$linewidth, 11),
8686
linetype = rep(data$linetype, 11),
8787
fill = rep(data$fill, 11),
8888
group = rep(seq_len(nrow(data)), 11)
@@ -94,7 +94,7 @@ GeomCrossbar <- ggproto("GeomCrossbar", Geom,
9494
y = c(data$ymax, data$ymin, data$ymin, data$ymax, data$ymax),
9595
alpha = rep(data$alpha, 5),
9696
colour = rep(data$colour, 5),
97-
size = rep(data$size, 5),
97+
linewidth = rep(data$linewidth, 5),
9898
linetype = rep(data$linetype, 5),
9999
fill = rep(data$fill, 5),
100100
group = rep(seq_len(nrow(data)), 5) # each bar forms it's own group

R/geom-curve.r

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ geom_curve <- function(mapping = NULL, data = NULL,
4040
#' @usage NULL
4141
#' @export
4242
GeomCurve <- ggproto("GeomCurve", GeomSegment,
43-
default_aes = aes(colour = "black", size = 0.5, linetype = 1, alpha = NA),
43+
default_aes = aes(colour = "black", linewidth = 0.5, linetype = 1, alpha = NA),
4444
draw_panel = function(data, panel_params, coord, curvature = 0.5, angle = 90,
4545
ncp = 5, arrow = NULL, arrow.fill = NULL, lineend = "butt", na.rm = FALSE) {
4646

@@ -60,7 +60,7 @@ GeomCurve <- ggproto("GeomCurve", GeomSegment,
6060
gp = gpar(
6161
col = alpha(trans$colour, trans$alpha),
6262
fill = alpha(arrow.fill, trans$alpha),
63-
lwd = trans$size * .pt,
63+
lwd = trans$linewidth * .pt,
6464
lty = trans$linetype,
6565
lineend = lineend),
6666
arrow = arrow

R/geom-density2d.r

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ geom_density2d <- geom_density_2d
104104
#' @usage NULL
105105
#' @export
106106
GeomDensity2d <- ggproto("GeomDensity2d", GeomPath,
107-
default_aes = aes(colour = "#3366FF", size = 0.5, linetype = 1, alpha = NA)
107+
default_aes = aes(colour = "#3366FF", linewidth = 0.5, linetype = 1, alpha = NA)
108108
)
109109

110110
#' @export

R/geom-errorbar.r

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ geom_errorbar <- function(mapping = NULL, data = NULL,
2828
#' @usage NULL
2929
#' @export
3030
GeomErrorbar <- ggproto("GeomErrorbar", Geom,
31-
default_aes = aes(colour = "black", size = 0.5, linetype = 1, width = 0.5,
31+
default_aes = aes(colour = "black", linewidth = 0.5, linetype = 1, width = 0.5,
3232
alpha = NA),
3333

3434
draw_key = draw_key_path,
@@ -61,12 +61,14 @@ GeomErrorbar <- ggproto("GeomErrorbar", Geom,
6161
y = y,
6262
colour = rep(data$colour, each = 8),
6363
alpha = rep(data$alpha, each = 8),
64-
size = rep(data$size, each = 8),
64+
linewidth = rep(data$linewidth, each = 8),
6565
linetype = rep(data$linetype, each = 8),
6666
group = rep(1:(nrow(data)), each = 8),
6767
row.names = 1:(nrow(data) * 8)
6868
))
6969
data <- flip_data(data, flipped_aes)
7070
GeomPath$draw_panel(data, panel_params, coord, lineend = lineend)
71-
}
71+
},
72+
73+
rename_size = TRUE
7274
)

R/geom-errorbarh.r

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ geom_errorbarh <- function(mapping = NULL, data = NULL,
5151
#' @usage NULL
5252
#' @export
5353
GeomErrorbarh <- ggproto("GeomErrorbarh", Geom,
54-
default_aes = aes(colour = "black", size = 0.5, linetype = 1, height = 0.5,
54+
default_aes = aes(colour = "black", linewidth = 0.5, linetype = 1, height = 0.5,
5555
alpha = NA),
5656

5757
draw_key = draw_key_path,
@@ -73,10 +73,12 @@ GeomErrorbarh <- ggproto("GeomErrorbarh", Geom,
7373
y = as.vector(rbind(data$ymin, data$ymax, NA, data$y, data$y, NA, data$ymin, data$ymax)),
7474
colour = rep(data$colour, each = 8),
7575
alpha = rep(data$alpha, each = 8),
76-
size = rep(data$size, each = 8),
76+
linewidth = rep(data$linewidth, each = 8),
7777
linetype = rep(data$linetype, each = 8),
7878
group = rep(1:(nrow(data)), each = 8),
7979
row.names = 1:(nrow(data) * 8)
8080
)), panel_params, coord, lineend = lineend)
81-
}
81+
},
82+
83+
rename_size = TRUE
8284
)

R/geom-hex.r

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ GeomHex <- ggproto("GeomHex", Geom,
9090
gp = gpar(
9191
col = coords$colour,
9292
fill = alpha(coords$fill, coords$alpha),
93-
lwd = coords$size * .pt,
93+
lwd = coords$linewidth * .pt,
9494
lty = coords$linetype,
9595
lineend = lineend,
9696
linejoin = linejoin,
@@ -106,12 +106,14 @@ GeomHex <- ggproto("GeomHex", Geom,
106106
default_aes = aes(
107107
colour = NA,
108108
fill = "grey50",
109-
size = 0.5,
109+
linewidth = 0.5,
110110
linetype = 1,
111111
alpha = NA
112112
),
113113

114-
draw_key = draw_key_polygon
114+
draw_key = draw_key_polygon,
115+
116+
rename_size = TRUE
115117
)
116118

117119

R/geom-hline.r

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,10 @@ GeomHline <- ggproto("GeomHline", Geom,
5555
GeomSegment$draw_panel(unique(data), panel_params, coord, lineend = lineend)
5656
},
5757

58-
default_aes = aes(colour = "black", size = 0.5, linetype = 1, alpha = NA),
58+
default_aes = aes(colour = "black", linewidth = 0.5, linetype = 1, alpha = NA),
5959
required_aes = "yintercept",
6060

61-
draw_key = draw_key_path
61+
draw_key = draw_key_path,
62+
63+
rename_size = TRUE
6264
)

R/geom-linerange.r

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ geom_linerange <- function(mapping = NULL, data = NULL,
9191
#' @usage NULL
9292
#' @export
9393
GeomLinerange <- ggproto("GeomLinerange", Geom,
94-
default_aes = aes(colour = "black", size = 0.5, linetype = 1, alpha = NA),
94+
default_aes = aes(colour = "black", linewidth = 0.5, linetype = 1, alpha = NA),
9595

9696
draw_key = draw_key_linerange,
9797

@@ -118,5 +118,7 @@ GeomLinerange <- ggproto("GeomLinerange", Geom,
118118
data <- transform(data, xend = x, y = ymin, yend = ymax)
119119
data <- flip_data(data, flipped_aes)
120120
ggname("geom_linerange", GeomSegment$draw_panel(data, panel_params, coord, lineend = lineend))
121-
}
121+
},
122+
123+
rename_size = TRUE
122124
)

R/geom-map.r

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ GeomMap <- ggproto("GeomMap", GeomPolygon,
149149
gp = gpar(
150150
col = data$colour,
151151
fill = alpha(data$fill, data$alpha),
152-
lwd = data$size * .pt,
152+
lwd = data$linewidth * .pt,
153153
lineend = lineend,
154154
linejoin = linejoin,
155155
linemitre = linemitre

R/geom-path.r

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -130,12 +130,12 @@ geom_path <- function(mapping = NULL, data = NULL,
130130
GeomPath <- ggproto("GeomPath", Geom,
131131
required_aes = c("x", "y"),
132132

133-
default_aes = aes(colour = "black", size = 0.5, linetype = 1, alpha = NA),
133+
default_aes = aes(colour = "black", linewidth = 0.5, linetype = 1, alpha = NA),
134134

135135
handle_na = function(self, data, params) {
136136
# Drop missing values at the start or end of a line - can't drop in the
137137
# middle since you expect those to be shown by a break in the line
138-
complete <- stats::complete.cases(data[c("x", "y", "size", "colour", "linetype")])
138+
complete <- stats::complete.cases(data[c("x", "y", "linewidth", "colour", "linetype")])
139139
kept <- stats::ave(complete, data$group, FUN = keep_mid_true)
140140
data <- data[kept, ]
141141

@@ -170,7 +170,7 @@ GeomPath <- ggproto("GeomPath", Geom,
170170
linetype <- unique(df$linetype)
171171
new_data_frame(list(
172172
solid = identical(linetype, 1) || identical(linetype, "solid"),
173-
constant = nrow(unique(df[, c("alpha", "colour","size", "linetype")])) == 1
173+
constant = nrow(unique(df[, c("alpha", "colour","linewidth", "linetype")])) == 1
174174
), n = 1)
175175
})
176176
solid_lines <- all(attr$solid)
@@ -192,7 +192,7 @@ GeomPath <- ggproto("GeomPath", Geom,
192192
gp = gpar(
193193
col = alpha(munched$colour, munched$alpha)[!end],
194194
fill = alpha(munched$colour, munched$alpha)[!end],
195-
lwd = munched$size[!end] * .pt,
195+
lwd = munched$linewidth[!end] * .pt,
196196
lty = munched$linetype[!end],
197197
lineend = lineend,
198198
linejoin = linejoin,
@@ -207,7 +207,7 @@ GeomPath <- ggproto("GeomPath", Geom,
207207
gp = gpar(
208208
col = alpha(munched$colour, munched$alpha)[start],
209209
fill = alpha(munched$colour, munched$alpha)[start],
210-
lwd = munched$size[start] * .pt,
210+
lwd = munched$linewidth[start] * .pt,
211211
lty = munched$linetype[start],
212212
lineend = lineend,
213213
linejoin = linejoin,
@@ -217,7 +217,9 @@ GeomPath <- ggproto("GeomPath", Geom,
217217
}
218218
},
219219

220-
draw_key = draw_key_path
220+
draw_key = draw_key_path,
221+
222+
rename_size = TRUE
221223
)
222224

223225
# Trim false values from left and right: keep all values from

R/geom-pointrange.r

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ geom_pointrange <- function(mapping = NULL, data = NULL,
3030
#' @usage NULL
3131
#' @export
3232
GeomPointrange <- ggproto("GeomPointrange", Geom,
33-
default_aes = aes(colour = "black", size = 0.5, linetype = 1, shape = 19,
34-
fill = NA, alpha = NA, stroke = 1),
33+
default_aes = aes(colour = "black", size = 0.5, linewidth = 0.5, linetype = 1,
34+
shape = 19, fill = NA, alpha = NA, stroke = 1),
3535

3636
draw_key = draw_key_pointrange,
3737

0 commit comments

Comments
 (0)