library(readr)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(ggplot2)
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(gganimate)
library(transformr)
library(gifski)
library(av)
Indicators <- read_csv("Economic Indicators.csv")
## Rows: 304 Columns: 6
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (1): Country
## dbl (5): Year, GDP (in billion USD), Inflation Rate (%), Unemployment Rate (...
## 
## ℹ 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.
str(Indicators)
## spc_tbl_ [304 × 6] (S3: spec_tbl_df/tbl_df/tbl/data.frame)
##  $ Country              : chr [1:304] "USA" "USA" "USA" "USA" ...
##  $ Year                 : num [1:304] 2010 2011 2012 2013 2014 ...
##  $ GDP (in billion USD) : num [1:304] 15000 15500 16000 16500 17000 17500 18000 18500 19000 19500 ...
##  $ Inflation Rate (%)   : num [1:304] 1.64 3.16 2.07 1.5 1.62 0.12 1.26 2.13 2.44 1.81 ...
##  $ Unemployment Rate (%): num [1:304] 9.63 8.94 8.1 7.7 7.25 5.32 4.87 4.3 3.91 3.7 ...
##  $ Economic Growth (%)  : num [1:304] 2.55 1.53 2.28 1.84 2.53 3.08 2.69 2.81 2.9 2.33 ...
##  - attr(*, "spec")=
##   .. cols(
##   ..   Country = col_character(),
##   ..   Year = col_double(),
##   ..   `GDP (in billion USD)` = col_double(),
##   ..   `Inflation Rate (%)` = col_double(),
##   ..   `Unemployment Rate (%)` = col_double(),
##   ..   `Economic Growth (%)` = col_double()
##   .. )
##  - attr(*, "problems")=<externalptr>
head(Indicators)
## # A tibble: 6 × 6
##   Country  Year `GDP (in billion USD)` `Inflation Rate (%)`
##   <chr>   <dbl>                  <dbl>                <dbl>
## 1 USA      2010                  15000                 1.64
## 2 USA      2011                  15500                 3.16
## 3 USA      2012                  16000                 2.07
## 4 USA      2013                  16500                 1.5 
## 5 USA      2014                  17000                 1.62
## 6 USA      2015                  17500                 0.12
## # ℹ 2 more variables: `Unemployment Rate (%)` <dbl>,
## #   `Economic Growth (%)` <dbl>

Dataset Overview

The dataset contains information on macroeconomic indicators for multiple countries over several years. Here are the variables:

Statistic Visualisation

# Subset for emerging market countries
selected_countries <- c("Bangladesh", "India", "Indonesia", "Malaysia", 
                        "Pakistan", "Vietnam")

FilteredIndicators <- Indicators %>%
  filter(Country %in% selected_countries)

Plot 2: Inflation Rate (%) Over Time

ggplot(FilteredIndicators, aes(x = Year, y = `Inflation Rate (%)`, color = Country)) +
  geom_line(linewidth = 1) +
  labs(
    title = "Inflation Trends (2010–2025)",
    subtitle = "Emerging Markets",
    x = "Year",
    y = "Inflation Rate (%)",
    color = "Country"
    ) +
  theme_light()

countries <- unique(FilteredIndicators$Country)

# Initialize plotly object
fig <- plot_ly()

# Add traces (GDP bar & Inflation line) for each country
for (c in countries) {
  
  country_data <- FilteredIndicators %>% filter(Country == c)
  
  fig <- fig %>% 
    add_bars(data = country_data,
             x = ~Year,
             y = ~`GDP (in billion USD)`,
             name = "GDP",
             visible = ifelse(c == countries[1], TRUE, FALSE)) %>%
    
    add_lines(data = country_data,
              x = ~Year,
              y = ~`Inflation Rate (%)`,
              name = "Inflation",
              yaxis = "y2",
              visible = ifelse(c == countries[1], TRUE, FALSE))
 
}

# Create dropdown buttons
buttons <- lapply(1:length(countries), function(i) {
  list(method = "update",
       label = countries[i],
       args = list(
         list(visible = rep(FALSE, length(countries)*2)) %>%
           {.[[1]][(2*i-1):(2*i)] <- TRUE; .},
         list(title = paste("GDP and Inflation in", countries[i]))
       ))
})

# Layout with Dropdown
fig <- fig %>% layout(
  title = paste("GDP and Inflation in", countries[1]),
  xaxis = list(title = "Year"),
  yaxis = list(title = "GDP (in Billion USD)"),
  yaxis2 = list(overlaying = "y",
                side = "right",
                title = "Inflation Rate (%)"),
  updatemenus = list(list(
    active = 0,
    buttons = buttons
  ))
)
fig