# 3D Scatter Plot of Changes in GDP & Life Expectancy Over Time
plot_ly(gapminder, x = gdpPercap, y = lifeExp, z = year,
type = "scatter3d", mode = "markers", color = continent,
marker = list(size = 4, opacity = 0.8)) %>%
layout(title = "GDP vs. Life Expectancy vs. Year", scene = list(
xaxis = list(title = "GDP per Capita (log)", type = "log"),
yaxis = list(title = "Life Expectancy (years)"),
zaxis = list(title = "Year")))
# Linear Relationship Between GDP & Life Expectancy
data2007 <- subset(gapminder, year == 2007)
ggplot(data2007, aes(x = gdpPercap, y = lifeExp)) +
scale_x_log10(labels = scales::comma) +
geom_point(aes(color = continent), size = 1.5, alpha = 0.6) +
geom_smooth(method = "lm", se = T, color = "midnightblue") +
labs(title = "Linear Regression of GDP vs. Life Expectancy (2007)",
x = "GDP per Capita (log)", y = "Life Expectancy (years)") +
theme_light()
`geom_smooth()` using formula = 'y ~ x'
