Aaron Data 110 HW week 6

#Load Libraries

library(ggplot2)
library(dplyr)

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

#Load Dataset

setwd("C:/Users/Truly/OneDrive/Documents/Data work")
nations <- read.csv("nations.csv")  

#Data Preparation

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

#Filter Data and Plot

filtered_nations <- nations %>%
  filter(country %in% c("Japan", "Canada", "Nigeria", "Philippines"))  

ggplot(filtered_nations, aes(x = year, y = gdp_trillions, color = country)) +
  geom_point() +
  geom_line() +
  scale_color_brewer(palette = "Set1") +
  labs(title = "GDP in Trillions over Time", x = "Year", y = "GDP (Trillions)") +
  theme_minimal()

#Group and Summarize Data

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

#Create Area Plot

ggplot(region_gdp, aes(x = year, y = GDP, fill = region)) +
  geom_area(color = "white", size = 0.1) +
  scale_fill_brewer(palette = "Set2") +
  labs(title = "GDP by Region Over Time", x = "Year", y = "GDP (Trillions)") +
  theme_minimal()
Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
ℹ Please use `linewidth` instead.

#For interactivity

library(plotly)

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(
  ggplot(region_gdp, aes(x = year, y = GDP, fill = region)) +
    geom_area(color = "white", size = 0.1) +
    scale_fill_brewer(palette = "Set2") +
    labs(title = "GDP by Region Over Time", x = "Year", y = "GDP (Trillions)") +
    theme_minimal()
)