Assignment 6 | Statistical Charts

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)

nations <- read.csv("nations.csv")
nations <- mutate(nations, gdp = gdp_percap * population / 1000000000000)
countries <- filter(nations, country %in% c("United States", "China", "India", "Japan"))

ggplot(countries, aes(x = year, y = gdp, color = country)) +
  geom_point() +
  geom_line() +
  scale_color_brewer(palette = "Set1") +
  labs(
    title = "GDP by Country",
    x = "Year",
    y = "GDP in Trillions",
    color = "Country"
  ) +
  theme_minimal()

regions <- group_by(nations, region, year)

regions <- summarise(regions, GDP = sum(gdp, na.rm = TRUE))
`summarise()` has regrouped the output.
ℹ Summaries were computed grouped by region and year.
ℹ Output is grouped by region.
ℹ Use `summarise(.groups = "drop_last")` to silence this message.
ℹ Use `summarise(.by = c(region, year))` for per-operation grouping
  (`?dplyr::dplyr_by`) instead.
ggplot(regions, aes(x = year, y = GDP, fill = region)) +
  geom_area(color = "white", linewidth = 0.1) +
  scale_fill_brewer(palette = "Set2") +
  labs(
    title = "GDP by Region",
    x = "Year",
    y = "GDP in Trillions",
    fill = "Region"
  ) +
  theme_minimal()