library(tidyverse) library(gt) library(gapminder)

filtered_data <- gapminder %>% filter(year %in% c(1987, 2007))

life_exp_summary <- gapminder %>% filter(year %in% c(1987, 2007)) %>% group_by(continent, country) %>% summarize( start_life = first(lifeExp),
end_life = last(lifeExp),
change = end_life - start_life,
.groups = “drop” )

target_countries <- c(“Niger”, “Bangladesh”, “El Salvador”, “Iraq”, “Zimbabwe”)

focal_countries <- life_exp_summary %>% filter(country %in% target_countries)

print(focal_countries)

life_exp_summary_avg <- life_exp_summary %>% group_by(continent) %>% summarise( avg_change = mean(change, na.rm = TRUE), avg_begin_life = mean(start_life, na.rm = TRUE), avg_end_life = mean(end_life, na.rm = TRUE) ) %>% mutate(continent = factor(continent, levels = c(“Africa”, “Asia”, “Europe”, “North America”, “Oceania”, “South America”)))

life_exp_summary_avg %>% arrange(desc(avg_change)) %>% gt() %>% cols_label( continent = “Continent”, avg_begin_life = “1987”, avg_end_life = “2007”, avg_change = “Change” ) %>% tab_style( style = cell_text(weight = “bold”), locations = cells_column_labels( columns = c(continent, avg_begin_life, avg_end_life, avg_change) ) ) %>% fmt_number( columns = c(“avg_begin_life”, “avg_end_life”, “avg_change”),
decimals = 1 ) %>% tab_header( title = md(“Life Expectancy Changes by Continent”), subtitle = “Average life expectancy in years” ) %>% tab_source_note( source_note = “Note: Data from Gapminder” )

selected_countries <- c(“Niger”, “Bangladesh”, “El Salvador”, “Iraq”, “Zimbabwe”) data <- gapminder %>% filter(country %in% selected_countries & year %in% c(1987, 2007))

str(data)

ggplot(data, aes(x = year, y = lifeExp, color = country, group = country)) + geom_line(size = 1.5, linetype = “solid”) + geom_point(size = 3) + # Adds dots for clarity scale_color_viridis_d(option = “D”) + theme_minimal(base_size = 14) +
theme( legend.position = “bottom”,
plot.title = element_text(face = “bold”, size = 16, color = “darkblue”),
plot.subtitle = element_text(size = 13, color = “darkgray”),
axis.title.x = element_text(size = 13, color = “black”),
axis.title.y = element_text(size = 13, color = “black”),
panel.grid.minor = element_blank(),
panel.grid.major.y = element_blank(),
panel.border = element_rect(color = “black”, fill = NA, linewidth = 0.8)
) + labs( title = ” Life Expectancy Changes (1987-2007)“,
subtitle =”Comparing Growth in Selected Countries “,
x =”Year”,
y = “Average Life Expectancy (Years)”,
color = “Country” )

  1. In 1987, Asia had the second lowest life expectancy. By 2007, on average. Asia has had the largest increase in life expectancy for a single continent. Even with the largest change in life expectancy, in 2007, Asia is still second last in life expectancy with Africa behind them at 54.8, followed by North America, Europe, and Oceania with the highest life expectancy at 80.7 years. The change rate in life expectancy increase between most of the continents is the same. Asia and North America only have a 0.4 difference in the change in life expectancy, while Oceania is close behind North America with a 0.1 difference. The largest difference between Europe, Oceania, North America and Asia and Africa is that over a twenty year period, Africa remained having the smallest change in life expectancy at an increase of 1.5 years.

  2. In 1987, Zimbabwe, Iraq and El Salvador all begin in the general area of one another, having a life expectancy of around 62-65. In 1987, Bangladesh is significantly behind Zimbabwe, Iraq, and El Salvador with an average life expectancy of just over 50 years old. At the time, in 1987, Niger had the lowest average life expectancy at around 45 years old. El Salvador, Bangladesh, Niger each demonstrate a steady increase in life expectancy between the years 1987 and 2007 with the slope. Iraq’s average life expectancy decreased in the twenty years, ending at 60 in 2007. Zimbabwe demonstrates the most drastic decrease in life expectancy with having a similar life expectancy to two other countries, but ending completely distant from them in 2007 with a average life expectancy of approximately 45 years old.

load(“anes_2020-2.rda”) nona_anes <- anes_2020 %>% filter(!is.na(TrustPeople) & !is.na(AgeGroup)) nona_anes

trust_percentage <- nona_anes %>% group_by(AgeGroup, TrustPeople) %>% summarise(count = n(), .groups = “drop”) %>%
mutate(percent = (count / sum(count)) * 100)

print(trust_percentage)

total_sample_size <- nrow(nona_anes) print(total_sample_size)

trust_table <- nona_anes %>% group_by(TrustPeople, AgeGroup) %>% summarize(count = n(), .groups = ‘drop’) %>% group_by(AgeGroup) %>% mutate(percentage = count / sum(count) * 100) %>% ungroup() %>% select(AgeGroup, TrustPeople, percentage) %>%

gt() %>%

cols_label( AgeGroup = md(“Age Group”), TrustPeople = md(“Trust People”), percentage = md(“Percentage”) ) %>%

# Style headers (Bold) tab_style( style = cell_text(weight = “bold”), locations = cells_column_labels(columns = c(AgeGroup, TrustPeople, percentage)) ) %>%

fmt_number( columns = c(percentage), decimals = 1 ) %>%

tab_header( title = md(“Interpersonal Trust by Age Group”), subtitle = “Distribution of Responses (percentages)” ) %>%

tab_source_note( source_note = paste(“Data: ANES 2020 (Sample size:”, total_sample_size, “)”) )

print (trust_table)

nona_anes %>% group_by(TrustPeople, AgeGroup) %>% summarise(count = n(), .groups = “drop”) %>% ungroup() %>% group_by(AgeGroup) %>% mutate(percentage = count / sum(count) * 100) %>% ungroup() %>% ggplot(aes(x = AgeGroup, y = percentage, fill = TrustPeople)) + geom_bar(stat = “identity”, position = “stack”) + scale_fill_viridis_d(option = “mako”) + coord_flip() + theme_minimal() +

denmark_data <- read_fst(“denmark_data.fst”) italy_data <- read_fst(“italy_data.fst”)

head(denmark_data)