Import your data

data <- read_csv("https://raw.githubusercontent.com/rfordatascience/tidytuesday/main/data/2022/2022-01-11/colony.csv")
## Rows: 1222 Columns: 10
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (2): months, state
## dbl (8): year, colony_n, colony_max, colony_lost, colony_lost_pct, colony_ad...
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
data
## # A tibble: 1,222 × 10
##     year months        state     colony_n colony_max colony_lost colony_lost_pct
##    <dbl> <chr>         <chr>        <dbl>      <dbl>       <dbl>           <dbl>
##  1  2015 January-March Alabama       7000       7000        1800              26
##  2  2015 January-March Arizona      35000      35000        4600              13
##  3  2015 January-March Arkansas     13000      14000        1500              11
##  4  2015 January-March Californ…  1440000    1690000      255000              15
##  5  2015 January-March Colorado      3500      12500        1500              12
##  6  2015 January-March Connecti…     3900       3900         870              22
##  7  2015 January-March Florida     305000     315000       42000              13
##  8  2015 January-March Georgia     104000     105000       14500              14
##  9  2015 January-March Hawaii       10500      10500         380               4
## 10  2015 January-March Idaho        81000      88000        3700               4
## # ℹ 1,212 more rows
## # ℹ 3 more variables: colony_added <dbl>, colony_reno <dbl>,
## #   colony_reno_pct <dbl>

Chapter 15

Create a factor

Modify factor order

Make two bar charts here - one before ordering another after

data %>%
    count(months) %>%

    ggplot(aes(n, months)) + 
    geom_col()

data %>%
    count(months) %>%

    ggplot(aes(n, fct_reorder(months, n))) + 
    geom_col()

data %>%
  mutate(months = months %>% fct_infreq() %>% fct_rev()) %>%
  ggplot(aes(months)) +
   geom_bar()

Modify factor levels

Show examples of three functions:

  • fct_recode
data %>%
  mutate(months = fct_recode(months,
    "Jan-Mar"    = "January-March",
    "Apr-Jun"      = "April-June",
    "Jul-Sep" = "July-September",
    "Oct-Dec" = "October-December",
  )) %>%
  count(months)
## # A tibble: 4 × 2
##   months      n
##   <fct>   <int>
## 1 Apr-Jun   329
## 2 Jan-Mar   329
## 3 Jul-Sep   282
## 4 Oct-Dec   282
  • fct_collapse
data %>%
  mutate(state = fct_collapse(state,
    East_coast = c("Maine", "Connecticut", "Massachusetts"),
    West_coast = c("California", "Arizona", "Colorado", "Idaho", "Montana"),
  )) %>%
  count(state)
## # A tibble: 41 × 2
##    state          n
##    <fct>      <int>
##  1 Alabama       26
##  2 West_coast   130
##  3 Arkansas      26
##  4 East_coast    78
##  5 Florida       26
##  6 Georgia       26
##  7 Hawaii        26
##  8 Illinois      26
##  9 Indiana       26
## 10 Iowa          26
## # ℹ 31 more rows
  • fct_lump
data %>%
  mutate(months = fct_lump(months,n=1)) %>%
  count(months)
## # A tibble: 3 × 2
##   months            n
##   <fct>         <int>
## 1 April-June      329
## 2 January-March   329
## 3 Other           564

Chapter 16

No need to do anything here.