1a.Load necessary libraries
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(tidyr)
1b.Load and Clear the Data
Inflation_Data <- suppressMessages(suppressWarnings(read_csv("Inflation-data (1).csv", show_col_types = FALSE)))
# Remove unnecessary "Unnamed" columns
cleaned_data <- Inflation_Data %>%
select(`Country`, starts_with("19"), starts_with("20")) %>% # Select country and year columns
pivot_longer(cols = starts_with("19"):starts_with("20"),
names_to = "Year",
values_to = "Inflation_Rate") %>%
mutate(Year = as.numeric(Year)) %>%
filter(Year >= 1970 & Year <= 2023, !is.na(Inflation_Rate))
# Display summary of cleaned data
summary(cleaned_data)
## Country 2001 2002 2003
## Length:5160 Min. :-43.400 Min. :-34.400 Min. :-8.600
## Class :character 1st Qu.: 1.900 1st Qu.: 1.600 1st Qu.: 1.600
## Mode :character Median : 3.900 Median : 2.900 Median : 3.300
## Mean : 8.788 Mean : 6.485 Mean : 6.487
## 3rd Qu.: 7.400 3rd Qu.: 8.000 3rd Qu.: 7.800
## Max. :357.300 Max. :108.900 Max. :98.200
##
## 2004 2005 2006 2007
## Min. : -4.800 Min. :-31.50 Min. :-1.900 Min. :-72.700
## 1st Qu.: 1.700 1st Qu.: 2.30 1st Qu.: 2.400 1st Qu.: 2.400
## Median : 3.700 Median : 4.10 Median : 4.300 Median : 4.800
## Mean : 6.291 Mean : 5.82 Mean : 6.164 Mean : 5.552
## 3rd Qu.: 7.800 3rd Qu.: 8.50 3rd Qu.: 8.200 3rd Qu.: 8.200
## Max. :113.600 Max. : 37.00 Max. :53.200 Max. : 30.900
##
## 2008 2009 2010 2011
## Min. : 1.40 Min. :-6.800 Min. :-3.900 Min. :-0.400
## 1st Qu.: 5.10 1st Qu.: 0.800 1st Qu.: 1.600 1st Qu.: 3.200
## Median : 8.40 Median : 3.300 Median : 3.500 Median : 4.800
## Mean : 10.84 Mean : 4.629 Mean : 4.499 Mean : 6.416
## 3rd Qu.: 12.40 3rd Qu.: 7.100 3rd Qu.: 6.200 3rd Qu.: 7.700
## Max. :157.00 Max. :46.100 Max. :28.200 Max. :53.200
##
## 2012 2013 2014 2015
## Min. :-3.000 Min. :-2.40 Min. :-1.400 Min. : -3.800
## 1st Qu.: 2.400 1st Qu.: 1.40 1st Qu.: 0.800 1st Qu.: 0.100
## Median : 3.800 Median : 2.70 Median : 2.600 Median : 1.800
## Mean : 5.745 Mean : 4.97 Mean : 4.342 Mean : 4.403
## 3rd Qu.: 6.600 3rd Qu.: 5.80 3rd Qu.: 5.700 3rd Qu.: 5.000
## Max. :59.500 Max. :90.00 Max. :62.100 Max. :121.400
##
## 2016 2017 2018 2019
## Min. : -3.100 Min. :-13.300 Min. : -14.4 Min. : -16.4
## 1st Qu.: 0.300 1st Qu.: 1.200 1st Qu.: 1.3 1st Qu.: 0.9
## Median : 1.800 Median : 2.300 Median : 2.4 Median : 2.3
## Mean : 5.773 Mean : 7.318 Mean : 399.5 Mean : 125.8
## 3rd Qu.: 5.600 3rd Qu.: 5.300 3rd Qu.: 4.5 3rd Qu.: 4.1
## Max. :254.400 Max. :438.100 Max. :65374.1 Max. :19906.0
## NA's :31 NA's :31 NA's :31
## 2020 2021 2022 2023
## Min. : -2.6 Min. : -3.00 Min. : 1.00 Min. : -1.70
## 1st Qu.: 0.5 1st Qu.: 1.90 1st Qu.: 5.40 1st Qu.: 3.70
## Median : 2.5 Median : 3.50 Median : 8.00 Median : 5.60
## Mean : 126.0 Mean : 18.74 Mean : 14.13 Mean : 14.32
## 3rd Qu.: 5.1 3rd Qu.: 5.90 3rd Qu.: 11.50 3rd Qu.: 9.40
## Max. :17087.7 Max. :1588.50 Max. :200.90 Max. :337.50
## NA's :59 NA's :59 NA's :59 NA's :95
## Year Inflation_Rate
## Min. :1970 Min. : -71.300
## 1st Qu.:1978 1st Qu.: 3.375
## Median :1986 Median : 8.100
## Mean :1986 Mean : 55.200
## 3rd Qu.:1994 3rd Qu.: 16.800
## Max. :2000 Max. :23773.100
##
2a.Insight 1: Average Inflation by Country (1970-2023)
# Calculate the country summary
country_summary <- cleaned_data %>%
group_by(Country) %>%
summarize(Avg_Inflation = mean(Inflation_Rate, na.rm = TRUE))
# Filter for Top 20 Highest Inflation Countries
top_20_highest <- country_summary %>%
arrange(desc(Avg_Inflation)) %>% # Sort by descending order
head(20) # Select the top 20
# Filter for Top 10 Lowest Inflation Countries
top_20_lowest <- country_summary %>%
arrange(Avg_Inflation) %>% # Sort by ascending order
head(20) # Select the lowest 20
# Display the top 20 highest inflation countries
print(top_20_highest)
## # A tibble: 20 × 2
## Country Avg_Inflation
## <chr> <dbl>
## 1 Georgia 2206.
## 2 Armenia 1153.
## 3 Congo, Dem. Rep. 1140.
## 4 Nicaragua 982.
## 5 Turkmenistan 873.
## 6 Kazakhstan 750.
## 7 Tajikistan 522.
## 8 Bolivia 453.
## 9 Azerbaijan 425.
## 10 Peru 418.
## 11 Brazil 388.
## 12 Uzbekistan 362.
## 13 Angola 352.
## 14 Argentina 307.
## 15 Moldova 278.
## 16 Croatia 242.
## 17 Macedonia, FYR 240.
## 18 Ukraine 196.
## 19 Belarus 188.
## 20 Kyrgyz Republic 151.
# Display the top 10 lowest inflation countries
print(top_20_lowest)
## # A tibble: 20 × 2
## Country Avg_Inflation
## <chr> <dbl>
## 1 Macao SAR, China -2.4
## 2 Zimbabwe 1.38
## 3 New Caledonia 1.5
## 4 Micronesia, Fed. Sts. 2.16
## 5 Puerto Rico 2.18
## 6 Bosnia and Herzegovina 2.4
## 7 Aruba 2.88
## 8 Oman 3.24
## 9 Panama 3.36
## 10 Germany 3.37
## 11 Singapore 3.45
## 12 Switzerland 3.47
## 13 Cayman Islands 3.56
## 14 Malta 3.64
## 15 Brunei Darussalam 3.85
## 16 Kiribati 4.00
## 17 Austria 4.05
## 18 Netherlands 4.06
## 19 Japan 4.12
## 20 Congo, Rep. 4.19
2b.Insight 2: Global Average Inflation (1970-2023):
# Group by Year and calculate the world average inflation rate for each year
world_inflation_by_year <- cleaned_data %>%
group_by(Year) %>%
summarize(World_Avg_Inflation = mean(Inflation_Rate, na.rm = TRUE))
# Display the global average inflation data from 1970 to 2023
print(world_inflation_by_year)
## # A tibble: 31 × 2
## Year World_Avg_Inflation
## <dbl> <dbl>
## 1 1970 6.12
## 2 1971 6.50
## 3 1972 8.11
## 4 1973 15.7
## 5 1974 23.1
## 6 1975 18.4
## 7 1976 16.0
## 8 1977 15.9
## 9 1978 12.9
## 10 1979 16.1
## # ℹ 21 more rows
ggplot(world_inflation_by_year, aes(x = Year, y = World_Avg_Inflation)) +
geom_line(color = "blue", size = 1) +
labs(title = "World Average Inflation Rate (1970-2023)",
x = "Year",
y = "Average Inflation Rate (%)") +
theme_minimal()
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

