Import your data

nuke <- read_csv("https://raw.githubusercontent.com/rfordatascience/tidytuesday/main/data/2019/2019-08-20/nuclear_explosions.csv")
## Rows: 2051 Columns: 16
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr  (6): country, region, source, purpose, name, type
## dbl (10): date_long, year, id_no, latitude, longitude, magnitude_body, magni...
## 
## ℹ 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.

Chapter 15

Create a factor

nuke_fct <- nuke %>% mutate(country = factor(country))

nuke_fct %>% distinct(country)
## # A tibble: 7 × 1
##   country
##   <fct>  
## 1 USA    
## 2 USSR   
## 3 UK     
## 4 FRANCE 
## 5 CHINA  
## 6 INDIA  
## 7 PAKIST
nuke_fct %>% count(country)
## # A tibble: 7 × 2
##   country     n
##   <fct>   <int>
## 1 CHINA      45
## 2 FRANCE    210
## 3 INDIA       3
## 4 PAKIST      2
## 5 UK         45
## 6 USA      1032
## 7 USSR      714

Modify factor order

Make two bar charts here - one before ordering another after Unordered Factor Levels

nuke_fct %>%

        ggplot(aes(y = country)) +
        geom_bar() +

    # Labeling
    labs(y = NULL, x = "Number of Explosions")

Order Factor Levels

nuke_fct %>%

        ggplot(aes(y = fct_reorder(.f = country, .x = year))) +
        geom_bar() +

    # Labeling
    labs(y = NULL, x = "Number of Explosions")

Modify factor levels

Show examples of three functions:

  • fct_recode
  • fct_collapse
  • fct_lump
nuke_fct %>% distinct(country)
## # A tibble: 7 × 1
##   country
##   <fct>  
## 1 USA    
## 2 USSR   
## 3 UK     
## 4 FRANCE 
## 5 CHINA  
## 6 INDIA  
## 7 PAKIST
# Recode
nuke_fct %>%

    # Rename Levels
    mutate(country_rev = fct_recode(country, "United States" = "USA")) %>%
    select(country, country_rev) %>%
    filter (country == "USA")
## # A tibble: 1,032 × 2
##    country country_rev  
##    <fct>   <fct>        
##  1 USA     United States
##  2 USA     United States
##  3 USA     United States
##  4 USA     United States
##  5 USA     United States
##  6 USA     United States
##  7 USA     United States
##  8 USA     United States
##  9 USA     United States
## 10 USA     United States
## # ℹ 1,022 more rows
# Collapse Multiple Levels into One
nuke_fct %>%

    mutate(country_col = fct_collapse(country, "South Asia" = c("INDIA", "PAKISTAN"))) %>%
    count(country_col)
## Warning: There was 1 warning in `mutate()`.
## ℹ In argument: `country_col = fct_collapse(country, `South Asia` = c("INDIA",
##   "PAKISTAN"))`.
## Caused by warning:
## ! Unknown levels in `f`: PAKISTAN
## # A tibble: 7 × 2
##   country_col     n
##   <fct>       <int>
## 1 CHINA          45
## 2 FRANCE        210
## 3 South Asia      3
## 4 PAKIST          2
## 5 UK             45
## 6 USA          1032
## 7 USSR          714
# Lump Small Levels into Other Levels
nuke_fct %>% count(country)
## # A tibble: 7 × 2
##   country     n
##   <fct>   <int>
## 1 CHINA      45
## 2 FRANCE    210
## 3 INDIA       3
## 4 PAKIST      2
## 5 UK         45
## 6 USA      1032
## 7 USSR      714
nuke_fct %>% mutate(country_lump = fct_lump(country)) %>% count(country_lump)
## # A tibble: 2 × 2
##   country_lump     n
##   <fct>        <int>
## 1 USA           1032
## 2 Other         1019

Chapter 16

No need to do anything here.