data <- read_excel("../00_data/myData.xlsx")
food_levels <- c(
"Pork", "Poultry", "Beef", "Lamb & Goat", "Fish", "Eggs", "Milk - inc. cheese", "Wheat and Wheat Products", "Rice", "Soybeans", "Nuts inc. Peanut Butter"
)
Make two bar charts here - one before ordering another after
# Unordered
food_levels <- data %>%
group_by(food_category) %>%
summarise(avg_consumption = mean(consumption, na.rm = TRUE))
food_levels %>%
ggplot(aes(x = avg_consumption, y = food_category)) +
geom_point()
# Ordered
food_levels %>%
ggplot(aes(x = avg_consumption, y = fct_reorder(.f = food_category, .x = avg_consumption))) +
geom_point() +
# Labeling
labs(y = NULL, x = "Mean Consumption")
Show examples of three functions:
# fct_recode
data %>%
mutate(food_category_rev = fct_recode(food_category, "Milk - inc. cheese" = "Beef")) %>%
select(food_category, food_category_rev) %>%
filter(food_category == "Beef")
## # A tibble: 130 × 2
## food_category food_category_rev
## <chr> <fct>
## 1 Beef Milk - inc. cheese
## 2 Beef Milk - inc. cheese
## 3 Beef Milk - inc. cheese
## 4 Beef Milk - inc. cheese
## 5 Beef Milk - inc. cheese
## 6 Beef Milk - inc. cheese
## 7 Beef Milk - inc. cheese
## 8 Beef Milk - inc. cheese
## 9 Beef Milk - inc. cheese
## 10 Beef Milk - inc. cheese
## # ℹ 120 more rows
# fct_collapse
data %>%
mutate(food_category_col = fct_collapse(food_category, "Meat" = c("Pork", "Beef", "Poultry", "Lamb & Goat", "Fish"))) %>%
select(food_category, food_category_col) %>%
filter(food_category != "Eggs")
## # A tibble: 1,300 × 2
## food_category food_category_col
## <chr> <fct>
## 1 Pork Meat
## 2 Poultry Meat
## 3 Beef Meat
## 4 Lamb & Goat Meat
## 5 Fish Meat
## 6 Milk - inc. cheese Milk - inc. cheese
## 7 Wheat and Wheat Products Wheat and Wheat Products
## 8 Rice Rice
## 9 Soybeans Soybeans
## 10 Nuts inc. Peanut Butter Nuts inc. Peanut Butter
## # ℹ 1,290 more rows
#fct_lump
data %>% 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
data %>% mutate(food_category_lump = fct_lump(food_category)) %>% distinct(food_category_lump)
## # A tibble: 11 × 1
## food_category_lump
## <fct>
## 1 Pork
## 2 Poultry
## 3 Beef
## 4 Lamb & Goat
## 5 Fish
## 6 Eggs
## 7 Milk - inc. cheese
## 8 Wheat and Wheat Products
## 9 Rice
## 10 Soybeans
## 11 Nuts inc. Peanut Butter
No need to do anything here.