# Packages
library(tidyverse)
library(mosaic)
How have the GDP rankings of African countries changed over time? And what has been the rate of change in Liberia’s GDP over time?
This dataset features Gross Domestic Product (GDP) data from 1960 to 2020 for various countries and regions, highlighting economic trends and growth. As a key economic indicator, GDP measures the total value of goods and services produced, reflecting the economic health of a nation or region. Available on Kaggle, this dataset supports data science projects and encourages community engagement through sharing insights and methodologies.
gdp_1960_2020 <- read_csv("gdp_1960_2020.csv")
head(gdp_1960_2020)
| year | rank | country | state | gdp | gdp_percent |
|---|---|---|---|---|---|
| 1960 | 1 | the United States | America | 543300000000 | 0.4684827 |
| 1960 | 2 | United Kingdom | Europe | 73233967692 | 0.0631490 |
| 1960 | 3 | France | Europe | 62225478000 | 0.0536565 |
| 1960 | 4 | China | Asia | 59716467625 | 0.0514930 |
| 1960 | 5 | Japan | Asia | 44307342950 | 0.0382058 |
| 1960 | 6 | Canada | America | 40461721692 | 0.0348898 |
names(gdp_1960_2020)
## [1] "year" "rank" "country" "state" "gdp"
## [6] "gdp_percent"
The variables I used in my data visualization are:
Year, country, gdp, state, & gdp_percent
# Filter data for African countries only
african_gdp_data <- filter(gdp_1960_2020, state == "Africa")
head(african_gdp_data)
| year | rank | country | state | gdp | gdp_percent |
|---|---|---|---|---|---|
| 1960 | 19 | South Africa | Africa | 7575396973 | 0.0065322 |
| 1960 | 29 | Nigeria | Africa | 4196092258 | 0.0036183 |
| 1960 | 34 | Congo (gold) | Africa | 3359404117 | 0.0028968 |
| 1960 | 37 | Algeria | Africa | 2723593384 | 0.0023485 |
| 1960 | 40 | Morocco | Africa | 2037150716 | 0.0017566 |
| 1960 | 47 | Sudan | Africa | 1307333333 | 0.0011273 |
# Calculate summary statistics for GDP
summary_stats <- favstats(~ gdp, data = african_gdp_data)
summary_stats
| min | Q1 | median | Q3 | max | mean | sd | n | missing | |
|---|---|---|---|---|---|---|---|---|---|
| 9122751 | 849963326 | 2934846651 | 10706395832 | 546676374567 | 16458827546 | 47668881356 | 2854 | 0 |
This visualization traces the GDP growth of each African country from 1960 to 2020. It allows viewers to observe trends in economic development, identify periods of rapid growth or decline, and compare the trajectories of different countries. By examining the entire continent, we can see which countries have experienced significant economic advancements and start to think about how external factors like global oil prices or regional conflicts have impacted economies.
library(ggplot2)
library(plotly)
##
## Attaching package: 'plotly'
## The following object is masked from 'package:mosaic':
##
## do
## 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
# Convert factors to ensure grouping works correctly
african_gdp_data$country <- as.factor(african_gdp_data$country)
p <- ggplot(african_gdp_data, aes(x = year, y = gdp, color = country, group = country,
text = paste("Country:", country, "<br>GDP:", gdp))) +
geom_line() +
theme_minimal() +
labs(title = "GDP Growth Over Time for All African Countries",
x = "Year",
y = "GDP")
# Convert to a plotly object
ggplotly(p, tooltip = "text")
####Top and Bottom Performers in GDP Growth
Provide a summary of what this visualization shows and how it helps to answer your research question.
The visualization showcases the top and bottom 5 GDP performers among African countries in the year 2020. I chose 2022 because it is the final year data are available to date. Each bar in the plot represents a country, with the height of the bar indicating its GDP value. The color gradient applied to the bars provides visual emphasis, with darker shades indicating higher GDP values. This visualization helps answer the research question by illustrating the economic disparities among African countries in terms of GDP performance. By identifying both the top and bottom performers, viewers can gain insights into the economic dynamics within the continent. Understanding which countries have the highest and lowest GDP values in 2020 contributes to a broader understanding of how economic resources are distributed across African nations and provides context for analyzing trends in GDP rankings over time.
library(dplyr)
library(scales)
##
## Attaching package: 'scales'
## The following object is masked from 'package:mosaic':
##
## rescale
## The following object is masked from 'package:purrr':
##
## discard
## The following object is masked from 'package:readr':
##
## col_factor
# Define the color gradient function
get_gradient_color <- function(rank, total) {
grDevices::colorRampPalette(c("blue", "lightblue"))(total)[rank]
}
# Filter for the top 5 GDP performers in 2020
top_2020 <- african_gdp_data %>%
filter(year == 2020) %>%
arrange(desc(gdp)) %>%
slice(1:5)
# Add a color gradient based on rank
top_2020$color <- get_gradient_color(1:nrow(top_2020), nrow(top_2020))
# Filter for the bottom 5 GDP performers in 2020
bottom_2020 <- african_gdp_data %>%
filter(year == 2020) %>%
arrange(desc(gdp)) %>%
slice((n()-4):n())
# Add a color gradient based on rank
bottom_2020$color <- get_gradient_color(1:nrow(bottom_2020), nrow(bottom_2020))
# Plot for top 5 performers
top_plot <- ggplot(top_2020, aes(x = reorder(country, gdp), y = gdp, fill = color)) +
geom_col(show.legend = FALSE) +
geom_text(aes(label = scales::comma(gdp)), hjust = 1.1, size = 3.5) +
coord_flip() +
theme_minimal() +
labs(title = "Top 5 GDP Performers in 2020",
x = "",
y = "GDP")
# Plot for bottom 5 performers
bottom_plot <- ggplot(bottom_2020, aes(x = reorder(country, gdp), y = gdp, fill = color)) +
geom_col(show.legend = FALSE) +
geom_text(aes(label = scales::comma(gdp)), hjust = 1.1, size = 3.5) +
coord_flip() +
theme_minimal() +
labs(title = "Bottom 5 GDP Performers in 2020",
x = "",
y = "GDP")
# Print the plots
print(top_plot)
print(bottom_plot)
The code generates two visualizations: one showing the top 5 fastest-growing economies over time and the other displaying the top 5 slowest-growing economies. Each line represents a country’s GDP trend, with color indicating the country. Users can hover over lines for detailed GDP, year, and growth rate information. These visualizations directly address the research question by providing insights into African countries’ GDP growth rates over time, allowing users to identify trends and disparities in economic development.
# Calculate the growth rate for each country
growth_rates <- african_gdp_data %>%
group_by(country) %>%
summarize(start_gdp = first(gdp),
end_gdp = last(gdp),
growth_rate = (last(gdp) - first(gdp)) / first(gdp) * 100,
.groups = 'drop') %>%
arrange(desc(growth_rate))
# Identify the top 5 fastest growing economies
top_5_countries <- head(growth_rates, 5)$country
# Filter the data to only include the top 5 fastest growing countries and calculate percentage change for each year
top_5_data <- african_gdp_data %>%
filter(country %in% top_5_countries) %>%
arrange(country, year) %>%
group_by(country) %>%
mutate(percentage_change = (gdp - lag(gdp)) / lag(gdp) * 100) %>%
ungroup()
# Create the ggplot with the text aesthetic for hover information
top_5_plot <- ggplot(top_5_data, aes(x = year, y = gdp, group = country, color = country,
text = paste("Country:", country,
"<br>Year:", year,
"<br>GDP:", dollar(gdp),
"<br>Annual Growth:", round(percentage_change, 2), "%"))) +
geom_line() +
geom_point() +
scale_y_continuous(labels = scales::dollar_format(prefix = "$", suffix = "", big.mark = ",", decimal.mark = ".", accuracy = 1)) +
theme_minimal() +
labs(title = "Top 5 Fastest Growing Economies Over Time", x = "Year", y = "GDP (in USD)", caption = "Data source: Your Source")
# Convert the ggplot object to plotly for interactivity
top_5_plotly <- ggplotly(top_5_plot, tooltip = "text")
# Print the interactive plot
top_5_plotly
# Calculate the growth rate for each country
growth_rates <- african_gdp_data %>%
group_by(country) %>%
summarize(start_gdp = first(gdp),
end_gdp = last(gdp),
growth_rate = (last(gdp) - first(gdp)) / first(gdp) * 100,
.groups = 'drop') %>%
arrange(desc(growth_rate))
# Identify the top 5 slowest growing economies
top_5_slowest_countries <- tail(growth_rates, 5)$country
# Filter the data to only include these countries and calculate percentage change
top_5_slowest_data <- african_gdp_data %>%
filter(country %in% top_5_slowest_countries) %>%
arrange(country, year) %>%
mutate(perc_change = (gdp - lag(gdp)) / lag(gdp) * 100)
# Create the ggplot with hover text information
top_5_slowest_plot <- ggplot(top_5_slowest_data, aes(x = year, y = gdp, group = country, color = country,
text = paste("Year: ", year,
"<br>GDP: ", dollar(gdp, accuracy = 1),
"<br>Annual Growth: ", round(perc_change, 2), "%"))) +
geom_line() +
geom_point() +
theme_minimal() +
labs(title = "Top 5 Slowest Growing Economies Over Time", x = "Year", y = "GDP (in USD)")
# Convert the ggplot object to plotly for interactivity
top_5_slowest_plotly <- ggplotly(top_5_slowest_plot, tooltip = "text")
# Print the interactive plot
top_5_slowest_plotly
This visualization illustrates the GDP trends of Liberia over time, showcasing how its economic performance has evolved. The line plot depicts Liberia’s GDP trajectory across different years, highlighting fluctuations and overall trends in economic growth. By interacting with the plot, users can hover over data points to view specific details such as GDP values, corresponding years, and the percentage change in GDP from the previous year. In the context of the research question, which pertains to understanding the changes in GDP rankings of African countries over time, this visualization contributes by providing a focused analysis of Liberia’s economic performance. By examining Liberia’s GDP trends, policymakers, economists, and researchers can gain insights into the country’s economic development trajectory, identify periods of growth or decline, and assess the factors influencing its GDP fluctuations. This understanding of Liberia’s economic dynamics contributes to a broader comprehension of the overall economic landscape in Africa and aids in identifying patterns and trends that may inform future policy decisions and research directions
# Filter the data for Liberia and calculate percentage change
liberia_gdp <- african_gdp_data %>%
filter(country == "Liberia") %>%
arrange(year) %>%
mutate(perc_change = (gdp - lag(gdp)) / lag(gdp) * 100)
# Create the ggplot
gdp_plot <- ggplot(liberia_gdp, aes(x = year, y = gdp)) +
geom_line() +
geom_point() +
scale_y_continuous(labels = scales::dollar_format(prefix = "$", suffix = "", big.mark = ",", decimal.mark = ".", accuracy = 1)) +
theme_minimal() +
labs(title = "GDP of Liberia Over Time", x = "Year", y = "GDP (in USD)", caption = "Data source: Your Source")
# Convert to a plotly object with custom hover text
gdp_plotly <- ggplotly(gdp_plot) %>%
layout(hovermode = 'closest') %>%
add_trace(
data = liberia_gdp,
type = 'scatter',
mode = 'markers',
x = ~year,
y = ~gdp,
text = ~paste("Year: ", year, "<br>GDP: ", scales::dollar(gdp, prefix = "$", suffix = "", big.mark = ",", decimal.mark = ".", accuracy = 1), "<br>% Change: ", round(perc_change, 2), "%"),
hoverinfo = 'text'
)
# Print the interactive plot
gdp_plotly
The Flourish visualization presents an animated bar chart race showcasing the GDP rankings of multiple countries over time. As the animation progresses, viewers can observe how the positions of different countries change relative to each other in terms of GDP ranking. The height of each bar represents the GDP value, and the bars move horizontally to reflect changes in ranking over time. In the context of the research question regarding the changes in GDP rankings of African countries over time, this visualization provides valuable insights into the comparative economic progress of these nations. It allows viewers to identify which countries have experienced improvements or declines in their GDP rankings over time and assess the relative economic competitiveness of different African economies.
The Flourish visualization presents an animated bar chart race comparing Liberia’s GDP ranking with that of 20 other African countries over time. Each bar represents the GDP ranking of a specific country, with Liberia’s position highlighted for comparison. As the animation progresses, viewers can observe the movement of the bars to see how Liberia’s GDP ranking fluctuates relative to other African nations. This visualization offers a dynamic way to analyze Liberia’s economic performance in comparison to its peers within the African continent. By tracking Liberia’s position in the GDP ranking over time, viewers can assess whether the country has experienced improvements, declines, or stability in its economic standing relative to other African countries.
Wickham, H. (2016). ggplot2: Elegant Graphics for Data Analysis. Springer-Verlag New York. ISBN 978-3-319-24277-4.
Kaggle. (n.d.). Gross Domestic Product (GDP) by Country 1960-2020. Retrieved from https://www.kaggle.com/World Bank Data/gdp-ranking.
Class Lesson on Information Design & Visual Analytics.
OpenAI. (2022). ChatGPT. https://openai.com/gpt
Flourish Studio. (n.d.). Data Visualization Platform. Retrieved from https://flourish.studio/.