From 8e2430817fe850e3613307f1fd744197eb2aa22d Mon Sep 17 00:00:00 2001 From: Hadley Wickham Date: Mon, 8 Aug 2022 11:58:09 -0500 Subject: [PATCH] Mention ordered factors --- factors.qmd | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/factors.qmd b/factors.qmd index f3038b2..01c7973 100644 --- a/factors.qmd +++ b/factors.qmd @@ -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.