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

fct_infreq() should support weights #261

Closed
krlmlr opened this issue Apr 27, 2020 · 1 comment · Fixed by #330
Closed

fct_infreq() should support weights #261

krlmlr opened this issue Apr 27, 2020 · 1 comment · Fixed by #330
Labels
feature a feature request or enhancement help wanted ❤️ we'd love your help!
Milestone

Comments

@krlmlr
Copy link
Member

krlmlr commented Apr 27, 2020

similarly to fct_lump() .

library(forcats)

x <- letters[c(1:3, 2)]
w <- c(4, 1, 3, 1)

x
#> [1] "a" "b" "c" "b"
w
#> [1] 4 1 3 1

fct_infreq(x)
#> [1] a b c b
#> Levels: b a c

fct_infreq(x, w = w)
#> Error in fct_infreq(x, w = w): unused argument (w = w)

# Should behave like:
fct_reorder(x, w, sum, .desc = TRUE)
#> [1] a b c b
#> Levels: a c b

Created on 2020-04-27 by the reprex package (v0.3.0)

@hadley hadley added the feature a feature request or enhancement label Dec 9, 2020
@hadley hadley added the help wanted ❤️ we'd love your help! label Mar 2, 2022
@twest820
Copy link

twest820 commented Dec 1, 2022

Thanks @krlmlr. I just hit this with geom_bar(aes(y = fct_infreq(<factor>), weight = <weight>)), which doesn't sort the discrete axis correctly, presumably because ggplot 3.4.0 isn't flowing weight to fct_infreq(). fct_reorder() handles this case correctly.

geom_bar(aes(y = fct_reorder(<factor>, <weight>, sum), weight = <weight>))

It seems perhaps also worth noting the reordering FAQ entry for geom_bar() doesn't consider this case. So, depending what happens with this issue, it may be worth revising the FAQ.

@hadley hadley added this to the v1.0.0 milestone Jan 3, 2023
@hadley hadley changed the title Support weights in fct_infreq() fct_infreq() should support weights Jan 3, 2023
hadley added a commit that referenced this issue Jan 3, 2023
hadley added a commit that referenced this issue Jan 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature a feature request or enhancement help wanted ❤️ we'd love your help!
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants