Skip to content

Commit 5ebe979

Browse files
authored
rename size -> linewidth in element_rect and element_line (#4956)
1 parent de4c225 commit 5ebe979

File tree

11 files changed

+91
-61
lines changed

11 files changed

+91
-61
lines changed

R/theme-defaults.r

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,12 @@ theme_grey <- function(base_size = 11, base_family = "",
117117
# Elements in this first block aren't used directly, but are inherited
118118
# by others
119119
line = element_line(
120-
colour = "black", size = base_line_size,
120+
colour = "black", linewidth = base_line_size,
121121
linetype = 1, lineend = "butt"
122122
),
123123
rect = element_rect(
124124
fill = "white", colour = "black",
125-
size = base_rect_size, linetype = 1
125+
linewidth = base_rect_size, linetype = 1
126126
),
127127
text = element_text(
128128
family = base_family, face = "plain",
@@ -190,7 +190,7 @@ theme_grey <- function(base_size = 11, base_family = "",
190190
panel.background = element_rect(fill = "grey92", colour = NA),
191191
panel.border = element_blank(),
192192
panel.grid = element_line(colour = "white"),
193-
panel.grid.minor = element_line(size = rel(0.5)),
193+
panel.grid.minor = element_line(linewidth = rel(0.5)),
194194
panel.spacing = unit(half_line, "pt"),
195195
panel.spacing.x = NULL,
196196
panel.spacing.y = NULL,
@@ -264,7 +264,7 @@ theme_bw <- function(base_size = 11, base_family = "",
264264
panel.border = element_rect(fill = NA, colour = "grey20"),
265265
# make gridlines dark, same contrast with white as in theme_grey
266266
panel.grid = element_line(colour = "grey92"),
267-
panel.grid.minor = element_line(size = rel(0.5)),
267+
panel.grid.minor = element_line(linewidth = rel(0.5)),
268268
# contour strips to match panel contour
269269
strip.background = element_rect(fill = "grey85", colour = "grey20"),
270270
# match legend key to background
@@ -292,15 +292,15 @@ theme_linedraw <- function(base_size = 11, base_family = "",
292292
theme(
293293
# black text and ticks on the axes
294294
axis.text = element_text(colour = "black", size = rel(0.8)),
295-
axis.ticks = element_line(colour = "black", size = rel(0.5)),
295+
axis.ticks = element_line(colour = "black", linewidth = rel(0.5)),
296296
# NB: match the *visual* thickness of axis ticks to the panel border
297297
# 0.5 clipped looks like 0.25
298298

299299
# pure black panel border and grid lines, but thinner
300-
panel.border = element_rect(fill = NA, colour = "black", size = rel(1)),
300+
panel.border = element_rect(fill = NA, colour = "black", linewidth = rel(1)),
301301
panel.grid = element_line(colour = "black"),
302-
panel.grid.major = element_line(size = rel(0.1)),
303-
panel.grid.minor = element_line(size = rel(0.05)),
302+
panel.grid.major = element_line(linewidth = rel(0.1)),
303+
panel.grid.minor = element_line(linewidth = rel(0.05)),
304304

305305
# strips with black background and white text
306306
strip.background = element_rect(fill = "black"),
@@ -331,15 +331,15 @@ theme_light <- function(base_size = 11, base_family = "",
331331
theme(
332332
# white panel with light grey border
333333
panel.background = element_rect(fill = "white", colour = NA),
334-
panel.border = element_rect(fill = NA, colour = "grey70", size = rel(1)),
334+
panel.border = element_rect(fill = NA, colour = "grey70", linewidth = rel(1)),
335335
# light grey, thinner gridlines
336336
# => make them slightly darker to keep acceptable contrast
337337
panel.grid = element_line(colour = "grey87"),
338-
panel.grid.major = element_line(size = rel(0.5)),
339-
panel.grid.minor = element_line(size = rel(0.25)),
338+
panel.grid.major = element_line(linewidth = rel(0.5)),
339+
panel.grid.minor = element_line(linewidth = rel(0.25)),
340340

341341
# match axes ticks thickness to gridlines and colour to panel border
342-
axis.ticks = element_line(colour = "grey70", size = rel(0.5)),
342+
axis.ticks = element_line(colour = "grey70", linewidth = rel(0.5)),
343343

344344
# match legend key to panel.background
345345
legend.key = element_rect(fill = "white", colour = NA),
@@ -377,11 +377,11 @@ theme_dark <- function(base_size = 11, base_family = "",
377377
# inverse grid lines contrast compared to theme_grey
378378
# make them thinner and try to keep the same visual contrast as in theme_light
379379
panel.grid = element_line(colour = "grey42"),
380-
panel.grid.major = element_line(size = rel(0.5)),
381-
panel.grid.minor = element_line(size = rel(0.25)),
380+
panel.grid.major = element_line(linewidth = rel(0.5)),
381+
panel.grid.minor = element_line(linewidth = rel(0.25)),
382382

383383
# match axes ticks thickness to gridlines
384-
axis.ticks = element_line(colour = "grey20", size = rel(0.5)),
384+
axis.ticks = element_line(colour = "grey20", linewidth = rel(0.5)),
385385

386386
# match legend key to panel.background
387387
legend.key = element_rect(fill = "grey50", colour = NA),
@@ -441,13 +441,13 @@ theme_classic <- function(base_size = 11, base_family = "",
441441
panel.grid.minor = element_blank(),
442442

443443
# show axes
444-
axis.line = element_line(colour = "black", size = rel(1)),
444+
axis.line = element_line(colour = "black", linewidth = rel(1)),
445445

446446
# match legend key to panel.background
447447
legend.key = element_blank(),
448448

449449
# simple, black and white strips
450-
strip.background = element_rect(fill = "white", colour = "black", size = rel(2)),
450+
strip.background = element_rect(fill = "white", colour = "black", linewidth = rel(2)),
451451
# NB: size is 1 but clipped, it looks like the 0.5 of the axes
452452

453453
complete = TRUE
@@ -531,12 +531,12 @@ theme_test <- function(base_size = 11, base_family = "",
531531

532532
t <- theme(
533533
line = element_line(
534-
colour = "black", size = base_line_size,
534+
colour = "black", linewidth = base_line_size,
535535
linetype = 1, lineend = "butt"
536536
),
537537
rect = element_rect(
538538
fill = "white", colour = "black",
539-
size = base_rect_size, linetype = 1
539+
linewidth = base_rect_size, linetype = 1
540540
),
541541
text = element_text(
542542
family = base_family, face = "plain",

R/theme-elements.r

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
#'
1515
#' @param fill Fill colour.
1616
#' @param colour,color Line/border colour. Color is an alias for colour.
17-
#' @param size Line/border size in mm; text size in pts.
17+
#' @param linewidth Line/border size in mm.
18+
#' @param size text size in pts.
1819
#' @param inherit.blank Should this element inherit the existence of an
1920
#' `element_blank` among its parents? If `TRUE` the existence of
2021
#' a blank element among its parents will cause this element to be blank as
@@ -43,7 +44,7 @@
4344
#' plot.background = element_rect(
4445
#' fill = "grey90",
4546
#' colour = "black",
46-
#' size = 1
47+
#' linewidth = 1
4748
#' )
4849
#' )
4950
#' @name element
@@ -61,12 +62,17 @@ element_blank <- function() {
6162

6263
#' @export
6364
#' @rdname element
64-
element_rect <- function(fill = NULL, colour = NULL, size = NULL,
65-
linetype = NULL, color = NULL, inherit.blank = FALSE) {
65+
element_rect <- function(fill = NULL, colour = NULL, linewidth = NULL,
66+
linetype = NULL, color = NULL, inherit.blank = FALSE, size = deprecated()) {
67+
68+
if (lifecycle::is_present(size)) {
69+
lifecycle::deprecate_warn("3.4.0", "element_rect(size)", "element_rect(linewidth)")
70+
linewidth <- size
71+
}
6672

6773
if (!is.null(color)) colour <- color
6874
structure(
69-
list(fill = fill, colour = colour, size = size, linetype = linetype,
75+
list(fill = fill, colour = colour, linewidth = linewidth, linetype = linetype,
7076
inherit.blank = inherit.blank),
7177
class = c("element_rect", "element")
7278
)
@@ -80,13 +86,18 @@ element_rect <- function(fill = NULL, colour = NULL, size = NULL,
8086
#' lengths in consecutive positions in the string.
8187
#' @param lineend Line end Line end style (round, butt, square)
8288
#' @param arrow Arrow specification, as created by [grid::arrow()]
83-
element_line <- function(colour = NULL, size = NULL, linetype = NULL,
84-
lineend = NULL, color = NULL, arrow = NULL, inherit.blank = FALSE) {
89+
element_line <- function(colour = NULL, linewidth = NULL, linetype = NULL,
90+
lineend = NULL, color = NULL, arrow = NULL, inherit.blank = FALSE, size = deprecated()) {
91+
92+
if (lifecycle::is_present(size)) {
93+
lifecycle::deprecate_warn("3.4.0", "element_line(size)", "element_line(linewidth)")
94+
linewidth <- size
95+
}
8596

8697
if (!is.null(color)) colour <- color
8798
if (is.null(arrow)) arrow <- FALSE
8899
structure(
89-
list(colour = colour, size = size, linetype = linetype, lineend = lineend,
100+
list(colour = colour, linewidth = linewidth, linetype = linetype, lineend = lineend,
90101
arrow = arrow, inherit.blank = inherit.blank),
91102
class = c("element_line", "element")
92103
)
@@ -201,11 +212,16 @@ element_grob.element_blank <- function(element, ...) zeroGrob()
201212
#' @export
202213
element_grob.element_rect <- function(element, x = 0.5, y = 0.5,
203214
width = 1, height = 1,
204-
fill = NULL, colour = NULL, size = NULL, linetype = NULL, ...) {
215+
fill = NULL, colour = NULL, linewidth = NULL, linetype = NULL, ..., size = deprecated()) {
216+
217+
if (lifecycle::is_present(size)) {
218+
lifecycle::deprecate_warn("3.4.0", "element_grob.element_rect(size)", "element_grob.element_rect(linewidth)")
219+
linewidth <- size
220+
}
205221

206222
# The gp settings can override element_gp
207-
gp <- gpar(lwd = len0_null(size * .pt), col = colour, fill = fill, lty = linetype)
208-
element_gp <- gpar(lwd = len0_null(element$size * .pt), col = element$colour,
223+
gp <- gpar(lwd = len0_null(linewidth * .pt), col = colour, fill = fill, lty = linetype)
224+
element_gp <- gpar(lwd = len0_null(element$linewidth * .pt), col = element$colour,
209225
fill = element$fill, lty = element$linetype)
210226

211227
rectGrob(x, y, width, height, gp = modify_list(element_gp, gp), ...)
@@ -244,17 +260,22 @@ element_grob.element_text <- function(element, label = "", x = NULL, y = NULL,
244260

245261
#' @export
246262
element_grob.element_line <- function(element, x = 0:1, y = 0:1,
247-
colour = NULL, size = NULL, linetype = NULL, lineend = NULL,
248-
default.units = "npc", id.lengths = NULL, ...) {
263+
colour = NULL, linewidth = NULL, linetype = NULL, lineend = NULL,
264+
default.units = "npc", id.lengths = NULL, ..., size = deprecated()) {
265+
266+
if (lifecycle::is_present(size)) {
267+
lifecycle::deprecate_warn("3.4.0", "element_grob.element_line(size)", "element_grob.element_line(linewidth)")
268+
linewidth <- size
269+
}
249270

250271
# The gp settings can override element_gp
251272
gp <- gpar(
252273
col = colour, fill = colour,
253-
lwd = len0_null(size * .pt), lty = linetype, lineend = lineend
274+
lwd = len0_null(linewidth * .pt), lty = linetype, lineend = lineend
254275
)
255276
element_gp <- gpar(
256277
col = element$colour, fill = element$colour,
257-
lwd = len0_null(element$size * .pt), lty = element$linetype,
278+
lwd = len0_null(element$linewidth * .pt), lty = element$linetype,
258279
lineend = element$lineend
259280
)
260281
arrow <- if (is.logical(element$arrow) && !element$arrow) {

R/theme.r

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,9 +204,9 @@
204204
#'
205205
#' # Axes ----------------------------------------------------------------------
206206
#' # Change styles of axes texts and lines
207-
#' p1 + theme(axis.line = element_line(size = 3, colour = "grey80"))
207+
#' p1 + theme(axis.line = element_line(linewidth = 3, colour = "grey80"))
208208
#' p1 + theme(axis.text = element_text(colour = "blue"))
209-
#' p1 + theme(axis.ticks = element_line(size = 2))
209+
#' p1 + theme(axis.ticks = element_line(linewidth = 2))
210210
#'
211211
#' # Change the appearance of the y-axis title
212212
#' p1 + theme(axis.title.y = element_text(size = rel(1.5), angle = 90))
@@ -712,6 +712,11 @@ combine_elements <- function(e1, e2) {
712712
e1$size <- e2$size * unclass(e1$size)
713713
}
714714

715+
# Calculate relative linewidth
716+
if (is.rel(e1$linewidth)) {
717+
e1$linewidth <- e2$linewidth * unclass(e1$linewidth)
718+
}
719+
715720
e1
716721
}
717722

man/element.Rd

Lines changed: 10 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/theme.Rd

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/_snaps/theme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
---
2020

21-
Theme element `test` has "NULL" property without default: fill, colour, size, and linetype
21+
Theme element `test` has "NULL" property without default: fill, colour, linewidth, and linetype
2222

2323
# element tree can be modified
2424

tests/testthat/test-guides.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ test_that("colorsteps and bins checks the breaks format", {
243243
# Visual tests ------------------------------------------------------------
244244

245245
test_that("axis guides are drawn correctly", {
246-
theme_test_axis <- theme_test() + theme(axis.line = element_line(size = 0.5))
246+
theme_test_axis <- theme_test() + theme(axis.line = element_line(linewidth = 0.5))
247247
test_draw_axis <- function(n_breaks = 3,
248248
break_positions = seq_len(n_breaks) / (n_breaks + 1),
249249
labels = as.character,
@@ -330,7 +330,7 @@ test_that("axis guides are drawn correctly in plots", {
330330
ggplot(mtcars, aes(wt, mpg)) +
331331
geom_point() +
332332
theme_test() +
333-
theme(axis.line = element_line(size = 5, lineend = "square"))
333+
theme(axis.line = element_line(linewidth = 5, lineend = "square"))
334334
)
335335
})
336336

tests/testthat/test-scales-breaks-labels.r

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ test_that("minor breaks draw correctly", {
282282
)
283283
theme <- theme_test() +
284284
theme(
285-
panel.grid.major = element_line(colour = "grey30", size = 0.5),
285+
panel.grid.major = element_line(colour = "grey30", linewidth = 0.5),
286286
panel.grid.minor = element_line(colour = "grey70")
287287
)
288288

0 commit comments

Comments
 (0)