── 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 graphggplot(filtered_countries1, aes(x = year, y = gdp_trillions, color = country)) +#setting the x and y and setting the countries to be coloredgeom_point() +# set it for scatter plotsgeom_line() +# Makes it so I can sue line graphsscale_color_brewer(palette ="Set1") +# Setting the color setscale_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 accentslabs(title ="GDP Over Time for Selected Countries", #Setting the charts titlex ="Year", # setting the title for the x axisy ="GDP in Trillions of Dollars") #Setting the title for the y axis
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 nicegeom_area(color ="black", size =0.5) +#Adds a thin black line around each group to easily tell whats whatscale_fill_brewer(palette ="Set2") +# This sets the color set we are usingtheme_minimal() +#Makes a simple white background with gray accentslabs(title ="GDP by Region Over Time", #Sets the title of the plotx ="Year", # Titles the x axisy ="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.