Food Inflation: Comparing with Interactive Chart of {ggiraph}

[This article was first published on DataGeeek, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

According to authorities, up to 205 million are facing food insecurity in about 45 countries. Besides global warming that causes poor rainfall performance, another strong reason to worry about the issue is the Russian invasion of Ukraine which causes trade-related restrictions.

I’ve just wondered how this reflects on food inflation. To do that, we will examine the food CPI rates of some G20 countries. First, we build our dataset.

We will create a dataset with annual growth rates (%) of food prices of some selected G20 countries, and we will merge them with a dataset that contains some extra information about the related countries like income status, and region.

library(tidyverse)
library(tsibble)
library(WDI)
library(countrycode)
library(ggiraph)
library(scales)
library(sysfonts)
library(showtext)
library(ggtext)

df <- read_csv("https://raw.githubusercontent.com/mesdi/blog/main/food.csv")

df_tidy <-
  df %>%
  janitor::clean_names() %>%
  mutate(location = countrycode(location, "genc3c", "country.name"),
         location = case_when(
           location == "Turkey" ~ "Turkiye",
           TRUE ~ location
           ),
         time = yearmonth(time)
         ) %>%
  na.omit()



#Extracting income level and region from the countries in World Bank database
df_countries <- 
  WDI(extra = TRUE) %>% 
  as_tibble() %>% 
  mutate(country = case_when(
    country == "Korea, Rep." ~ "South Korea",
    country == "Russian Federation" ~ "Russia",
    TRUE ~ country))
  

df_merged <- 
  df_tidy %>% 
  left_join(df_countries %>% 
              select(country, region, income) %>% 
              unique(), 
            by = c("location" = "country")
            )


  df_merged
# A tibble: 188 x 5
   location     time value region        income     
   <chr>       <mth> <dbl> <chr>         <chr>      
 1 Canada   2022 Jan  6.46 North America High income
 2 Canada   2022 Feb  7.40 North America High income
 3 Canada   2022 Mar  8.66 North America High income
 4 Canada   2022 Apr  9.74 North America High income
 5 Canada   2022 May  9.70 North America High income
 6 Canada   2022 Jun  9.43 North America High income
 7 Canada   2022 Jul  9.91 North America High income
 8 Canada   2022 Aug 10.8  North America High income
 9 Canada   2022 Sep 11.4  North America High income
10 Canada   2022 Oct 11.0  North America High income
# ... with 178 more rows
# i Use `print(n = ...)` to see more rows

Now, we will compare the latest rates in 2023 for each country and rank them. Some countries will be removed from the graph because they have no data in the related year.

We will make an interactive plot with ggiraph package. When we hover over the bar, we can see the rates, the exact date, and the region of the related countries. We will also colorize the bars based on income level for each country.

#adding google font
font_add_google("Roboto Mono", "Mono")
showtext_auto()

 
df_plot <- 
  df_merged %>% 
  filter(time >=  yearmonth("2023 Jan")) %>%
  slice_max(time, by = location) %>% 
  mutate(location = forcats::fct_reorder(location, value)) %>% 
  ggplot(aes(location, 
             value, 
             fill = income,
             tooltip = glue::glue("{number(round(value, 2) , suffix = '%')}\n{time}\n{region}"),
             data_id = location)) +
    geom_bar_interactive(stat = "identity") + 
    scale_fill_manual(values = c("#E69F00", "#009E73")) +
    scale_y_continuous(labels = label_number(suffix = "%")) +
    labs(x = "", 
         y = "",
         title = "Comparing food inflation of the G20 countries ",
         subtitle = "The<span style = 'color:#009E73;'> <b>upper-middle</b></span> and <span style = 'color:#E69F00;'><b>high</b></span> income countries"
         ) +
    coord_flip() +
    theme_minimal(
      base_size = 14, 
      base_family = "Mono") +
    theme(
      legend.position = "none",
      plot.background = element_rect(fill = "#C3C8F7", color = NA),
      panel.grid.major.y = element_blank(),
      plot.title = element_text(hjust = 0.5, face = "bold"),
      plot.subtitle = element_markdown(hjust = 0.5, size = 13)
    )
  
  
    
girafe(
  ggobj = df_plot,
  options = list(
    opts_hover(css = ''),
    opts_hover_inv(css = "opacity:0.1;"),
    opts_sizing(rescale = FALSE),
    opts_tooltip(
      offx = 25, 
      offy = 0, 
      use_fill = TRUE,
      css = 'font-size:12pt;padding:5px;font-family:Roboto Mono;'
  )
 ),
 width_svg = 8.5,
 height_svg = 7.2
)

As is seen in the above chart, Turkiye and Argentina are separated from the rest. European countries are much higher values than East Asian countries. It is quite interesting that high-income countries like Germany, the UK, and France have higher values than upper-middle-income countries like South Africa and Mexico.

To leave a comment for the author, please follow the link and comment on their blog: DataGeeek.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)