@@ -29,11 +29,21 @@ update_labels <- function(p, labels) {
29
29
# ' the first argument, the `name`). If you're changing other scale options, this
30
30
# ' is recommended.
31
31
# '
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.
37
47
# ' @export
38
48
# ' @examples
39
49
# ' p <- ggplot(mtcars, aes(mpg, wt, colour = cyl)) + geom_point()
@@ -52,10 +62,18 @@ update_labels <- function(p, labels) {
52
62
# ' # The plot tag appears at the top-left, and is typically used
53
63
# ' # for labelling a subplot with a letter.
54
64
# ' 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 ))]
58
75
args <- rename_aes(args )
76
+
59
77
structure(args , class = " labels" )
60
78
}
61
79
@@ -73,6 +91,6 @@ ylab <- function(label) {
73
91
74
92
# ' @rdname labs
75
93
# ' @export
76
- ggtitle <- function (label , subtitle = NULL ) {
94
+ ggtitle <- function (label , subtitle = waiver() ) {
77
95
labs(title = label , subtitle = subtitle )
78
96
}
0 commit comments