In this step, I load the ‘gapminder’ dataset from the ‘dslabs’ package and examine the structure to understand what variables are available.
library(ggthemes)library(ggrepel)data(gapminder)
Prepare the dataset
In this step, I select the variables ‘fertility’, ‘life_expectancy’, and ‘continent’ from the dataset. I also remove missing values and convert the ‘continent’ column into a factor to group the data properly.
fertility life_expectancy continent
1 6.19 62.87 Europe
2 7.65 47.50 Africa
3 7.32 35.98 Africa
4 4.43 62.97 Americas
5 3.11 65.39 Americas
6 4.55 66.86 Asia
Create the multivariable scatterplot
here, I visualize the relationship between fertility rate and life expectancy. I use continent as a third variable represented by color.
ggplot(df, aes(x = fertility, y = life_expectancy, color = continent)) +geom_point(size =3, alpha =0.8) +labs(title ="Fertility Rate vs Life Expectancy by Continent",x ="Fertility Rate (Births per Woman)",y ="Life Expectancy (Years)",color ="Continent" ) +theme_economist() +# From ggthemesscale_color_brewer(palette ="Set1")
Description
For this assignment, I used the gapminder dataset from the dslabs package. This dataset includes global statistics such as fertility rate, life expectancy, GDP, and population by country and year. I chose this dataset because it allows me to explore meaningful relationships between health and development indicators across continents.
I selected three variables: fertility, life_expectancy, and continent. After checking for missing values and cleaning the data, I created a multivariable scatterplot. The x-axis represents fertility rate (births per woman), and the y-axis shows life expectancy (years). I used continent as a third variable represented by color. I applied a non-default theme (theme_economist) and changed the color palette using scale_color_brewer() to make the graph more readable and visually engaging.
The resulting plot reveals an inverse relationship between fertility and life expectancy and highlights regional differences across continents.