Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 34 additions & 34 deletions content/blog/2023/forcats-1-0-0/index.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ photo:
author: Diego Morales

# one of: "deep-dive", "learn", "package", "programming", "roundup", or "other"
categories: [package]
categories: [package]
tags: [forcats]
---

Expand Down Expand Up @@ -94,15 +94,15 @@ In the plot below, I've attempted to use `fct_infreq()` to reorder the levels of
#| fig.alt: >
#| The bar chart of hair color, now ordered so that the least
#| frequent colours come first and the most frequent colors come last.
#| This makes it easy to see that the most common hair color is none
#| This makes it easy to see that the most common hair color is none
#| (~35), followed by brown (~18), then black (~12). Surprisingly,
#| NAs are at the top of the graph, even though there are ~5 NAs and
#| NAs are at the top of the graph, even though there are ~5 NAs and
#| other colors have smaller values.
library(ggplot2)
library(dplyr, warn.conflicts = FALSE)

ggplot(starwars, aes(y = fct_rev(fct_infreq(hair_color)))) +
geom_bar() +
ggplot(starwars, aes(y = fct_rev(fct_infreq(hair_color)))) +
geom_bar() +
labs(y = "Hair color")
```

Expand All @@ -114,10 +114,10 @@ We can make `fct_infreq()` do what we want by moving the `NA` from the values to
#| fig-asp: 1
#| fig-width: 4
#| fig-alt: >
#| The bar chart of hair color, now ordered so that NAs are
#| The bar chart of hair color, now ordered so that NAs are
#| ordered where you'd expect: in between white (4) and black (12).
ggplot(starwars, aes(y = fct_rev(fct_infreq(fct_na_value_to_level(hair_color))))) +
geom_bar() +
ggplot(starwars, aes(y = fct_rev(fct_infreq(fct_na_value_to_level(hair_color))))) +
geom_bar() +
labs(y = "Hair color")
```

Expand All @@ -127,15 +127,15 @@ That code is getting a little verbose so lets pull it out into a separate dplyr
#| fig-asp: 1
#| fig-width: 4
#| results: false
starwars |>
starwars |>
mutate(
hair_color = hair_color |>
fct_na_value_to_level() |>
fct_infreq() |>
hair_color = hair_color |>
fct_na_value_to_level() |>
fct_infreq() |>
fct_rev()
) |>
ggplot(aes(y = hair_color)) +
geom_bar() +
) |>
ggplot(aes(y = hair_color)) +
geom_bar() +
labs(y = "Hair color")
```

Expand All @@ -148,16 +148,16 @@ I've left the (Other) category as a bar at the end, but if I wanted to I could c
#| fig-alt: >
#| The bar chart of hair color, with NA hair colour now labelled as (Unknown)
#| and the low frequency bars lumped into (Other).
starwars |>
starwars |>
mutate(
hair_color = hair_color |>
fct_na_value_to_level("(Unknown)") |>
fct_infreq() |>
fct_lump_min(2, other_level = "(Other)") |>
fct_rev()
) |>
ggplot(aes(y = hair_color)) +
geom_bar() +
hair_color = hair_color |>
fct_na_value_to_level("(Unknown)") |>
fct_infreq() |>
fct_lump_min(2, other_level = "(Other)") |>
fct_rev()
) |>
ggplot(aes(y = hair_color)) +
geom_bar() +
labs(y = "Hair color")
```

Expand All @@ -169,17 +169,17 @@ One way to fix that is with `fct_na_level_to_value()`:
#| fig-alt: >
#| The bar chart of hair color, with "unknown" hair colour now lumped in
#| with (Unknown) instead of other
starwars |>
starwars |>
mutate(
hair_color = hair_color |>
fct_na_level_to_value("Unknown") |>
fct_na_value_to_level("(Unknown)") |>
fct_infreq() |>
fct_lump_min(2, other_level = "(Other)") |>
fct_rev()
) |>
ggplot(aes(y = hair_color)) +
geom_bar() +
hair_color = hair_color |>
fct_na_level_to_value("Unknown") |>
fct_na_value_to_level("(Unknown)") |>
fct_infreq() |>
fct_lump_min(2, other_level = "(Other)") |>
fct_rev()
) |>
ggplot(aes(y = hair_color)) +
geom_bar() +
labs(y = "Hair color")
```

Expand Down
677 changes: 677 additions & 0 deletions content/blog/2023/forcats-1-0-0/index.html

Large diffs are not rendered by default.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading