Mention ordered factors

This commit is contained in:
Hadley Wickham 2022-08-08 11:58:09 -05:00
parent fc80f12bec
commit 8e2430817f
1 changed files with 18 additions and 0 deletions

View File

@ -410,3 +410,21 @@ Read the documentation to learn about `fct_lump_min()` and `fct_lump_prop()` whi
3. Notice there are 9 groups (excluding other) in the `fct_lump` example above.
Why not 10?
(Hint: type `?fct_lump`, and find the default for the argument `other_level` is "Other".)
## Ordered factors
Before we go on, there's a special type of factor that needs to be mentioned briefly: ordered factors.
Ordered factors, created with `ordered()`, imply a strict ordering of levels such that the first level is "less than" the second level and so on.
You can recognize them when printing because they use `<` between the factor levels:
```{r}
ordered(c("a", "b", "c"))
```
In practice, `ordered()` factors behave very similarly to regular factors.
There are only two places where you might notice different behaviour:
- If you map an ordered factor to color or fill in ggplot2, it will default to `scale_color_viridis()`/`scale_fill_viridis()`, a color scale that implies a ranking.
- If you use an ordered function in a linear model, it will use "polygonal contrasts". These are midly useful, but you are unlikely to have heard of them unless you have a PhD in Statistics, and even then you probably don't routinely interpret them.
Given the arguable utility of these differences, we don't generally recommend using ordered factors.