Nations KMB

library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.5.1     ✔ tibble    3.2.1
✔ lubridate 1.9.4     ✔ tidyr     1.3.1
✔ purrr     1.0.4     
── 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(ggplot2)
library(dplyr)
library(RColorBrewer)
setwd("~/Desktop/Data Science MC/Data Science 110")
nations <- read.csv("nations.csv")

Top 4 Economies within North and South America

nations <- nations %>%
  mutate(gdp = (gdp_percap * population) / 1e12)
selected_countries <- c("United States", "Canada", "Mexico", "Brazil")
nations_filtered <- nations %>%
  filter(country %in% selected_countries, !is.na(gdp))
ggplot(nations_filtered, aes(x = year, y = gdp_percap, color = country)) +
  geom_point(size = 3, alpha = 0.8, shape = 20) +
  geom_line(size = 1)+
  scale_color_brewer(palette = "Set2") +
  labs(title = "Top Economies of North and South America", x = "Year", y = "GDP Per Capita (USD)") + 
  theme_bw() +
  theme(plot.title = element_text(hjust = 0.5, face = "bold", size = 14))
Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
ℹ Please use `linewidth` instead.

country_gdp <- nations %>%
  filter(country %in% selected_countries) %>%
  group_by(country, year) %>%
  summarise(sum_gdp = sum(gdp, na.rm = TRUE))
`summarise()` has grouped output by 'country'. You can override using the
`.groups` argument.
ggplot(country_gdp, aes(x = year, y = sum_gdp, fill = country)) +
  geom_area(color = "white", size = 0.2) +
  scale_fill_brewer(palette = "Set2") +  
  labs(title = "Total GDP of the Largest North and South American Economies",
       x = "Year",
       y = "Total GDP (Trillions in USD)",
       fill = "Country") +
  theme_bw() +
  theme(plot.title = element_text(hjust = 0.5, face = "bold", size = 12))

Disclaimer

The Area Graph is showing the United States is dominating in the growth of GDP over the years, while other countries GDPs aren’t as large but are steadily increasing amongst other major American economies.