Problem 01: Dataset Description and variables
I took data from gapminder that provides global statistics about life expectancy (lifeExp), GDP per capita (gdpPercap), population (pop), and continent (continent) from multiple years (year). The dataset monitors how health and economic metrics transform across multiple years for nations worldwide.
library(ggplot2)
library(plotly)
## Warning: package 'plotly' was built under R version 4.4.3
##
## 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(gapminder)
## Warning: package 'gapminder' was built under R version 4.4.3
data(gapminder)
head(gapminder)
## # A tibble: 6 × 6
## country continent year lifeExp pop gdpPercap
## <fct> <fct> <int> <dbl> <int> <dbl>
## 1 Afghanistan Asia 1952 28.8 8425333 779.
## 2 Afghanistan Asia 1957 30.3 9240934 821.
## 3 Afghanistan Asia 1962 32.0 10267083 853.
## 4 Afghanistan Asia 1967 34.0 11537966 836.
## 5 Afghanistan Asia 1972 36.1 13079460 740.
## 6 Afghanistan Asia 1977 38.4 14880372 786.
In this step I loaded the data for assessment of its structure. The collected variables include numeric values like GDP and life expectancy together with categorical data such as continent which makes visualization possible.
Problem 02: Static Visualization
The static plot shows GDP per capita (USD) and life expectancy data using continent colors and displays a black regression line while utilizing log-scaled per capita GDP values. Transparency (alpha = 0.6) avoids overplotting.
ggplot(gapminder, aes(x = gdpPercap, y = lifeExp, color = continent)) +
geom_point(alpha = 0.6) +
geom_smooth(method = "lm", color = "black", se = FALSE) +
scale_x_log10(labels = scales::dollar) + # I added the scale function so that its readable by spreading out the GDP values more evenly and it shows the numbers as dollar amounts clearer.
labs(title = "Wealth vs Health by Continent",
x = "GDP per Capita (USD)",
y = "Life Expectancy (Years)") +
theme_minimal()
## `geom_smooth()` using formula = 'y ~ x'
To summarise, GDP and life expectancy demonstrate positive connections through the data presented in the plot.
Problem 03 Interactive Chart
Generated an interactive chart based on plotly: represents average statistics from continents regarding GDP, life expectancy and total population values. The chart shows specific numerical values through tooltips when users hover their mouse cursor.
library(gapminder)
library(ggplot2)
library(plotly)
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
continent_data <- gapminder %>%
group_by(continent) %>%
summarize(
avg_life = mean(lifeExp),
avg_gdp = mean(gdpPercap),
total_pop = sum(pop)
)
# Now I'm creating a ggplot bubble chart
p <- ggplot(continent_data, aes(x = avg_gdp, y = avg_life, size = total_pop, color = continent)) +
geom_point(alpha = 0.7) +
scale_size(range = c(5, 30), name = "Total Population") +
labs(
title = "Life Expectancy vs GDP by Continent",
x = "Average GDP per Capita",
y = "Average Life Expectancy",
color = "Continent"
) +
theme_minimal() +
scale_x_continuous(labels = scales::dollar_format()) +
theme(legend.position = "right")
interactive_plot <- ggplotly(p)
interactive_plot
To conclude, the interactivity features help to examine specific set of data about Africa’s reduced GDP/life expectancy ratio as compared to Europe’s increased averages. Through size measurement I have identified the significant population size of Asia.
Problem 04 Animation
Generated an animated visual presentation illustrates how GDP and life expectancy and population changed throughout the years. Data points adjust in size according to population changes and the different regions show their data through color coding.
library(gganimate)
## Warning: package 'gganimate' was built under R version 4.4.3
animated_plot <- ggplot(gapminder, aes(x = gdpPercap, y = lifeExp, size = pop, color = continent)) +
geom_point(alpha = 0.7) +
scale_size(range = c(2, 15)) +
scale_x_log10() +
labs(title = "Year: {frame_time}", x = "GDP per Capita", y = "Life Expectancy") +
transition_time(year) +
ease_aes("linear")
animate(animated_plot, duration = 15, fps = 10, renderer = gifski_renderer())
The animated presentation indicates global advancements through life expectancy increases but it also demonstrates discrepancies because Africa has lagged behind in terms of GDP growth.