#GRAPH2 ## Load the libraries

# install.packages("tidyverse")

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

#Load and process nations data Load the nations data, and add a column showing GDP in trillions of dollars.

nations <- read_csv("nations.csv") %>%
  mutate(gdp_tn = gdp_percap*population/10)
## Parsed with column specification:
## cols(
##   iso2c = col_character(),
##   iso3c = col_character(),
##   country = col_character(),
##   year = col_double(),
##   gdp_percap = col_double(),
##   population = col_double(),
##   birth_rate = col_double(),
##   neonat_mortal_rate = col_double(),
##   region = col_character(),
##   income = col_character()
## )

First, prepare the data using dplyr:

getwd()
## [1] "C:/Users/Salifou Sylla/Desktop/DATA110"
data2 <- nations %>%
  group_by(year,region) %>%
  summarize(gdp_tn = sum(gdp_tn, na.rm = TRUE)) %>%
  arrange(year,region)
## `summarise()` regrouping output by 'year' (override with `.groups` argument)

The arrange step is important, the data has to be in order when drawing a time series - otherwise any line drawn through the data will follow the path of the data order, not the correct time order.

Now draw a basic chart with default settings:

plot2 <- ggplot(data2, aes(x = year, y = gdp_tn, fill = region)) +
  xlab("YEAR") +
  ylab("GDP ($ trillion)") +
  ggtitle("GDP COMPARISON BY REGIONS") +
  geom_area(color="white") +
  
  scale_fill_brewer(palette="Set2")
plot2