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(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5     v purrr   0.3.4
## v tibble  3.1.6     v stringr 1.4.0
## v tidyr   1.1.4     v forcats 0.5.1
## v readr   2.1.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(ggplot2)

Loading up the nations dataset

setwd("~/Data 110 Folder")
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
## 
## i Use `spec()` to retrieve the full column specification for this data.
## i Specify the column types or set `show_col_types = FALSE` to quiet this message.

View the data

view(nations)

Giving the GDP of each country in trillions of dollars, by multiplying gdp_percap by population and dividing by a trillion.

nations1 <- mutate(nations, countrygdp = gdp_percap*population/10^12)

Filter the data with dplyr for the four desired countries

nations_filter <- filter(nations1, country %in% c("Canada", "Egypt, Arab Rep.", "Brazil","China"))

Creating the first graph

nations_filter %>%
  
  ggplot(aes(year, countrygdp, color = country))+
  scale_color_brewer(palette = "Set1")+
  theme_bw()+
  geom_point()+
  geom_line()+
  ggtitle("China's Constant Rise")+
  xlab("Year")+
  ylab("GDP  ($ Trillion) ")

Grouping by region and year

nation_group <- nations1 %>%
  group_by(region, year)%>%
  summarise (GDP=sum(countrygdp,na.rm = TRUE))
## `summarise()` has grouped output by 'region'. You can override using the `.groups` argument.

Creating the second chart

int_nations<- nation_group %>%
  ggplot(aes(year, GDP, fill=region))+
  scale_fill_brewer(palette = "Set2")+
  geom_area(color="white")+
  ggtitle("GDP by World Bank Region")+
  xlab("Year")+
  ylab("GDP  ($ Trilion) ")

int_nations <- ggplotly(int_nations)

int_nations