slums=sdg_data("EN_LND_SLUM")
water=sdg_data("SH_H2O_SAFE")
combined_data <- left_join(water, slums, by = c("GeoAreaName", "TimePeriod"))
latin_america <- c("Brazil", "Mexico", "El Salvador", "Guatemala", "Argentina", "Chile", "Colombia")
filtered_data <- combined_data %>%
  filter(GeoAreaName %in% latin_america)
summary_stats <- filtered_data %>%
  summarise(Average_Water_Access = mean(Value.x, na.rm = TRUE),
            Average_Slum_Population = mean(Value.y, na.rm = TRUE))

correlation <- cor(filtered_data$Value.x, filtered_data$Value.y, use = "complete.obs")

summary_stats
## # A tibble: 1 × 2
##   Average_Water_Access Average_Slum_Population
##                  <dbl>                   <dbl>
## 1                 68.4                    26.3
correlation
## [1] -0.3854679
ggplot(filtered_data, aes(x = Value.x, y = Value.y)) +
  geom_point() +
  geom_smooth(method = "lm") +
  labs(title = "Access to Clean Water Services and Population in Slums, Selected Latin American Countries",
       x = "Percentage of Population with Access to Clean Water",
       y= "Percentage of Population Living in Slums")
## `geom_smooth()` using formula = 'y ~ x'
## Warning: Removed 175 rows containing non-finite values (`stat_smooth()`).
## Warning: Removed 175 rows containing missing values (`geom_point()`).