Chapter 15 Factors

Creating factors

Import data

myData <- read_csv("../00_data/myData.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.

Modify factor order

##transform data: calculate average TV hours by religion
colony_n_by_state <- myData %>%
    
    group_by(state) %>%
    summarise(
        colony_n = mean(colony_n, na.rm = TRUE)
    )

colony_n_by_state
## # A tibble: 47 × 2
##    state       colony_n
##    <chr>          <dbl>
##  1 Alabama         7900
##  2 Arizona        28260
##  3 Arkansas       21580
##  4 California    940400
##  5 Colorado       19580
##  6 Connecticut     3516
##  7 Florida       244480
##  8 Georgia       122680
##  9 Hawaii         15480
## 10 Idaho          96680
## # … with 37 more rows
#plot
colony_n_by_state %>%
    ggplot(aes(x = colony_n, y = state))+
    geom_point()

Ordered Factor Levels

colony_n_by_state %>%
    
    ggplot(aes(x = colony_n, y = fct_reorder(.f = state, .x = colony_n)))+
    geom_point()+
    
    #Labeling
    labs(y = NULL, x = "Mean Colony Numbers")

Moving a single level to the front

colony_n_by_state %>%
    
    ggplot(aes(x = colony_n,
               y = fct_reorder(.f = state, .x = colony_n) %>%
               fct_relevel("Don't Know"))) +
    geom_point() +
    
    #Labeling
    labs(y = NULL, x = "Mean Colony Numbers")
## Warning: 1 unknown level in `f`: Don't Know
## 1 unknown level in `f`: Don't Know