# Load the packages needed for the dataset and graph.
library(dslabs)
library(dplyr)
library(ggplot2)Fertility Rate and Infant Mortality in 2016
Load the Packages
Prepare the Data
# Use only the data from 2016.
# Remove rows with missing values in the variables we need.
gapminder_2016 <- gapminder %>%
filter(year == 2016) %>%
filter(!is.na(fertility),
!is.na(infant_mortality),
!is.na(life_expectancy))Create the Scatterplot
# Put fertility on the x-axis and infant mortality on the y-axis.
# Use life expectancy as a third variable shown by point color.
ggplot(gapminder_2016,
aes(x = fertility, y = infant_mortality,
color = life_expectancy)) +
geom_point(size = 3, alpha = 0.8) +
# Change the default point colors and include a color legend.
scale_color_gradient(low = "#2C7FB8", high = "#D95F0E",
name = "Life expectancy\n(years)") +
# Add a title, axis labels with units, and the required source.
labs(title = "Fertility Rate and Infant Mortality in 2016",
x = "Fertility rate (children per woman)",
y = "Infant mortality (deaths per 1,000 live births)",
caption = "Source: DS Labs") +
# Use a non-default ggplot theme.
theme_minimal()Description
I used the gapminder dataset from the dslabs package. My graph uses fertility rate, infant mortality, and life expectancy for countries in 2016. I first filtered the data to include only 2016 and removed missing values for these variables. Then I created a scatterplot using ggplot2. Fertility rate is on the x-axis, infant mortality is on the y-axis, and the color of each point shows life expectancy. I also changed the colors and used theme_minimal() instead of the default ggplot theme.
The graph helps us compare three measures of health and population at the same time. In general, countries with higher fertility rates tend to have higher infant mortality rates. Countries with lower infant mortality also tend to have higher life expectancy. This is an overall pattern, and not every country follows it exactly. My graph focuses on these three variables in one year rather than showing changes over time.