How to set the order and value of factors in R using factor and fct_reorder (CC148)
Channel: Riffomonas Project
Date: 2021-09-23

Ever wonder how to set the order of categorical variables in a figure generated in R? Factors! Changing the order of categorical values can be useful in figures when the data are ordinal or when you want to order the values by another variable. Pat will use the base R factor function and the forcats fct_reorder function. The data depict the percentage of people in 15 countries who would be willing to receive the COVID-19 vaccine as of August and October of 2020.

Pat uses #factor and the #fct_reorder function from the #forcats package as well as functions from the #ggplot2, #dplyr, showtext, ggtext and glue packages in #RStudio. The accompanying blog post can be found at https://www.riffomonas.org/code_club/2021-09-23-factors.

Want more practice on the concepts covered in Code Club? You can sign up for my weekly newsletter at https://shop.riffomonas.org/youtube to get practice problems, tips, and insights.

R: https://r-project.org
RStudio: https://rstudio.com
Workshops: https://www.mothur.org/wiki/workshops

Here's a link for the chartr newsletter: https://www.chartr.co

If you're interested in taking an upcoming 3 day R workshop be sure to check out our schedule at https://riffomonas.org/workshops/


You can also find complete tutorials for learning R with the tidyverse using...
Microbial ecology data: https://www.riffomonas.org/minimalR/
General data: https://www.riffomonas.org/generalR/

0:00 Using factors to represent categorical and ordinal data
2:56 Manually setting the order and labels of values in a factor with factor
5:51 Setting the order of levels in a factor with fct_reorder
11:00 Critique of figure