bar plot
misc
Author

Ana Luisa Bodevan

Published

September 16, 2025

Code
# # # # # # TIDY TUESDAY WEEK 37 - ALLRECIPES # # # # # #

######## 1. SETUP

# 1.1 Load libraries 

library(pacman)

pacman :: p_load(tidyverse, dplyr, scales, ggrepel, ggtext, showtext, ggforce)

# 1.2 Load data 

cuisines <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/main/data/2025/2025-09-16/cuisines.csv')

# 1.3 Set theme 

font_add_google("Ubuntu", "Ubuntu", regular.wt = 400, bold.wt = 700)
showtext_auto()
showtext_opts(dpi = 300)

theme_set(
  theme_minimal() +
    theme(
      plot.title = element_text(face = "bold", size = 18, family = "ubuntu", hjust = 0.5, margin = margin(b = 5)),
      plot.subtitle = element_text(size = 14, family = "ubuntu", hjust = 0.5, margin = margin(b = 15), color = "grey40"),
      plot.caption = element_text(size = 10, family = "ubuntu", hjust = 0.5, margin = margin(t = 15), color = "grey50"),
      axis.title = element_text(size = 12, family = "ubuntu"),
      axis.text = element_text(size = 11, family = "ubuntu"),
      panel.grid.minor = element_blank(),
      panel.grid.major.x = element_line(color = "grey90", linewidth  = 0.3),
      plot.background = element_rect(fill = "white", color = NA),
      panel.background = element_rect(fill = "white", color = NA)
    )
)

######## 2. DATA WRANGLING

# 2.1 What is the composition of nutrients by cuisine?

cuisines %>% 
  select(country, fat, carbs, protein) |>
  pivot_longer(-country, names_to = 'macronutrient', values_to = 'value') |>
  group_by(country, macronutrient) |>
  summarise(mean_value = mean(value, na.rm = TRUE)) |>
  group_by(country) |>
  mutate(proportion = mean_value / sum(mean_value)) |>
  ungroup() |>
  left_join(
    cuisines |>
      select(country, calories) |>
      group_by(country) |>
      summarise(calories = mean(calories, na.rm = TRUE)) |>
      ungroup(),
    by = 'country'
  ) |>
  mutate(country = fct_reorder(country, calories)) |>
  ggplot(aes(x = proportion * calories, y = country, fill = macronutrient)) +
  geom_col()

# 2.2 Get mean calories per cuisine

mean_calories <-
  cuisines %>% 
  select(country, calories) %>% 
  group_by(country) %>% 
  filter(n() > 50) %>% 
  summarise(calories = mean(calories, na.rm = TRUE)) %>% 
  ungroup()

top_cuisines <- mean_calories %>%
  slice_max(order_by = mean_calories, n = 20) %>%
  pull(country)

# 2.3 Prep data for visualization 

plot <- cuisines %>%
  filter(country %in% top_cuisines) %>%
  select(country, fat, carbs, protein, calories) %>%
  pivot_longer(cols = fat:protein, names_to = 'macronutrient', values_to = 'value') %>%
  group_by(country, macronutrient) %>%
  summarise(mean_value = mean(value, na.rm = TRUE), .groups = 'drop') %>%
  group_by(country) %>%
  mutate(proportion = mean_value / sum(mean_value)) %>%
  ungroup() %>%
  left_join(
    cuisines %>%
      filter(country %in% top_cuisines) %>%
      group_by(country) %>%
      summarise(mean_calories = mean(calories, na.rm = TRUE), .groups = 'drop'),
    by = 'country'
  ) %>%
  mutate(
    country = fct_reorder(country, mean_calories),
    scaled_value = proportion * mean_calories
  )

######## 3. PLOT

macronutrient_colors <- c(
  "carbs" = "#F8766D",   
  "fat" = "#7CAE00",     
  "protein" = "#00BFC4"  
)

ggplot(plot, aes(x = scaled_value, y = country, fill = macronutrient)) +
  geom_col(width = 0.7) +  # rounded bars
  
  scale_fill_manual(values = macronutrient_colors, name = "Macronutrient") +
  scale_x_continuous(labels = scales::comma) +
  
  labs(
    title = "Macronutrient Composition of World Cuisines",
    subtitle = "Average contribution of carbs, fat, and protein scaled by calories",
    x = "Calories",
    y = "",
    caption = "Data: AllRecipes #TidyTuesday | @anabodevan"
  ) +
  theme_minimal(base_family = "Ubuntu") +
  
  theme(
    legend.position = "top",
    legend.title = element_text(face = "bold"),
    plot.title.position = "plot",
    plot.title = element_text(face = "bold", size = 14),
    plot.subtitle = element_text(size = 10, color = "grey40"),
    plot.caption = element_text(size = 6, color = "grey40"),
    axis.text.y = element_text(size = 9),
    axis.text.x = element_text(size = 9),
    panel.grid.major.y = element_blank(),
    panel.grid.minor = element_blank(),
    plot.background = element_rect(fill = "#FFF8F0", color = NA)
  )