@@ -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}
0 commit comments