Import your data

food <- read_excel("data/my_Data_updated.xlsx")
food
## # A tibble: 1,430 × 4
##    country     food_category consumption co2_emmission
##    <chr>       <chr>               <dbl>         <dbl>
##  1 Argentina   Beef                 55.5         1712 
##  2 Australia   Beef                 33.9         1045.
##  3 Albania     Beef                 22.5          694.
##  4 Iceland     Beef                 13.4          412.
##  5 New Zealand Beef                 22.5          694.
##  6 USA         Beef                 36.2         1118.
##  7 Uruguay     Beef                 29.1          898.
##  8 Luxembourg  Beef                 29.9          922.
##  9 Brazil      Beef                 39.2         1211.
## 10 Kazakhstan  Beef                 23.4          721.
## # ℹ 1,420 more rows

Chapter 15

Create a factor

# Transform data: calculate average consumption by food category
consumption_by_food <- food %>% 
    group_by(food_category) %>% 
    summarise(
        avg_consumption = mean(consumption, na.rm = TRUE)
    )
consumption_by_food
## # A tibble: 11 × 2
##    food_category            avg_consumption
##    <chr>                              <dbl>
##  1 Beef                              12.1  
##  2 Eggs                               8.16 
##  3 Fish                              17.3  
##  4 Lamb & Goat                        2.60 
##  5 Milk - inc. cheese               126.   
##  6 Nuts inc. Peanut Butter            4.14 
##  7 Pork                              16.1  
##  8 Poultry                           21.2  
##  9 Rice                              29.4  
## 10 Soybeans                           0.861
## 11 Wheat and Wheat Products          71.5
# Plot 
consumption_by_food %>% 
    ggplot(aes(x = avg_consumption, y = food_category)) +
    geom_point()

Modify factor order

Make two bar charts here - one before ordering another after

consumption_by_food %>% 
    ggplot(aes(x = avg_consumption, y = fct_reorder(.f = food_category, .x = avg_consumption))) + 
    geom_point() + 

    # Labeling
    labs(y = NULL, x = "Mean Consumption")

Modify factor levels

Show examples of three functions:

  • fct_recode
  • fct_collapse
  • fct_lump
food %>% distinct(food_category)
## # A tibble: 11 × 1
##    food_category           
##    <chr>                   
##  1 Beef                    
##  2 Eggs                    
##  3 Fish                    
##  4 Lamb & Goat             
##  5 Milk - inc. cheese      
##  6 Nuts inc. Peanut Butter 
##  7 Pork                    
##  8 Poultry                 
##  9 Rice                    
## 10 Soybeans                
## 11 Wheat and Wheat Products
# Recode 
food %>% 
    #Rename levels 
    mutate(category_rev = fct_recode(food_category, "Cattle" = "Beef")) %>% 
    select(food_category, category_rev) %>% 
    filter(food_category == "Beef")
## # A tibble: 130 × 2
##    food_category category_rev
##    <chr>         <fct>       
##  1 Beef          Cattle      
##  2 Beef          Cattle      
##  3 Beef          Cattle      
##  4 Beef          Cattle      
##  5 Beef          Cattle      
##  6 Beef          Cattle      
##  7 Beef          Cattle      
##  8 Beef          Cattle      
##  9 Beef          Cattle      
## 10 Beef          Cattle      
## # ℹ 120 more rows
# Collapse multiple levels into one 
food %>% 
    mutate(cat_col = fct_collapse(food_category, "Product" = c("Beef", "Other"))) %>% 
    select(food_category, cat_col) %>%
    filter(food_category != "Eggs")
## Warning: There was 1 warning in `mutate()`.
## ℹ In argument: `cat_col = fct_collapse(food_category, Product = c("Beef",
##   "Other"))`.
## Caused by warning:
## ! Unknown levels in `f`: Other
## # A tibble: 1,300 × 2
##    food_category cat_col
##    <chr>         <fct>  
##  1 Beef          Product
##  2 Beef          Product
##  3 Beef          Product
##  4 Beef          Product
##  5 Beef          Product
##  6 Beef          Product
##  7 Beef          Product
##  8 Beef          Product
##  9 Beef          Product
## 10 Beef          Product
## # ℹ 1,290 more rows
# Lump small levels into other levels 
food %>% count(food_category)
## # A tibble: 11 × 2
##    food_category                n
##    <chr>                    <int>
##  1 Beef                       130
##  2 Eggs                       130
##  3 Fish                       130
##  4 Lamb & Goat                130
##  5 Milk - inc. cheese         130
##  6 Nuts inc. Peanut Butter    130
##  7 Pork                       130
##  8 Poultry                    130
##  9 Rice                       130
## 10 Soybeans                   130
## 11 Wheat and Wheat Products   130
food %>% mutate(category_lump = fct_lump(food_category)) %>% distinct(category_lump)
## # A tibble: 11 × 1
##    category_lump           
##    <fct>                   
##  1 Beef                    
##  2 Eggs                    
##  3 Fish                    
##  4 Lamb & Goat             
##  5 Milk - inc. cheese      
##  6 Nuts inc. Peanut Butter 
##  7 Pork                    
##  8 Poultry                 
##  9 Rice                    
## 10 Soybeans                
## 11 Wheat and Wheat Products

Chapter 16

No need to do anything here.