Skip to content

Make title, subtitle, caption and tag as labs()'s named arguments #2669

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Aug 24, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions R/labels.r
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,14 @@ update_labels <- function(p, labels) {
#' changing other scale options.
#'
#' @param label The text for the axis, plot title or caption below the plot.
#' @param subtitle the text for the subtitle for the plot which will be
#' @param title The text for the title.
#' @param subtitle The text for the subtitle for the plot which will be
#' displayed below the title. Leave `NULL` for no subtitle.
#' @param ... A list of new name-value pairs. The name should either be
#' an aesthetic, or one of "title", "subtitle", "caption", or "tag".
#' @param caption The text for the caption which will be displayed in the
#' bottom-right of the plot by default.
#' @param tag The text for the tag label which will be displayed at the
#' top-left of the plot by default.
#' @param ... A list of new name-value pairs. The name should be an aesthetic.
#' @export
#' @examples
#' p <- ggplot(mtcars, aes(mpg, wt, colour = cyl)) + geom_point()
Expand All @@ -51,10 +55,20 @@ update_labels <- function(p, labels) {
#' # The plot tag appears at the top-left, and is typically used
#' # for labelling a subplot with a letter.
#' p + labs(title = "title", tag = "A")
labs <- function(...) {
#'
#' # If you want to remove a label, set it to NULL.
#' p + labs(title = "title") + labs(title = NULL)
labs <- function(..., title, subtitle, caption, tag) {
args <- list(...)
if (is.list(args[[1]])) args <- args[[1]]
if (length(args) > 0 && is.list(args[[1]])) args <- args[[1]]
args <- rename_aes(args)

# Since NULL should be preserved, we should wrap args with list()
if (!missing(title)) args["title"] <- list(title)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lionel- can you think of a more elegant way to write this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps remove the named arguments and let users pass them through ...?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's what we did previously 😄 We're adding them as named params here for better docs + autocomplete.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm I can't think of anything better.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, then I leave this as is :)

if (!missing(subtitle)) args["subtitle"] <- list(subtitle)
if (!missing(caption)) args["caption"] <- list(caption)
if (!missing(tag)) args["tag"] <- list(tag)

structure(args, class = "labels")
}

Expand Down
20 changes: 15 additions & 5 deletions man/labs.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions tests/testthat/test-labels.r
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@ test_that("setting guide labels works", {
expect_identical(labs(colour = "my label")$colour, "my label")
# American spelling
expect_identical(labs(color = "my label")$colour, "my label")

# No extra elements exists
expect_equivalent(labs(title = "my title"), list(title = "my title")) # formal argument
expect_equivalent(labs(colour = "my label"), list(colour = "my label")) # dot
expect_equivalent(labs(foo = "bar"), list(foo = "bar")) # non-existent param

# labs() can take a list as the first argument
expect_identical(labs(list(title = "my title", tag = "A)"))$tag, "A)")

# NULL is preserved
expect_equivalent(labs(title = NULL), list(title = NULL))
})


Expand Down