Nations Charts Assignments

Author

Hana Rose

` ## Load Libraries

library(dplyr)
Warning: package 'dplyr' was built under R version 4.4.3

Attaching package: 'dplyr'
The following objects are masked from 'package:stats':

    filter, lag
The following objects are masked from 'package:base':

    intersect, setdiff, setequal, union
library(ggplot2)
Warning: package 'ggplot2' was built under R version 4.4.3
library(gapminder)
Warning: package 'gapminder' was built under R version 4.4.3

Load Data

setwd("C:/Users/Hana Rose/OneDrive/Data 110")
nations <- read.csv("nations.csv")

Create a New Variable in the Data

nations <- nations %>%
  mutate(gdp_trillions = (gdp_percap * population) / 1e12)

Filter for 4 Countries

four_countries <- nations %>%
  filter(country %in% c("United States", "China", "Germany", "Japan"))

Filter for NAs

four_countries <- four_countries %>%
  filter(complete.cases(gdp_trillions))

First Chart

ggplot(four_countries, aes(x = year, y = gdp_trillions, color = country)) +
  geom_point() +
  geom_line() +
  scale_color_brewer(palette = "Set1") +
  labs(title = "GDP in Trillions of Dollars Over Time",
       x = "Year",
       y = "GDP in Trillions of Dollars",
       color = "Country")

Group by Region and Year and Summarize

regional_gdp <- nations %>%
  group_by(region, year) %>%
  summarise(GDP = sum(gdp_trillions, na.rm = TRUE), .groups = "drop")

Second Chart

ggplot(regional_gdp, aes(x = year, y = GDP, fill = region)) +
  geom_area(color = "white", linewidth = 0.1) +
  scale_fill_brewer(palette = "Set2") +
  labs(title = "Regional GDP in Trillions of Dollars Over Time",
       x = "Year",
       y = "GDP in Trillions of Dollars",
       fill = "Region")