GDP and Life Expectancy Visualization

library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.2.1     ✔ readr     2.2.0
✔ forcats   1.0.1     ✔ stringr   1.6.0
✔ ggplot2   4.0.3     ✔ tibble    3.3.1
✔ lubridate 1.9.5     ✔ tidyr     1.3.2
✔ purrr     1.2.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(openintro)
Loading required package: airports
Loading required package: cherryblossom
Loading required package: usdata
library(dslabs)

Attaching package: 'dslabs'

The following object is masked from 'package:openintro':

    murders
library(ggplot2)
library(dplyr)
library(scales)

Attaching package: 'scales'

The following object is masked from 'package:purrr':

    discard

The following object is masked from 'package:readr':

    col_factor
data("gapminder")
head(gapminder)
              country year infant_mortality life_expectancy fertility
1             Albania 1960           115.40           62.87      6.19
2             Algeria 1960           148.20           47.50      7.65
3              Angola 1960           208.00           35.98      7.32
4 Antigua and Barbuda 1960               NA           62.97      4.43
5           Argentina 1960            59.87           65.39      3.11
6             Armenia 1960               NA           66.86      4.55
  population          gdp continent          region
1    1636054           NA    Europe Southern Europe
2   11124892  13828152297    Africa Northern Africa
3    5270844           NA    Africa   Middle Africa
4      54681           NA  Americas       Caribbean
5   20619075 108322326649  Americas   South America
6    1867396           NA      Asia    Western Asia
gapminder_clean <- gapminder %>%
  filter(
    !is.na(gdp),
    !is.na(life_expectancy),
    !is.na(population),
    !is.na(continent),
    gdp > 0,
    population > 0
  )
ggplot(gapminder_clean, aes(x = gdp, y = life_expectancy,
                            color = continent, size = population)) +
  geom_point(alpha = 0.6) +
  scale_x_log10() +
  labs(
    title = "GDP and Life Expectancy Around the World",
    subtitle = "Comparing countries by wealth, population, and continent",
    x = "GDP Per Capita",
    y = "Life Expectancy",
    color = "Continent",
    size = "Population",
    caption = "Source: Gapminder Dataset"
  )

# I used the Gapminder dataset from the `dslabs` package. This dataset contains information about countries like population, GDP per capita and life expectancy. I wanted to explore whether countries with higher GDP per capita generally have higher life expectancy and whether population size or geographic location shows any other patterns. The visualization uses four variables to look at the relationship  between economic development and life expectancy. I used a scatterplot  because it allows multiple continuous variables to be displayed at the same time.