# Load the packages library(dslabs)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
Load the Data
# Load the gapminder datasetdata(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
Prepare the Data
# Keep only the year 2011 so each country appears once# Compute dollars per day# Remove rows with missing valuesgap_2011 <- gapminder %>%filter(year ==2011) %>%mutate(dollars_per_day = gdp / population /365) %>%filter(!is.na(dollars_per_day), !is.na(life_expectancy))
Create the Base Plot
# Set up the plotp <-ggplot(gap_2011, aes(x = dollars_per_day,y = life_expectancy,color = continent))
Add the Points
# Add a point for each countryp <- p +geom_point(aes(size = population), alpha =0.7)
Set the Axis Scale
# Show the x-axis values with dollar signsp <- p +scale_x_log10(labels = scales::dollar)
Choose Colors
# The legend title is set to "Continent"p <- p +scale_color_manual(values =c("Africa"="red","Americas"="orange","Asia"="forestgreen","Europe"="blue","Oceania"="purple"),name ="Continent")# Control the point sizes and hide the population legendp <- p +scale_size(range =c(2, 12), guide ="none")
Add Titles, Labels, and Caption
# Add a title, subtitle, axis labels, and the data source captionp <- p +labs(title ="Richer Countries Tend to Live Longer",subtitle ="Daily income vs. life expectancy by continent",x ="Income per person per day (US dollars, log scale)",y ="Life expectancy (years)",caption ="Source: DS Labs (dslabs package, gapminder dataset)")
Apply a Theme
# Add a minimal themep <- p +theme_minimal(base_size =13)# bold title, gray subtitle, captionp <- p +theme(plot.title =element_text(face ="bold", size =16),plot.subtitle =element_text(color ="gray"),plot.caption =element_text(face ="italic"),)
Display the Graph
# Print the finished scatterplotp
Interpretation
The graph shows a clear positive relationship: as daily income rises, life expectancy rises too, though the gains level off at higher incomes. African countries cluster at the lower left, with low income and shorter lives, while European countries cluster at the upper right. Asian and American countries fall in between and show the widest spread. Larger points, such as China and India, show that populous countries can sit anywhere on the income scale.