R Markdown Presentation with Plotly: Life Expectancy Analysis

Anam Shaikh

2026-06-11

Introduction

This presentation explores the relationship between economic prosperity and life expectancy using the Gapminder dataset. The interactive visualization, built with Plotly in R, allows viewers to examine how GDP per capita correlates with life expectancy across countries worldwide.

The Gapminder dataset, popularized by Hans Rosling, contains longitudinal data on life expectancy, population, and GDP per capita for 142 countries spanning from 1952 to 2007. This analysis focuses on the 2007 snapshot to provide a contemporary view of global health and wealth disparities.

Overview

It creates a scatter plot comparing:

X-axis: GDP per capita (purchasing power parity) Y-axis: Life expectancy in years Hover text: Country names for identification The visualization reveals a well-documented pattern: wealthier nations generally have higher life expectancies, though the relationship is logarithmic rather than linear-gains in life expectancy diminish at higher income levels.

Life Expectancy vs. GDP per Capita (2007)

latest <- gapminder %>%
  filter(year == 2007)

plot_ly(
  latest,
  x = ~gdpPercap,
  y = ~lifeExp,
  color = ~continent,
  colors = c("Africa" = "#00BFA5", "Americas" = "#FF7043", 
             "Asia" = "#5C6BC0", "Europe" = "#EC407A", "Oceania" = "#9CCC65"),
  size = ~pop,
  sizes = c(10, 80),          # ← KEY FIX: min/max bubble size in pixels
  text = ~paste(
    "<b>", country, "</b><br>",
    "Life Expectancy:", round(lifeExp, 1), "years<br>",
    "GDP per Capita: $", format(round(gdpPercap), big.mark = ","), "<br>",
    "Population:", format(pop, big.mark = ",")
  ),
  hoverinfo = "text",
  type = "scatter",
  mode = "markers",
  marker = list(
    sizemode = "area",         # ← use "area" (more perceptually accurate than "diameter")
    opacity = 0.7,
    line = list(width = 1, color = "white")
  )
) %>%
  layout(
    title = list(text = "Life Expectancy vs. GDP per Capita (2007)", font = list(size = 18)),
    xaxis = list(title = "GDP per Capita (USD)", type = "log"),
    yaxis = list(title = "Life Expectancy (Years)"),
    legend = list(title = list(text = "Continent")),
    hoverlabel = list(bgcolor = "white", font = list(size = 12))
  )

Conclusion

The enhanced visualizations reveal several insights: 1. Strong correlation exists between GDP and life expectancy, particularly at lower income levels 2. Continental clustering shows Africa lagging behind other regions, while Europe leads 3. Convergence over time - the line chart demonstrates that all continents improved significantly from 1952 to 2007, with some narrowing of gaps 4. Outliers matter - countries like South Africa (affected by HIV/AIDS) and oil-rich nations challenge simple wealth-health assumptions