load libraries

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.4     v stringr 1.4.0
## v tidyr   1.1.3     v forcats 0.5.1
## v readr   2.0.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(scales)
## 
## Attaching package: 'scales'
## The following object is masked from 'package:purrr':
## 
##     discard
## The following object is masked from 'package:readr':
## 
##     col_factor
library(ggplot2)

Read dataset into Rstudio

setwd("~/Data110")
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.

Visualize first 10 rows

head(nations)
## # A tibble: 6 x 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
## # ... with 2 more variables: region <chr>, income <chr>

Create new object and add new column

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

Create another object while adding filters

nationsfilter <- filter(nations1, country %in% c("Haiti", "United Arab Emirates", "Brazil","Switzerland"))

Draw first interactive chart

intnat <- nationsfilter %>%
  
  ggplot(aes(year, gdptrillions, colour=country, text = paste("Region:", region)))+
  scale_color_brewer(palette = "Set1")+
  #theme(legend.title = none)+
  scale_color_discrete(name =  " ")+
  theme_bw()+
  geom_point()+
  geom_line()+
  ggtitle("Brazil Economy on the Rise")+
  xlab("Year")+
  ylab("GDP  ($ Trilion) ")
## Scale for 'colour' is already present. Adding another scale for 'colour',
## which will replace the existing scale.
intnat <- ggplotly(intnat)

intnat

Create another object (Group by Region, Year and summarize by GDP)

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

Create second interactive chart

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

natgroupint <- ggplotly(natgroupint)

natgroupint