Relationships Between GDP per Capita and life Expectancy (2007)
library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr 1.2.0 ✔ readr 2.1.6
✔ forcats 1.0.1 ✔ stringr 1.6.0
✔ ggplot2 4.0.2 ✔ tibble 3.3.1
✔ lubridate 1.9.5 ✔ tidyr 1.3.2
✔ purrr 1.2.1
── 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)data("gapminder")
# This chunk creates a scatter plot showing the relationship between GDP per capiat and life expectancy using the Gapminder dataset.gapminder %>%filter(year==2007)%>%ggplot(aes(x=gdp, y= life_expectancy, color= continent))+geom_point(size=3, alpha =0.7)+geom_smooth(method="lm", se=FALSE, color="black")+scale_x_log10()+labs(title ="Relationship Between GDP per Capita and Life Expectancy (2007)",subtitle="Using Gapminder Dataset from Dslabs",x="GDP per Capita(log scale)",y="Life Expectancy",color="Continent",caption="Source: DSLabs Gapminder dataset" )+theme_minimal()
`geom_smooth()` using formula = 'y ~ x'
Warning: Removed 4 rows containing non-finite outside the scale range
(`stat_smooth()`).
Warning: Removed 4 rows containing missing values or values outside the scale range
(`geom_point()`).
This visualization uses the Gapminder dataset from DSLabs to look at the relationship between GDP per capita and life expectancy in 2007. I used a scatter plot with GDP on a log scale and life expectancy on the y-axis. I also colored the points by continent to include another variable. Compared to what we did in class, I added a regression line and used a log scale to better show how the data is spread out. The graph shows that countries with higher GDP tend to have higher life expectancy. One thing I found interesting is that African countries are mostly grouped at the lower end for both GDP and life expectancy, while countries in Europe and the Americas are generally higher. It also looks like after a certain point, increases in GDP don’t lead to big increases in life expectancy.