You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
# 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)) +
The text was updated successfully, but these errors were encountered:
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:
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")
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.
First, what a useful and easy-to-use package! Thank you!
I am trying to pass arguments to
ggplot2::scale_x_discrete()
through the...
oftidytext::scale_x_reordered()
, but it is throwing an error about matching multiple arguments forlabels
.Here is a minimal reproducible example from the
reorder_within()
blog post for tidytext:The output of the above code is:
Perhaps the error is because
scale_x_reordered()
is already usinglabels
to remove the artificial suffix that was appended byreorder_within()
?Would it not be possible to allow the user to override the
labels
argument by passing their own customlabels
argument toscale_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 thelabels
argument inscale_x_reordered()
as follows:The text was updated successfully, but these errors were encountered: