Week 6 Homework

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
library(ggplot2)

# Load the Nations dataset
nations <- read.csv("/Users/xutongzhang/Downloads/nations.csv")
nations <- nations %>%
  mutate(gdp_in_trillions = (gdp_percap * population) / 1e12)
filtered_nations <- nations %>%
  filter(country %in% c("China", "Germany", "Japan", "United States"))
ggplot(filtered_nations, aes(x = year, y = gdp_in_trillions, color = country, linetype = country)) +
  geom_point() +
  geom_line() +
  scale_color_brewer(palette = "Set1") +
  scale_linetype_manual(values = c("China" = "dashed", "Germany" = "solid", "Japan" = "solid", "United States" = "solid"))

grouped_nations <- nations %>%
  filter(year >= 1990 & year <= 2015) %>%
  group_by(region, year) %>%
  summarise(GDP = sum(gdp_in_trillions, na.rm = TRUE))
`summarise()` has grouped output by 'region'. You can override using the
`.groups` argument.
ggplot(grouped_nations, aes(x = year, y = GDP, fill = region)) +
  geom_area(alpha = 0.6, color = "white", size = 0.1) +
  scale_fill_brewer(palette = "Set2")
Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
ℹ Please use `linewidth` instead.