Setting manual order for a factor column does not work when the data frame is grouped

When you try to set order to a factor column (like 5, 4, 3) , it sometimes doesn’t change the order as you expect when you have a group by step in your steps. In the below example, it shows 5, 3, 4 as the order where you expect 5, 4, 3.

fct_relevel(as.character(gear), "5", "4", "3")

You can work around it by adding a ugnroup() before the fct_level step like below example.