Nations Charts Assignments

Author

M. Tariq

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)
library(RColorBrewer)
library(readr)

Load in Data set

setwd("C:/Users/tmanh/OneDrive/Documents/college stuff/Data 110")
nations_data <- read_csv("nations.csv")
Rows: 5275 Columns: 10
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (5): iso2c, iso3c, country, region, income
dbl (5): year, gdp_percap, population, birth_rate, neonat_mortal_rate

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.

GDP in Trillions

nations_data <- nations_data %>%
  mutate(gdp = (gdp_percap * population) / 1e12)

Chart One

# filtering data
selected_countries <- c("United States", "China", "Germany", "Japan")
filtered_data <- nations_data %>%
  filter(country %in% selected_countries)
# creating the chart
ggplot(filtered_data, aes(x = year, y = gdp, color = country)) +
  geom_line() +
  geom_point(size = 2) +  
  scale_color_brewer(palette = "Set1") +
  labs(title = "GDP of Selected Countries Over Time",
       x = "Year",
       y = "GDP in Trillions of Dollars") +
  theme_minimal()

Chart Two

# Summarizing GDP
gdp_by_region <- nations_data %>%
  group_by(region, year) %>%
  summarise(GDP = sum(gdp, na.rm = TRUE))
`summarise()` has grouped output by 'region'. You can override using the
`.groups` argument.
ggplot(gdp_by_region, aes(x = year, y = GDP, fill = region)) +
  geom_area(color = "white", size = 0.1) +
  scale_fill_brewer(palette = "Set2") +
  labs(title = "Total GDP by Region Over Time",
       x = "Year",
       y = "Total GDP in Trillions of Dollars") +
  theme_minimal()
Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
ℹ Please use `linewidth` instead.