Data 110 HW5

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.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
setwd("~/Data 110 101")
nations <- read.csv("nations.csv")

Plot 1:

nations2 <- nations |>
  mutate(tri_gdp = (gdp_percap*population)/1000000000000) |>
  filter(country == "China" | country == "Germany" | country == "Japan" | country == "United States")
p1 <- ggplot(nations2, aes(year, tri_gdp, color = country)) +
  geom_point() +
  geom_line() + 
  scale_color_brewer(palette = "Set1") +
  theme_minimal() +
  labs(
    title = "China's Rise to Become the Largest Economy",
    color = "Country"
  ) +
  scale_x_continuous(name = "Year") +
  scale_y_continuous(name = "GDP (Trillions of $)"); p1

Interactive Plot 1:

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

Attaching package: 'plotly'
The following object is masked from 'package:ggplot2':

    last_plot
The following object is masked from 'package:stats':

    filter
The following object is masked from 'package:graphics':

    layout
ggplotly(p1)

Plot 2:

nations3 <- nations |>
  mutate(tri_gdp = (gdp_percap*population)/1000000000000) |>
  group_by(region, year) |>
  summarise(gdp_sum = sum(tri_gdp, na.rm = TRUE))
`summarise()` has grouped output by 'region'. You can override using the
`.groups` argument.
p2 <- ggplot(nations3, aes(year, gdp_sum, fill = region)) +
  geom_area(color = "white", linewidth = 0.1) +
  scale_fill_brewer(palette = "Set2") +
  theme_minimal() +
  labs(
    title = "GDP by World Bank Region",
    fill = "Region"
  ) +
  scale_x_continuous(name = "Year") +
  scale_y_continuous(name = "GDP ($ Trillion)"); p2

Interactive Plot 2:

ggplotly(p2)