DSLabs HW Assignment

library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.5.0     ✔ tibble    3.2.1
✔ lubridate 1.9.3     ✔ tidyr     1.3.1
✔ purrr     1.0.2     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(dslabs)
library(gganimate)
library(gifski)
data(gapminder)

Filtering the data from 1960 to 2016

gapminder_filtered <- gapminder %>%
  filter(year >= 1960 & year <= 2016)

Creating the base plot

# Function to create base plot
create_base_plot <- function() {
  ggplot(gapminder_filtered, aes(x = gdp, y = life_expectancy, size = population, color = continent)) +
    geom_point(alpha = 0.5, shape = 20) + 
    scale_size_continuous(range = c(2, 20), labels = scales::comma) +  
    scale_x_log10(labels = scales::comma) + 
    labs(title = "GDP vs Life Expectancy",  
         x = "GDP", y = "Life Expectancy") + 
    scale_color_brewer(palette = "Pastel1") +  
    theme_dark() +
    theme(legend.position = "bottom",
          legend.key = element_blank(),  
          panel.border = element_rect(color = "black", fill = NA, linewidth = 1),  # Add dark outline around plot
          plot.title = element_text(size = 14, face = "bold", hjust = 0.5),  # Adjust title font
          axis.text = element_text(size = 12, color = "black"),  # Adjust axis text font and color
          axis.title = element_text(size = 12, color = "black"))
}

Create the base plot

p_base <- create_base_plot()
# Print the base plot
print(p_base)
Warning: Removed 2972 rows containing missing values or values outside the scale range
(`geom_point()`).

Creating the animated plot

p_animated <- p_base +
  transition_time(year) +
  labs(title = "GDP vs Life Expectancy\nYear: {frame_time}")
# Animate the plot
animate(p_animated, renderer = gifski_renderer())
Warning: Removed 90 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 90 rows containing missing values or values outside the scale range
(`geom_point()`).
Warning: Removed 91 rows containing missing values or values outside the scale range
(`geom_point()`).
Warning: Removed 89 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 89 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 89 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 89 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 89 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 89 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 89 rows containing missing values or values outside the scale range
(`geom_point()`).
Warning: Removed 83 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 83 rows containing missing values or values outside the scale range
(`geom_point()`).
Warning: Removed 81 rows containing missing values or values outside the scale range
(`geom_point()`).
Warning: Removed 80 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 80 rows containing missing values or values outside the scale range
(`geom_point()`).
Warning: Removed 79 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 79 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 79 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 79 rows containing missing values or values outside the scale range
(`geom_point()`).
Warning: Removed 72 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 72 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 72 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 72 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 72 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 72 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 72 rows containing missing values or values outside the scale range
(`geom_point()`).
Warning: Removed 71 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 71 rows containing missing values or values outside the scale range
(`geom_point()`).
Warning: Removed 67 rows containing missing values or values outside the scale range
(`geom_point()`).
Warning: Removed 66 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 66 rows containing missing values or values outside the scale range
(`geom_point()`).
Warning: Removed 63 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 63 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 63 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 63 rows containing missing values or values outside the scale range
(`geom_point()`).
Warning: Removed 62 rows containing missing values or values outside the scale range
(`geom_point()`).
Warning: Removed 51 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 51 rows containing missing values or values outside the scale range
(`geom_point()`).
Warning: Removed 47 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 47 rows containing missing values or values outside the scale range
(`geom_point()`).
Warning: Removed 44 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 44 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 44 rows containing missing values or values outside the scale range
(`geom_point()`).
Warning: Removed 41 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 41 rows containing missing values or values outside the scale range
(`geom_point()`).
Warning: Removed 38 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 38 rows containing missing values or values outside the scale range
(`geom_point()`).
Warning: Removed 35 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 35 rows containing missing values or values outside the scale range
(`geom_point()`).
Warning: Removed 31 rows containing missing values or values outside the scale range
(`geom_point()`).
Warning: Removed 29 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 29 rows containing missing values or values outside the scale range
(`geom_point()`).
Warning: Removed 27 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 27 rows containing missing values or values outside the scale range
(`geom_point()`).
Warning: Removed 14 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 14 rows containing missing values or values outside the scale range
(`geom_point()`).
Warning: Removed 13 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 13 rows containing missing values or values outside the scale range
(`geom_point()`).
Warning: Removed 11 rows containing missing values or values outside the scale range
(`geom_point()`).
Warning: Removed 10 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 10 rows containing missing values or values outside the scale range
(`geom_point()`).
Warning: Removed 8 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 8 rows containing missing values or values outside the scale range
(`geom_point()`).
Warning: Removed 6 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 6 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 6 rows containing missing values or values outside the scale range
(`geom_point()`).
Warning: Removed 4 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 4 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 4 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 4 rows containing missing values or values outside the scale range
(`geom_point()`).
Warning: Removed 2 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 2 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 2 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 2 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 2 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 2 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 2 rows containing missing values or values outside the scale range
(`geom_point()`).
Warning: Removed 3 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 3 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 3 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 3 rows containing missing values or values outside the scale range
(`geom_point()`).
Warning: Removed 4 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 4 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 4 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 4 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 4 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 4 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 4 rows containing missing values or values outside the scale range
(`geom_point()`).
Warning: Removed 9 rows containing missing values or values outside the scale range
(`geom_point()`).
Warning: Removed 17 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 17 rows containing missing values or values outside the scale range
(`geom_point()`).
Warning: Removed 185 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 185 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 185 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 185 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 185 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 185 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 185 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 185 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 185 rows containing missing values or values outside the scale range
(`geom_point()`).

