Skip to content

Commit 6500a30

Browse files
yutannihilationclauswilke
authored andcommitted
Make title, subtitle, caption and tag as labs()'s named arguments (#2669)
* add tests for labs() * make label params as the formal arguments of labs() * respect missingness in ggtitle() * improve description about labs() * use waive() to represent missingness in labs() * fix a typo * apply tidyverse style * Enable !!! in labs() * Add a news bullet * Add the Oxford comma
1 parent adce4e2 commit 6500a30

File tree

4 files changed

+77
-16
lines changed

4 files changed

+77
-16
lines changed

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@
5050
coordinates of the geometries. You can customize the calculation method via
5151
`fun.geometry` argument (@yutannihilation, #2761).
5252

53+
* `labs()` now has named arguments `title`, `subtitle`, `caption`, and `tag`.
54+
Also, `labs()` now accepts tidyeval (@yutannihilation, #2669).
55+
5356
# ggplot2 3.0.0
5457

5558
## Breaking changes

R/labels.r

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,21 @@ update_labels <- function(p, labels) {
2929
#' the first argument, the `name`). If you're changing other scale options, this
3030
#' is recommended.
3131
#'
32-
#' @param label The text for the axis, plot title or caption below the plot.
33-
#' @param subtitle the text for the subtitle for the plot which will be
34-
#' displayed below the title. Leave `NULL` for no subtitle.
35-
#' @param ... A list of new name-value pairs. The name should either be
36-
#' an aesthetic, or one of "title", "subtitle", "caption", or "tag".
32+
#' If a plot already has a title, subtitle, caption, etc., and you want to
33+
#' remove it, you can do so by setting the respective argument to `NULL`. For
34+
#' example, if plot `p` has a subtitle, then `p + labs(subtitle = NULL)` will
35+
#' remove the subtitle from the plot.
36+
#'
37+
#' @param label The title of the respective axis (for `xlab()` or `ylab()`) or
38+
#' of the plot (for `ggtitle()`).
39+
#' @param title The text for the title.
40+
#' @param subtitle The text for the subtitle for the plot which will be
41+
#' displayed below the title.
42+
#' @param caption The text for the caption which will be displayed in the
43+
#' bottom-right of the plot by default.
44+
#' @param tag The text for the tag label which will be displayed at the
45+
#' top-left of the plot by default.
46+
#' @param ... A list of new name-value pairs. The name should be an aesthetic.
3747
#' @export
3848
#' @examples
3949
#' p <- ggplot(mtcars, aes(mpg, wt, colour = cyl)) + geom_point()
@@ -52,10 +62,18 @@ update_labels <- function(p, labels) {
5262
#' # The plot tag appears at the top-left, and is typically used
5363
#' # for labelling a subplot with a letter.
5464
#' p + labs(title = "title", tag = "A")
55-
labs <- function(...) {
56-
args <- list(...)
57-
if (is.list(args[[1]])) args <- args[[1]]
65+
#'
66+
#' # If you want to remove a label, set it to NULL.
67+
#' p + labs(title = "title") + labs(title = NULL)
68+
labs <- function(..., title = waiver(), subtitle = waiver(), caption = waiver(), tag = waiver()) {
69+
args <- rlang::list2(..., title = title, subtitle = subtitle, caption = caption, tag = tag)
70+
71+
is_waive <- vapply(args, is.waive, logical(1))
72+
args <- args[!is_waive]
73+
# remove duplicated arguments
74+
args <- args[!duplicated(names(args))]
5875
args <- rename_aes(args)
76+
5977
structure(args, class = "labels")
6078
}
6179

@@ -73,6 +91,6 @@ ylab <- function(label) {
7391

7492
#' @rdname labs
7593
#' @export
76-
ggtitle <- function(label, subtitle = NULL) {
94+
ggtitle <- function(label, subtitle = waiver()) {
7795
labs(title = label, subtitle = subtitle)
7896
}

man/labs.Rd

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

tests/testthat/test-labels.r

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,29 @@ test_that("setting guide labels works", {
2525
expect_identical(labs(colour = "my label")$colour, "my label")
2626
# American spelling
2727
expect_identical(labs(color = "my label")$colour, "my label")
28+
29+
# No extra elements exists
30+
expect_equivalent(labs(title = "my title"), list(title = "my title")) # formal argument
31+
expect_equivalent(labs(colour = "my label"), list(colour = "my label")) # dot
32+
expect_equivalent(labs(foo = "bar"), list(foo = "bar")) # non-existent param
33+
34+
# labs() has list-splicing semantics
35+
params <- list(title = "my title", tag = "A)")
36+
expect_identical(labs(!!!params)$tag, "A)")
37+
38+
# NULL is preserved
39+
expect_equivalent(labs(title = NULL), list(title = NULL))
40+
41+
# ggtitle works in the same way as labs()
42+
expect_identical(ggtitle("my title")$title, "my title")
43+
expect_identical(
44+
ggtitle("my title", subtitle = "my subtitle")$subtitle,
45+
"my subtitle"
46+
)
47+
expect_equivalent(
48+
ggtitle("my title", subtitle = NULL),
49+
list(title = "my title", subtitle = NULL)
50+
)
2851
})
2952

3053

0 commit comments

Comments
 (0)