3.Visualizing Inflation Trends by Country compare to world ave
inflation (1970-2023)
# Merge the cleaned_data with world_inflation_by_year to include the World_Avg_Inflation
merged_data <- merge(cleaned_data, world_inflation_by_year, by = "Year")
# Loop through each country in the data
for (country in unique(merged_data$Country)) {
# Filter the data to only include rows for the current country
country_data <- filter(merged_data, Country == country)
# Create a plot with country's inflation rate and world average inflation rate
p <- ggplot(country_data, aes(x = Year)) +
# Plot the country's inflation rate
geom_line(aes(y = Inflation_Rate, color = "Country Inflation Rate"), size = 1) +
# Plot the world average inflation rate
geom_line(aes(y = World_Avg_Inflation, color = "World Average Inflation Rate"), size = 1, linetype = "dashed") +
# Add labels and title
labs(title = paste("Inflation Rate of", country, "vs World Average (1970-2023)"),
x = "Year",
y = "Inflation Rate (%)") +
# Customize colors for the lines
scale_color_manual(name = "",
values = c("Country Inflation Rate" = "blue",
"World Average Inflation Rate" = "red")) +
# Use a minimal theme
theme_minimal() +
# Set x-axis breaks every 5 years
scale_x_continuous(breaks = seq(1970, 2023, by = 5)) +
# Position the legend at the bottom
theme(legend.position = "bottom")
# Display the plot for the current country
print(p)
}



























































































































































































## `geom_line()`: Each group consists of only one observation.
## ℹ Do you need to adjust the group aesthetic?
## `geom_line()`: Each group consists of only one observation.
## ℹ Do you need to adjust the group aesthetic?
