Click the Original, Code and Reconstruction tabs to read about the issues and how they were fixed.
Objective
The visualisation chosen had the following three main issues:
Reference
2020 Data: https://oec.world/en/visualize/tree_map/hs92/export/rus/all/52709/2020/ 2019 Data: https://oec.world/en/visualize/tree_map/hs92/export/rus/all/52709/2019/
# Install the packages (only needed if not already installed)
if (!requireNamespace("dplyr", quietly = TRUE)) {
install.packages("dplyr")
}
if (!requireNamespace("ggplot2", quietly = TRUE)) {
install.packages("ggplot2")
}
# Load the packages
library(dplyr)
library(ggplot2)
The following code was used to fix the issues identified in the original.
# Install the package (only needed if not already installed)
if (!requireNamespace("dplyr", quietly = TRUE)) {
install.packages("dplyr")
}
# Load the package
library(dplyr)
# Read the CSV files
data_2020 <- read.csv("2020.csv")
data_2019 <- read.csv("2019.csv")
# Print the names of the columns for each data frame
cat("Column names for 2020.csv:\n")
## Column names for 2020.csv:
print(colnames(data_2020))
## [1] "Continent" "Continent.ID" "Country" "Country.ID" "ISO.3"
## [6] "Trade.Value" "Year"
cat("\nColumn names for 2019.csv:\n")
##
## Column names for 2019.csv:
print(colnames(data_2019))
## [1] "Continent" "Continent.ID" "Country" "Country.ID" "ISO.3"
## [6] "Trade.Value" "Year"
# Combine the data frames
combined_data <- bind_rows(data_2020, data_2019)
# Convert trade values to millions
combined_data$Trade.Value <- combined_data$Trade.Value / 1000000
# Create the chart using ggplot2
chart <- ggplot(combined_data, aes(x = Country, y = Trade.Value, fill = as.factor(Year))) +
geom_bar(stat = "identity", position = "dodge") +
theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
labs(x = "Country",
y = "Trade Value (Millions)",
fill = "Year",
title = "Comparison of Countries and Trade Values in Millions for 2019 and 2020")
# Display the chart
print(chart)
top_25_countries <- combined_data %>%
group_by(Country) %>%
summarize(Total.Trade.Value = sum(Trade.Value)) %>%
top_n(25, Total.Trade.Value) %>%
select(Country)
top_25_data <- combined_data %>%
filter(Country %in% top_25_countries$Country)
remaining_data <- combined_data %>%
filter(!(Country %in% top_25_countries$Country))
chart_top_25 <- ggplot(top_25_data, aes(x = Country, y = Trade.Value, fill = as.factor(Year))) +
geom_bar(stat = "identity", position = "dodge") +
theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
labs(x = "Country",
y = "Trade Value (Millions)",
fill = "Year",
title = "Top 25 Countries: Trade Values in Millions for 2019 and 2020")
print(chart_top_25)
ggsave("compare_25.png", width = 8, height = 6, dpi = 300)
chart_remaining <- ggplot(remaining_data, aes(x = Country, y = Trade.Value, fill = as.factor(Year))) +
geom_bar(stat = "identity", position = "dodge") +
theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
labs(x = "Country",
y = "Trade Value (Millions)",
fill = "Year",
title = "Remaining Countries: Trade Values in Millions for 2019 and 2020")
# Display the chart for the remaining countries
print(chart_remaining)
ggsave("compare_rem.png", width = 8, height = 6, dpi = 300)
# Install the packages (if not already installed)
if (!requireNamespace("dplyr", quietly = TRUE)) {
install.packages("dplyr")
}
if (!requireNamespace("ggplot2", quietly = TRUE)) {
install.packages("ggplot2")
}
if (!requireNamespace("tidyverse", quietly = TRUE)) {
install.packages("tidyverse")
}
# Load the packages
library(dplyr)
library(ggplot2)
library(tidyverse)
# Calculate total trade value for each country in 2019 and 2020
trade_values_by_year <- combined_data %>%
group_by(Country, Year) %>%
summarise(Total.Trade.Value = sum(Trade.Value)) %>%
spread(Year, Total.Trade.Value)
# Calculate the difference in trade values and percentage
trade_values_diff <- trade_values_by_year %>%
mutate(Difference = `2020` - `2019`,
Percentage.Difference = (Difference / `2019`) * 100)
# Get the top 25 countries based on trade value in 2019 and 2020
top_25_countries <- trade_values_diff %>%
top_n(25, `2020`) %>%
pull(Country)
# Filter the differences for the top 25 countries
top_25_diff <- trade_values_diff %>% filter(Country %in% top_25_countries)
# Create a bar chart for the difference in trade values
chart_value_diff <- ggplot(top_25_diff, aes(x = Country, y = Difference, fill = Country)) +
geom_bar(stat = "identity") +
theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
labs(x = "Country",
y = "Difference in Trade Value",
title = "Difference in Trade Values from 2019 to 2020 for Top 25 Countries")
# Display the chart for the difference in trade values
print(chart_value_diff)
# Create a bar chart for the percentage difference in trade values
chart_percentage_diff <- ggplot(top_25_diff, aes(x = Country, y = Percentage.Difference, fill = Country)) +
geom_bar(stat = "identity") +
theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
labs(x = "Country",
y = "Percentage Difference in Trade Value",
title = "Percentage Difference in Trade Values from 2019 to 2020 for Top 25 Countries")
# Display the chart for the percentage difference in trade values
print(chart_percentage_diff)
top_25_countries <- combined_data %>%
group_by(Country) %>%
summarize(Total.Trade.Value = sum(Trade.Value)) %>%
top_n(25, Total.Trade.Value) %>%
select(Country)
top_25_data <- combined_data %>%
filter(Country %in% top_25_countries$Country)
remaining_data <- combined_data %>%
filter(!(Country %in% top_25_countries$Country))
chart_top_25 <- ggplot(top_25_data, aes(x = Country, y = Trade.Value, fill = as.factor(Year))) +
geom_bar(stat = "identity", position = "dodge") +
theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
labs(x = "Country",
y = "Trade Value (Millions)",
fill = "Year",
title = "Top 25 Countries: Trade Values in Millions for 2019 and 2020")
print(chart_top_25)
ggsave("chart_top_25.png", width = 8, height = 6, dpi = 300)
chart_remaining <- ggplot(remaining_data, aes(x = Country, y = Trade.Value, fill = as.factor(Year))) +
geom_bar(stat = "identity", position = "dodge") +
theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
labs(x = "Country",
y = "Trade Value (Millions)",
fill = "Year",
title = "Remaining Countries: Trade Values in Millions for 2019 and 2020")
# Display the chart for the remaining countries
print(chart_remaining)
ggsave("chart_remaining.png", width = 8, height = 6, dpi = 300)
library(ggplot2)
library(dplyr)
# Create a sample data frame
set.seed(123)
country <- sample(LETTERS[1:10], 50, replace = TRUE)
year <- rep(c(2019, 2020), 25)
value <- rnorm(50, 100, 50)
combined_data <- data.frame(Country = country, Year = year, Trade.Value = value)
# Get the top 25 countries based on trade value
top_25_countries <- combined_data %>%
group_by(Country) %>%
summarise(Total.Trade.Value = sum(Trade.Value)) %>%
top_n(25, Total.Trade.Value) %>%
select(Country)
# Create separate data frames for the top 25 countries and the remaining countries
top_25_data <- combined_data %>% filter(Country %in% top_25_countries$Country)
remaining_data <- combined_data %>% filter(!(Country %in% top_25_countries$Country))
# Create the chart for the top 25 countries
chart_top_25 <- ggplot(top_25_data, aes(x = Country, y = Trade.Value, fill = as.factor(Year))) +
geom_bar(stat = "identity", position = "dodge") +
theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
labs(x = "Country",
y = "Trade Value (Millions)",
fill = "Year",
title = "Top 25 Countries: Trade Values in Millions for 2019 and 2020")
# Display the chart for the top 25 countries
print(chart_top_25)
library(ggplot2)
library(dplyr)
# Create a sample data frame
set.seed(123)
country <- sample(LETTERS[1:10], 20, replace = TRUE)
year <- rep(c(2019, 2020), 10)
value <- rnorm(20, 100, 50)
combined_data <- data.frame(Country = country, Year = year, Trade.Value = value)
# Comparison chart for Trade.Value by country for 2019 and 2020
comparison_chart <- combined_data %>%
filter(Year %in% c(2019, 2020)) %>%
ggplot(aes(x = Country, y = Trade.Value, fill = as.factor(Year))) +
geom_bar(stat = "identity", position = "dodge") +
labs(x = "Country",
y = "Trade Value",
fill = "Year",
title = "Trade Value Comparison by Country for 2019 and 2020")
print(comparison_chart)
library(ggplot2)
library(dplyr)
combined_data <- bind_rows(data_2020, data_2019)
# Get the top 25 countries based on trade value
top_25_countries <- combined_data %>%
group_by(Country) %>%
summarise(Total.Trade.Value = sum(Trade.Value)) %>%
top_n(25, Total.Trade.Value) %>%
select(Country)
# Comparison chart for Trade.Value by country for 2019 and 2020
comparison_chart <- combined_data %>%
filter(Country %in% top_25_countries$Country, Year %in% c(2019, 2020)) %>%
ggplot(aes(x = Country, y = Trade.Value, fill = as.factor(Year))) +
geom_bar(stat = "identity", position = "dodge") +
labs(x = "Country",
y = "Trade Value",
fill = "Year",
title = "Trade Value Comparison by Country for 2019 and 2020 (Top 25 Countries)")
print(comparison_chart)
library(ggplot2)
library(dplyr)
# Read in the data from a CSV file
combined_data <- bind_rows(data_2020, data_2019)
# Get the top 25 countries based on trade value
top_25_countries <- combined_data %>%
group_by(Country) %>%
summarise(Total.Trade.Value = sum(Trade.Value)) %>%
top_n(25, Total.Trade.Value) %>%
select(Country)
# Filter the data to include only the top 25 countries
top_25_data <- combined_data %>%
filter(Country %in% top_25_countries$Country)
# Comparison chart for Trade.Value by country for 2019 and 2020 (top 25 countries only)
comparison_chart <- top_25_data %>%
filter(Year %in% c(2019, 2020)) %>%
ggplot(aes(x = Country, y = Trade.Value/1000000, fill = as.factor(Year))) +
geom_bar(stat = "identity", position = "dodge") +
labs(x = "Country",
y = "Trade Value (Millions)",
fill = "Year",
title = "Trade Value Comparison by Country for 2019 and 2020 (Top 25 Countries)") +
scale_y_continuous(labels = scales::comma)
print(comparison_chart)
# Save the plot as a PNG file
ggsave("comparison_chart.png", plot = comparison_chart, width = 10, height = 6, dpi = 300)
df1 <- read.csv("2019.csv")
df2 <- read.csv("2020.csv")
combined_df <- rbind(df1, df2)
write.csv(combined_df, "combined.csv", row.names = FALSE)
library(tidyverse) # for data wrangling and visualization
library(lubridate) # for working with dates
data <- read.csv("combined.csv")
data$Year <- ymd(paste0(data$Year, "-01-01"))
total_trade <- data %>%
group_by(Country, Year) %>%
summarize(Total_Trade = sum(Trade.Value, na.rm = TRUE))
ggplot(total_trade, aes(x = Year, y = Total_Trade, color = Country)) +
geom_line() +
labs(title = "Total Trade by Country over Time",
x = "Year",
y = "Total Trade",
color = "Country") +
theme_bw()
library(tidyverse)
library(lubridate)
data <- read.csv("combined_million.csv")
data$Year <- ymd(paste0(data$Year, "-01-01"))
total_trade <- data %>%
group_by(Country, Year) %>%
summarize(Total_Trade = sum(Trade.Value, na.rm = TRUE))
top_countries <- total_trade %>%
group_by(Country) %>%
summarize(Total_Trade = sum(Total_Trade)) %>%
top_n(10, Total_Trade) %>%
pull(Country)
total_trade <- total_trade %>%
filter(Country %in% top_countries)
ggplot(total_trade, aes(x = Year, y = Total_Trade, color = Country)) +
geom_line() +
labs(title = "Total Trade by Country over Time",
x = "Year",
y = "Total Trade",
color = "Country") +
theme_bw()
# read the combined.csv file
Million_2019 <- read.csv("2019.csv")
# convert Trade.Value to million
Million_2019$Trade.Value <- Million_2019$Trade.Value / 1000000
# write the updated data to a new csv file
write.csv(Million_2019, file = "combined_million.csv", row.names = FALSE)
# read the combined.csv file
Million_2020 <- read.csv("2020.csv")
# convert Trade.Value to million
Million_2020$Trade.Value <- Million_2020$Trade.Value / 1000000
# write the updated data to a new csv file
write.csv(Million_2020, file = "combined_million.csv", row.names = FALSE)
library(ggplot2)
library(dplyr)
# read the 2019.csv file
Million_2019 <- read.csv("2019.csv")
# convert Trade.Value to million
Million_2019$Trade.Value <- Million_2019$Trade.Value / 1000000
# write the updated data to a new csv file
write.csv(Million_2019, file = "combined_million.csv", row.names = FALSE)
# read the 2020.csv file
Million_2020 <- read.csv("2020.csv")
# convert Trade.Value to million
Million_2020$Trade.Value <- Million_2020$Trade.Value / 1000000
# write the updated data to a new csv file
write.csv(Million_2020, file = "combined_million.csv", row.names = FALSE)
# plot for 2019
total_trade <- Million_2019 %>%
group_by(Country) %>%
summarize(Total_Trade = sum(Trade.Value, na.rm = TRUE))
top_countries <- total_trade %>%
top_n(10, Total_Trade) %>%
pull(Country)
total_trade <- total_trade %>%
filter(Country %in% top_countries)
plot_2019 <- ggplot(total_trade, aes(x = Country, y = Total_Trade, fill = Country)) +
geom_col() +
coord_flip() +
labs(title = "Total Trade by Country (2019)",
x = "Country",
y = "Total Trade",
fill = "Country") +
theme_bw()
print(plot_2019)
ggsave("total_trade_2019.png", width = 8, height = 6, dpi = 300)
# plot for 2020
total_trade <- Million_2020 %>%
group_by(Country) %>%
summarize(Total_Trade = sum(Trade.Value, na.rm = TRUE))
top_countries <- total_trade %>%
top_n(10, Total_Trade) %>%
pull(Country)
total_trade <- total_trade %>%
filter(Country %in% top_countries)
plot_2020 <- ggplot(total_trade, aes(x = Country, y = Total_Trade, fill = Country)) +
geom_col() +
coord_flip() +
labs(title = "Total Trade by Country (2020)",
x = "Country",
y = "Total Trade",
fill = "Country") +
theme_bw()
print(plot_2020)
ggsave("total_trade_2020.png", width = 8, height = 6, dpi = 300)
total_trade <- data %>%
group_by(Country) %>%
summarize(Total_Trade = sum(Trade.Value, na.rm = TRUE))
top_countries <- total_trade %>%
top_n(10, Total_Trade) %>%
pull(Country)
total_trade <- total_trade %>%
filter(Country %in% top_countries)
ggplot(total_trade, aes(x = Country, y = Total_Trade, fill = Country)) +
geom_col() +
coord_flip() +
labs(title = "Total Trade by Country (2019-2020)",
x = "Country",
y = "Total Trade",
fill = "Country") +
theme_bw()
total_trade <- data %>%
group_by(Country) %>%
summarize(Total_Trade_Million = sum(Trade.Value, na.rm = TRUE) / 1e6)
top_countries <- total_trade %>%
top_n(20, Total_Trade_Million) %>%
pull(Country)
total_trade <- total_trade %>%
filter(Country %in% top_countries)
ggplot(total_trade, aes(x = Country, y = Total_Trade_Million, fill = Country)) +
geom_col() +
coord_flip() +
scale_y_continuous(labels = scales::comma_format(scale = 1e-6)) +
labs(title = "Total Trade by Country (2019-2020)",
x = "Country",
y = "Total Trade (Millions USD)",
fill = "Country") +
theme_bw()
library(readr)
library(dplyr)
data <- read_csv("combined.csv")
glimpse(data)
## Rows: 96
## Columns: 7
## $ Continent <chr> "Asia", "Asia", "Asia", "Asia", "Asia", "Asia", "Asia", "…
## $ Continent.ID <chr> "as", "as", "as", "as", "as", "as", "as", "as", "as", "as…
## $ Country <chr> "United Arab Emirates", "Azerbaijan", "China", "India", "…
## $ Country.ID <chr> "asare", "asaze", "aschn", "asind", "asjpn", "askaz", "as…
## $ ISO.3 <chr> "are", "aze", "chn", "ind", "jpn", "kaz", "kgz", "kor", "…
## $ Trade.Value <dbl> 23998390, 127819744, 33696172481, 1110518634, 3258434376,…
## $ Year <dbl> 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 201…
# read the combined.csv file
combined_data <- read.csv("combined.csv")
# convert Trade.Value to million
combined_data$Trade.Value <- combined_data$Trade.Value / 1000000
# write the updated data to a new csv file
write.csv(combined_data, file = "combined_million.csv", row.names = FALSE)
# load required libraries
library(ggplot2)
library(dplyr)
library(tidyr)
# read the combined_million.csv file
combined_data_million <- read.csv("combined_million.csv")
# filter data for years 2019 and 2020
combined_data_million_filtered <- combined_data_million %>%
filter(Year %in% c(2019, 2020))
# group data by Country and Year and calculate total trade value
grouped_data <- combined_data_million_filtered %>%
group_by(Country, Year) %>%
summarise(Total_Trade_Value = sum(Trade.Value))
# get top 20 countries based on total trade value for each year
top20_2019 <- grouped_data %>%
filter(Year == 2019) %>%
arrange(desc(Total_Trade_Value)) %>%
top_n(20)
top20_2020 <- grouped_data %>%
filter(Year == 2020) %>%
arrange(desc(Total_Trade_Value)) %>%
top_n(20)
# create 5 different charts to compare top 20 countries based on Trade.Value for both 2019 and 2020
# chart 1: Bar chart to compare total trade value for top 20 countries in 2019
ggplot(top20_2019, aes(x = reorder(Country, Total_Trade_Value), y = Total_Trade_Value)) +
geom_bar(stat = "identity") +
ggtitle("Top 20 Countries based on Trade Value in 2019") +
xlab("Country") +
ylab("Total Trade Value (Million USD)") +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1))
# chart 2: Bar chart to compare total trade value for top 20 countries in 2020
ggplot(top20_2020, aes(x = reorder(Country, Total_Trade_Value), y = Total_Trade_Value)) +
geom_bar(stat = "identity") +
ggtitle("Top 20 Countries based on Trade Value in 2020") +
xlab("Country") +
ylab("Total Trade Value (Million USD)") +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1))
# chart 3: Line chart to compare total trade value for a specific country (India) over 2019 and 2020
ggplot(top20_2019, aes(x = Year, y = Total_Trade_Value, color = Country)) +
geom_line(data = filter(grouped_data, Country == "India" & Year %in% c(2019, 2020))) +
ggtitle("Total Trade Value for India over 2019 and 2020") +
xlab("Year") +
ylab("Total Trade Value (Million USD)")
# chart 4: Stacked bar chart to compare total trade value by country and year
ggplot(rbind(top20_2019, top20_2020), aes(x = Year, y = Total_Trade_Value, fill = Country)) +
geom_bar(stat = "identity") +
ggtitle("Total Trade Value by Country and Year") +
xlab("Year") +
ylab("Total Trade Value (Million USD)")
ggplot(rbind(top20_2019, top20_2020), aes(x = Country, y = Total_Trade_Value, fill = as.factor(Year))) +
geom_bar(stat = "identity", position = "dodge") +
ggtitle("Total Trade Value by Country and Year") +
xlab("Country") +
ylab("Total Trade Value (Million USD)") +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1))
# load required libraries
library(ggplot2)
library(dplyr)
library(tidyr)
# read the combined_million.csv file
combined_data_million <- read.csv("combined_million.csv")
# filter data for years 2019 and 2020
combined_data_million_filtered <- combined_data_million %>%
filter(Year %in% c(2019, 2020))
# group data by Country and Year and calculate total trade value
grouped_data <- combined_data_million_filtered %>%
group_by(Country, Year) %>%
summarise(Total_Trade_Value = sum(Trade.Value))
# get top 20 countries based on total trade value for each year
top20_2019 <- grouped_data %>%
filter(Year == 2019) %>%
arrange(desc(Total_Trade_Value)) %>%
top_n(20)
top20_2020 <- grouped_data %>%
filter(Year == 2020) %>%
arrange(desc(Total_Trade_Value)) %>%
top_n(20)
# create 5 different charts to compare top 20 countries based on Trade.Value for both 2019 and 2020
# chart 1: Bar chart to compare total trade value for top 20 countries in 2019
ggplot(top20_2019, aes(x = reorder(Country, Total_Trade_Value), y = Total_Trade_Value)) +
geom_bar(stat = "identity") +
ggtitle("Top 20 Countries based on Trade Value in 2019") +
xlab("Country") +
ylab("Total Trade Value (Million USD)") +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1))
# chart 2: Bar chart to compare total trade value for top 20 countries in 2020
ggplot(top20_2020, aes(x = reorder(Country, Total_Trade_Value), y = Total_Trade_Value)) +
geom_bar(stat = "identity") +
ggtitle("Top 20 Countries based on Trade Value in 2020") +
xlab("Country") +
ylab("Total Trade Value (Million USD)") +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1))
# chart 3: Line chart to compare total trade value for a specific country (India) over 2019 and 2020
ggplot(grouped_data, aes(x = Year, y = Total_Trade_Value, color = Country)) +
geom_line(data = filter(grouped_data, Country == "India" & Year %in% c(2019, 2020))) +
ggtitle("Total Trade Value for India over 2019 and 2020") +
xlab("Year") +
ylab("Total Trade Value (Million USD)")
# load required libraries
library(ggplot2)
library(dplyr)
library(tidyr)
# calculate total trade value by continent for 2019
total_trade_2019 <- Million_2019 %>%
group_by(Continent) %>%
summarize(Total_Trade = sum(Trade.Value, na.rm = TRUE))
# calculate total trade value by continent for 2020
total_trade_2020 <- Million_2020 %>%
group_by(Continent) %>%
summarize(Total_Trade = sum(Trade.Value, na.rm = TRUE))
# merge the total trade data for 2019 and 2020
total_trade_combined <- full_join(total_trade_2019, total_trade_2020, by = "Continent") %>%
rename("Total_Trade_2019" = Total_Trade.x, "Total_Trade_2020" = Total_Trade.y)
# convert the data from wide to long format
total_trade_combined_long <- total_trade_combined %>%
pivot_longer(cols = c("Total_Trade_2019", "Total_Trade_2020"), names_to = "Year", values_to = "Total_Trade")
# create stacked bar chart
ggplot(total_trade_combined_long, aes(x = Continent, y = Total_Trade, fill = Year)) +
geom_bar(stat = "identity") +
ggtitle("Total Trade by Continent (2019 vs 2020)") +
xlab("Continent") +
ylab("Total Trade (Million USD)") +
scale_fill_manual(values = c("#FFA600", "#5B8F00"), name = "Year", labels = c("2019", "2020")) +
theme_bw()
# load required libraries
library(ggplot2)
library(dplyr)
library(tidyr)
# calculate total trade value by continent for 2019
total_trade_2019 <- Million_2019 %>%
group_by(Continent) %>%
summarize(Total_Trade = sum(Trade.Value, na.rm = TRUE))
# calculate total trade value by continent for 2020
total_trade_2020 <- Million_2020 %>%
group_by(Continent) %>%
summarize(Total_Trade = sum(Trade.Value, na.rm = TRUE))
# merge the total trade data for 2019 and 2020
total_trade_combined <- full_join(total_trade_2019, total_trade_2020, by = "Continent") %>%
rename("Total_Trade_2019" = Total_Trade.x, "Total_Trade_2020" = Total_Trade.y)
# filter the total trade data to show only Europe and Asia
total_trade_combined <- total_trade_combined %>%
filter(Continent %in% c("Europe", "Asia"))
# convert the data from wide to long format
total_trade_combined_long <- total_trade_combined %>%
pivot_longer(cols = c("Total_Trade_2019", "Total_Trade_2020"), names_to = "Year", values_to = "Total_Trade")
# create stacked bar chart
ggplot(total_trade_combined_long, aes(x = Continent, y = Total_Trade, fill = Year)) +
geom_bar(stat = "identity") +
ggtitle("Total Trade by Continent (2019 vs 2020)") +
xlab("Continent") +
ylab("Total Trade (Million USD)") +
scale_fill_manual(values = c("#FFA600", "#5B8F00"), name = "Year", labels = c("2019", "2020")) +
theme_bw()
Data Reference
The following plot fixes the main issues in the original.