Skip to content
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

Error in discrete_scale(...: formal argument "labels" matched by multiple actual arguments #200

Closed
mmp3 opened this issue Nov 3, 2021 · 3 comments

Comments

@mmp3
Copy link

mmp3 commented Nov 3, 2021

First, what a useful and easy-to-use package! Thank you!

I am trying to pass arguments to ggplot2::scale_x_discrete() through the ... of tidytext::scale_x_reordered(), but it is throwing an error about matching multiple arguments for labels.

Here is a minimal reproducible example from the reorder_within() blog post for tidytext:

# setup, directly from the blogpost:

library(tidyverse)
library(babynames)

top_names <- babynames %>%
    filter(year >= 1950,
           year < 1990) %>%
    mutate(decade = (year %/% 10) * 10) %>%
    group_by(decade) %>%
    count(name, wt = n, sort = TRUE) %>%
    ungroup


# The following is exactly like the blog post,
# except that I am passing the `labels` argument to `scale_x_reordered()`

library(tidytext)

top_names %>%
    group_by(decade) %>%
    top_n(15) %>%
    ungroup %>%
    mutate(decade = as.factor(decade),
           name = reorder_within(name, n, decade)) %>%
    ggplot(aes(name, n, fill = decade)) +
    geom_col(show.legend = FALSE) +
    facet_wrap(~decade, scales = "free_y") +
    coord_flip() +
    scale_x_reordered(labels= function(x) stringr::str_wrap(x,width=10)) +
    scale_y_continuous(expand = c(0,0)) +
    labs(y = "Number of babies per decade",
         x = NULL,
         title = "What were the most common baby names in each decade?",
         subtitle = "Via US Social Security Administration")

The output of the above code is:

Selecting by n
Error in discrete_scale(c("x", "xmin", "xmax", "xend"), "position_d",  :
  formal argument "labels" matched by multiple actual arguments

Perhaps the error is because scale_x_reordered() is already using labels to remove the artificial suffix that was appended by reorder_within()?

Would it not be possible to allow the user to override the labels argument by passing their own custom labels argument to scale_x_reorder()?

In that case, I would have to do the cleaning of the labels myself. So to follow the above example where I want to use str_wrap(), then I would have to override the labels argument in scale_x_reordered() as follows:

# assume that  sep = "___":
    scale_x_reordered(labels= function(x)  x %>%
                      str_replace("___[0-9]+$", "") %>%
                      stringr::str_wrap(width=10)) +
@juliasilge
Copy link
Owner

Thanks for this report @mmp3! I can see how that would be frustrating. In the commit above, I changed the scale_*_reordered() functions so they now take a function as an argument. This lets you do something like this:

library(tidyverse)
library(babynames)

top_names <- babynames %>%
  filter(year >= 1950,
         year < 1990) %>%
  mutate(decade = (year %/% 10) * 10) %>%
  group_by(decade) %>%
  count(name, wt = n, sort = TRUE) %>%
  ungroup



library(tidytext)

custom_labeler <- function(x) {
  x %>%
    str_replace("___[0-9]+$", "") %>%
    stringr::str_to_upper()
}

top_names %>%
  group_by(decade) %>%
  top_n(15) %>%
  ungroup %>%
  mutate(decade = as.factor(decade),
         name = str_wrap(name, width = 10),
         name = reorder_within(name, n, decade)) %>%
  ggplot(aes(n, name, fill = decade)) +
  geom_col(show.legend = FALSE) +
  facet_wrap(vars(decade), scales = "free_y") +
  scale_y_reordered(labels = custom_labeler)
#> Selecting by n

Created on 2021-11-04 by the reprex package (v2.0.1)

Folks can still use their own sep by passing in that arg to the existing little function, like scale_y_reordered(labels = ~ reorder_func(., sep = "CUSTOM_SEP_HERE")

@mmp3
Copy link
Author

mmp3 commented Nov 5, 2021

Awesome, that looks like the perfect solution. Thank you!

@github-actions
Copy link

This issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with a reprex: https://reprex.tidyverse.org) and link to this issue.

@github-actions github-actions bot locked and limited conversation to collaborators Mar 24, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants