HW Week 6

library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.2     ✔ readr     2.1.4
✔ forcats   1.0.0     ✔ stringr   1.5.0
✔ ggplot2   3.4.3     ✔ tibble    3.2.1
✔ lubridate 1.9.2     ✔ tidyr     1.3.0
✔ purrr     1.0.2     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
setwd("/Users/blossomanyanwu/Documents/Data 110 (Fall 2023)")
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.
head(nations)
# A tibble: 6 × 10
  iso2c iso3c country  year gdp_percap population birth_rate neonat_mortal_rate
  <chr> <chr> <chr>   <dbl>      <dbl>      <dbl>      <dbl>              <dbl>
1 AD    AND   Andorra  1996         NA      64291       10.9                2.8
2 AD    AND   Andorra  1994         NA      62707       10.9                3.2
3 AD    AND   Andorra  2003         NA      74783       10.3                2  
4 AD    AND   Andorra  1990         NA      54511       11.9                4.3
5 AD    AND   Andorra  2009         NA      85474        9.9                1.7
6 AD    AND   Andorra  2011         NA      82326       NA                  1.6
# ℹ 2 more variables: region <chr>, income <chr>

Present the GDP of each country in trillions of dollars

view(nations)
nations_mutate<- mutate(nations, gdp_trillion = (gdp_percap*population)/(10^12))

Select 4 Countries in West Africa

I decided to select Nigeria, Ghana, Cameroon, and Mali
west_africa_4<-
  filter(nations_mutate, country %in% c("Nigeria", "Mali", "Cameroon", "Ghana"))
west_africa_4
# A tibble: 100 × 11
   iso2c iso3c country  year gdp_percap population birth_rate neonat_mortal_rate
   <chr> <chr> <chr>   <dbl>      <dbl>      <dbl>      <dbl>              <dbl>
 1 CM    CMR   Camero…  2000      1905.   15927713       41.2               34.7
 2 CM    CMR   Camero…  1997      1728.   14709961       41.8               38.6
 3 CM    CMR   Camero…  2005      2256.   18126999       40.4               30.7
 4 CM    CMR   Camero…  1993      1523.   13169100       43.3               40.2
 5 CM    CMR   Camero…  2003      2111.   17218591       40.8               31.7
 6 CM    CMR   Camero…  2012      2715.   21659488       37.7               26.9
 7 CM    CMR   Camero…  1990      1789.   12070359       44.7               40.7
 8 CM    CMR   Camero…  2002      2042.   16779434       40.9               32.5
 9 CM    CMR   Camero…  1992      1663.   12796739       43.8               40.4
10 CM    CMR   Camero…  1994      1543.   13546823       42.9               40  
# ℹ 90 more rows
# ℹ 3 more variables: region <chr>, income <chr>, gdp_trillion <dbl>
view(west_africa_4)

Visualization of 4 West African Countries

ggplot(west_africa_4, aes(x=year, y=gdp_trillion, group=country, color=country)) +
  geom_point()+
  geom_line()+
  scale_color_brewer(palette="Set1")+
  labs(x="Year", y="GDP (in trillions)", title = "Nigeria is on top of the West African Economic Landscape", color ="")+
    theme_minimal()

Organizing GDP by Region

gdp_region<-nations_mutate %>%
  group_by(region, year) %>%
  summarise(GDP = sum(gdp_trillion, na.rm = TRUE))
`summarise()` has grouped output by 'region'. You can override using the
`.groups` argument.
view(gdp_region)
ggplot(gdp_region, aes(x= year, y=GDP, group=region, fill=region))+
  geom_area(color="white")+
  scale_fill_brewer(palette="Set2")+
  labs(x="Year", y="GDP (in trillions)", title = "GDP by Region")+
  theme_minimal()