Nations Charts Assignment

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.4.4     ✔ tibble    3.2.1
✔ lubridate 1.9.3     ✔ tidyr     1.3.1
✔ purrr     1.0.2     
── 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(dplyr)
library(ggplot2)
library(scales)

Attaching package: 'scales'

The following object is masked from 'package:purrr':

    discard

The following object is masked from 'package:readr':

    col_factor
nations <- read_csv("nations.csv") #Reads the file "Natinos" and sets it to the word nations so we can have a dataset for it
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.
nations <- nations |>
  mutate(gdp_trillions = (gdp_percap * population) / 1,000,000,000,000) #This takes the variables from the nations dataset and makes calculations so we can see the GDP in trillions
filtered_countries1 <- nations |>
  filter(country %in% c('Afghanistan', 'Belgium', 'Cambodia', 'Denmark')) #Choosing which countries I want to use for my graph

ggplot(filtered_countries1, aes(x = year, y = gdp_trillions, color = country)) +  #setting the x and y and setting the countries to be colored
  geom_point() + # set it for scatter plots
  geom_line() + # Makes it so I can sue line graphs
  scale_color_brewer(palette = "Set1") + # Setting the color set
  scale_y_continuous(labels = scales::label_number(suffix = "Trillion")) + # Adds Trillions at the end of the number (got some help from chatgpt, asking "how to add trillions on the y axis of my code.
  theme_minimal() + #Makes a simple white background with gray accents
  labs(title = "GDP Over Time for Selected Countries", #Setting the charts title
       x = "Year", # setting the title for the x axis
       y = "GDP in Trillions of Dollars") #Setting the title for the y axis
Warning: Removed 15 rows containing missing values (`geom_point()`).
Warning: Removed 15 rows containing missing values (`geom_line()`).

filtered_countries2 <- nations |>
  group_by(region, year) |>
  summarise(GDP = sum(gdp_trillions, na.rm = TRUE))# This groups all of the countries by region, it also groups the years together.
`summarise()` has grouped output by 'region'. You can override using the
`.groups` argument.
ggplot(filtered_countries2, aes(x = year, y = GDP, fill = region)) + # We are taking the grouped countries and making a plot with it, we are setting the x and y axis's and putting a fill on the region so the color will fill the region and make it look nice
  geom_area(color = "black", size = 0.5) + #Adds a thin black line around each group to easily tell whats what
  scale_fill_brewer(palette = "Set2") + # This sets the color set we are using
  theme_minimal() + #Makes a simple white background with gray accents
  labs(title = "GDP by Region Over Time", #Sets the title of the plot
       x = "Year", # Titles the x axis
       y = "Total GDP in Trillions of Dollars") #Titles the y axis
Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
ℹ Please use `linewidth` instead.