Nation Charts

Author

Renato Chavez

Published

March 5, 2023

library(tidyverse)
── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
✔ ggplot2 3.4.0     ✔ purrr   1.0.1
✔ tibble  3.1.8     ✔ dplyr   1.1.0
✔ tidyr   1.3.0     ✔ stringr 1.5.0
✔ readr   2.1.4     ✔ forcats 1.0.0
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()

We will be using the nations dataset for both graphs

setwd("/Users/renatochavez/Documents/Montgomery College/Spring 2023/DATA110/Datasets")
nations <- 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.

First graph

Create GDP by using mutate and the given formula

gdp <- nations 
gdp1 <- mutate(gdp, GDP = (gdp_percap * population) / 10^12)
gdp2 <- filter(gdp1, country == "Colombia" | country == "Peru" | country == "Chile" | country == "Uruguay")
ggplot(gdp2, aes(x = year, y = GDP, color = country)) +
  ggtitle("South American countries with their GDP in trillion $") +
  xlab("year") + 
  ylab("GDP ($ trillion)") + 
  theme_minimal(base_size = 12) +
  geom_point() + 
  geom_line() + 
  scale_color_brewer(palette = 'Set1')

Second Chart

For the second graph we will use information from the first one. Also, we will be implementing group_by and summarise.

gdp3 <- gdp1 %>%
  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(gdp3, aes(year, GDP)) + 
  xlab("year") + 
  ylab("GDP ($ trillion)") + 
  theme_minimal(base_size = 12) + 
  scale_fill_brewer(palette = 'Set2') + 
  ggtitle("GDP by World Bank Region") + 
  geom_area(color = "white", aes(fill = region))