Creating the faceted plot

p_faceted <- p_base +
  facet_wrap(~continent) + 
  transition_time(year) +
  labs(title = "GDP vs Life Expectancy\nYear: {frame_time}")
# Animate the faceted plot
animate(p_faceted, renderer = gifski_renderer())
Warning: Removed 90 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 90 rows containing missing values or values outside the scale range
(`geom_point()`).
Warning: Removed 91 rows containing missing values or values outside the scale range
(`geom_point()`).
Warning: Removed 89 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 89 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 89 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 89 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 89 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 89 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 89 rows containing missing values or values outside the scale range
(`geom_point()`).
Warning: Removed 83 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 83 rows containing missing values or values outside the scale range
(`geom_point()`).
Warning: Removed 81 rows containing missing values or values outside the scale range
(`geom_point()`).
Warning: Removed 80 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 80 rows containing missing values or values outside the scale range
(`geom_point()`).
Warning: Removed 79 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 79 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 79 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 79 rows containing missing values or values outside the scale range
(`geom_point()`).
Warning: Removed 72 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 72 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 72 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 72 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 72 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 72 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 72 rows containing missing values or values outside the scale range
(`geom_point()`).
Warning: Removed 71 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 71 rows containing missing values or values outside the scale range
(`geom_point()`).
Warning: Removed 67 rows containing missing values or values outside the scale range
(`geom_point()`).
Warning: Removed 66 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 66 rows containing missing values or values outside the scale range
(`geom_point()`).
Warning: Removed 63 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 63 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 63 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 63 rows containing missing values or values outside the scale range
(`geom_point()`).
Warning: Removed 62 rows containing missing values or values outside the scale range
(`geom_point()`).
Warning: Removed 51 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 51 rows containing missing values or values outside the scale range
(`geom_point()`).
Warning: Removed 47 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 47 rows containing missing values or values outside the scale range
(`geom_point()`).
Warning: Removed 44 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 44 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 44 rows containing missing values or values outside the scale range
(`geom_point()`).
Warning: Removed 41 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 41 rows containing missing values or values outside the scale range
(`geom_point()`).
Warning: Removed 38 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 38 rows containing missing values or values outside the scale range
(`geom_point()`).
Warning: Removed 35 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 35 rows containing missing values or values outside the scale range
(`geom_point()`).
Warning: Removed 31 rows containing missing values or values outside the scale range
(`geom_point()`).
Warning: Removed 29 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 29 rows containing missing values or values outside the scale range
(`geom_point()`).
Warning: Removed 27 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 27 rows containing missing values or values outside the scale range
(`geom_point()`).
Warning: Removed 14 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 14 rows containing missing values or values outside the scale range
(`geom_point()`).
Warning: Removed 13 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 13 rows containing missing values or values outside the scale range
(`geom_point()`).
Warning: Removed 11 rows containing missing values or values outside the scale range
(`geom_point()`).
Warning: Removed 10 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 10 rows containing missing values or values outside the scale range
(`geom_point()`).
Warning: Removed 8 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 8 rows containing missing values or values outside the scale range
(`geom_point()`).
Warning: Removed 6 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 6 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 6 rows containing missing values or values outside the scale range
(`geom_point()`).
Warning: Removed 4 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 4 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 4 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 4 rows containing missing values or values outside the scale range
(`geom_point()`).
Warning: Removed 2 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 2 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 2 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 2 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 2 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 2 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 2 rows containing missing values or values outside the scale range
(`geom_point()`).
Warning: Removed 3 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 3 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 3 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 3 rows containing missing values or values outside the scale range
(`geom_point()`).
Warning: Removed 4 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 4 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 4 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 4 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 4 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 4 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 4 rows containing missing values or values outside the scale range
(`geom_point()`).
Warning: Removed 9 rows containing missing values or values outside the scale range
(`geom_point()`).
Warning: Removed 17 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 17 rows containing missing values or values outside the scale range
(`geom_point()`).
Warning: Removed 185 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 185 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 185 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 185 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 185 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 185 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 185 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 185 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 185 rows containing missing values or values outside the scale range
(`geom_point()`).

Summary:

In this analysis, I wanted to explore the connection between a country’s GDP and its life expectancy. To do this, I utilized the “gapminder” dataset, a collection of economic and health data spanning numerous countries and decades. Specifically, I zoomed in on the period from 1960 to 2016 to observe recent trends. Upon running the initial code, a static graph appeared, prominently featuring Africa and Asia as focal points. This observation prompts consideration regarding the reasons behind this prominence.

Subsequently, when the animated plot was generated, intriguing patterns emerged. Asia stood out with the largest population, and all continents exhibited a positive trend, with varying degrees of increase. Notably, Africa’s trajectory appeared slightly lower compared to other continents.

Futhermore, when generating the faceted plot, the unique pace of increase for each continent became apparent. Asia notably outpaced other continents in its upward trajectory. The Americas and Europe showed steady increases with some notable peaks. Oceania had the smallest points but also experienced growth. Across all continents, there was a consistent pattern of overall increase, though occasionally interrupted by fluctuations. To sum up, the analysis uncovered a consistent global trend: as GDP increases, so does life expectancy.