Nations HW

Here we set everything up by loading our library, setting our working directory and pulling in the dataset into a dataframe.

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
setwd("~/Data 110 Class Folder")
nations <- read_csv("nations.csv")
Rows: 5275 Columns: 10
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (5): iso2c, iso3c, country, region, income
dbl (5): year, gdp_percap, population, birth_rate, neonat_mortal_rate

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.

Mutate the dataset to add a GDP column.

nations <- nations %>%
  mutate(gdp = (gdp_percap * population) / 10^12)

Create a new dataframe filtering for the 4 countries we want to look at.

df1 <- nations |>
  filter(country %in% c("Singapore", "Malaysia", "Indonesia", "Vietnam"))

Create a plot of the GDP of the 4 selected countries.

ggplot(df1, aes(x = year, y = gdp, color = country, group = country)) +
  geom_line(size = 1) +  
  geom_point(size = 2) +  
  scale_color_brewer(palette = "Set1") +
  labs(title = "GDP Over Time for Selected Countries",
       x = "Year",
       y = "GDP (Trillions of USD)",
       color = "Country") +
  theme_minimal()
Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
ℹ Please use `linewidth` instead.

Create a new dataframe, grouping all the nations by region and plotting their GDP.

df_region <- nations %>%
  group_by(region, year) %>%
  summarise(sum_GDP = sum(gdp, na.rm = TRUE))
`summarise()` has grouped output by 'region'. You can override using the
`.groups` argument.
ggplot(df_region, aes(x = year, y = sum_GDP, fill = region)) +
  geom_area(color = "white", size = 0.2) +  
  scale_fill_brewer(palette = "Set2") +  
  labs(title = "Total GDP by Region Over Time",
       x = "Year",
       y = "GDP (Trillions of USD)",
       fill = "Region") +
  theme_minimal()