forcats

Package forcats contain tools for working with EDAV - Categorical Data (R Type#Factors)

Recode Factor Levels

To recode the levels, do not use levels(x) <- c("A", "B"); use fct_recode() in forcats instead.

library(forcats)
x <- factor(c("G234", "G452", "G136"))  
y <- fct_recode(x, Physics = "G234", Math = "G452", Chemistry = "G136")  
y

Reorder Factor Levels

Include NA

When a factor contains NAs, NAs will not form a level. And when plotting, NA always orders first (in the top-down graph, and last in the left-right graphs). We can use fct_explicit_na(x, "NA") to make NA a real level with the name "NA". Then we can reorder the levels including "NA"

df <- data.frame(temperature = factor(c("cold", "warm", "hot", NA)),
                 count = c(15, 5, 22, 12))

df %>%
  mutate(temperature = fct_explicit_na(temperature, "NA") %>% # try comment this and the following lines
      fct_relevel("NA", "hot", "warm", "cold")) %>%
  ggplot(aes(x = temperature, y = count)) +
  geom_col() +
  coord_flip()

Lumping

Creative Commons License by zcysxy