project 6

# Load the required libraries
library(dplyr)
library(ggplot2)

# Load the nations dataset using the full file path
nations <- read.csv("C:/Users/amanu/OneDrive/Documentos/Assignment 5 data 110_files/assignment 6 part b_files/nations (1).csv")

# Create a new variable 'gdp' in trillions of dollars
# Formula: gdp_percap * population / 1,000,000,000,000
nations <- nations |>
  mutate(gdp = gdp_percap * population / 1e12)

Chart 1: GDP Over Time — Selected Countries

# Filter the data to only include these four countries
selected <- nations |>
  filter(country %in% c("China", "United States", "Japan", "Germany"))
  # Draw the line + point chart for the four selected countries
# color = country maps each country to a different color
ggplot(selected, aes(x = year, y = gdp, color = country)) +
  geom_line() +                                  # draws the lines
  geom_point() +                                 # draws the points on top
  scale_color_brewer(palette = "Set1") +         # applies the Set1 color palette
  labs(
    title = "GDP Over Time: Selected Countries",
    x = "Year",
    y = "GDP (trillions USD)",
    color = "Country"
  ) +
  theme_minimal()

Chart 2: GDP by Region Over Time

# Group the data by region and year, then sum GDP across all countries
# na.rm = TRUE ignores missing values so they don't break the sum
regional <- nations |>
  group_by(region, year) |>summarise(GDP = sum(gdp, na.rm = TRUE), .groups = "drop")

# Draw the stacked area chart
# fill = region colors each region's area differently
# color = "white" and linewidth = 0.15 add a thin white border between areas
ggplot(regional, aes(x = year, y = GDP, fill = region)) +
  geom_area(color = "white", linewidth = 0.15) + # stacked areas with thin white outline
  scale_fill_brewer(palette = "Set2") +          # applies the Set2 color palette
  labs(
    title = "GDP by Region Over Time",
    x = "Year",
    y = "GDP (trillions USD)",
    fill = "Region"
  ) +
  theme_minimal()


Quarto

Quarto enables you to weave together content and executable code into a finished document. To learn more about Quarto see https://quarto.org.

Running Code

When you click the Render button a document will be generated that includes both content and the output of embedded code. You can embed code like this:

1 + 1
[1] 2

You can add options to executable code like this

[1] 4

The echo: false option disables the printing of code (only output is displayed).