forcats - reorder labels

Hello,
I'm a bit confused trying to rearrange some factors.
This is the example:

set.seed(123)
fff=tibble(region=round(runif(20, min=1, max=4),0),
       geta=runif(20, min=50, max=150))
fff=fff %>% 
  mutate(region=factor(region)) %>%
  mutate(region=factor(region, 1:4 , c("a","b","c","d")))

fff               

fff %>% group_by(region) %>% count()
# A tibble: 4 x 2
# Groups:   region [4]
  region     n
  <fct>  <int>
1 a          3
2 b          6
3 c          5
4 d          6

But I want to reproduce this

This is the example:

...          

fff %>% group_by(region) %>% count()
# A tibble: 4 x 2
# Groups:   region [4]
  region     n
  <fct>  <int>
1 d          6
2 b          6
3 a          3
4 c          5

I run some code with fct_relevel o fct_reorder, but I can't achieve what I need.
Thanks for your time and interest.

I solved It using this


fff %>%  
mutate(region=fct_relevel(region,"b","d","a","c") ) %>%
group_by(region) %>% 
count()  

  

The main issue here is to declare
mutate(region=fct_relevel(region,"b","d","a","c") ) %>%
before the group_by command. That was my error.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.