Nations_Charts_Assignments

Author

Jonathan_RH

Loading Tidyverse

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

Bring in the data set

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.

Mutating The Data

Nations_GDP <- nations |>
  mutate(gdp_trillions = (gdp_percap * population)/10^12)

Data for Plot 1

P1_Data <- Nations_GDP |>
  filter(country %in% c("Nicaragua", "Costa Rica", "El Salvador", "Guatemala", "Honduras")) |>
   mutate(country = fct_reorder(country, gdp_trillions, .desc = TRUE))

Plot 1

p1 <- ggplot(P1_Data, aes(x = year, y = gdp_trillions, group = country, color = country)) +
  geom_line(size = 0.6)+
  geom_point() +
  scale_color_brewer(palette = "Set1") +
  labs(title ="The GPD of the Orignial Five Centeral American Countries", x = "Year", y = "GDP (TRILLIONS)", color = "CA Countries") +
  theme_bw()
Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
ℹ Please use `linewidth` instead.
p1

Data for Plot 2

P2_Data <- Nations_GDP |>
  group_by(region,year) |>
  summarise(gdp_trillions = sum(gdp_trillions, na.rm = TRUE))
`summarise()` has grouped output by 'region'. You can override using the
`.groups` argument.

Plot 2

p2 <- ggplot(P2_Data, aes(x = year, y = gdp_trillions)) +
  geom_area(color = 'white', aes(fill = region)) +
  scale_fill_brewer(palette = 'YlOrRd') +
  labs(title = 'GDP by World Bank Region', x = 'Year', y = 'GDP ($Trillion)', fill = 'Region') +
  theme_dark()
  

p2

Source

Organzing the legends https://forcats.tidyverse.org/reference/fct_reorder.html