Install and load required packages and libraries.

library(RColorBrewer)
## Warning: package 'RColorBrewer' was built under R version 4.1.3
library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.1.3
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.6     v purrr   0.3.4
## v tibble  3.1.7     v dplyr   1.0.9
## v tidyr   1.2.0     v stringr 1.4.0
## v readr   2.1.2     v forcats 0.5.1
## Warning: package 'ggplot2' was built under R version 4.1.3
## Warning: package 'tibble' was built under R version 4.1.3
## Warning: package 'tidyr' was built under R version 4.1.3
## Warning: package 'readr' was built under R version 4.1.3
## Warning: package 'purrr' was built under R version 4.1.3
## Warning: package 'dplyr' was built under R version 4.1.3
## Warning: package 'stringr' was built under R version 4.1.3
## Warning: package 'forcats' was built under R version 4.1.3
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(plotly)
## Warning: package 'plotly' was built under R version 4.1.3
## 
## 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

Import the “Nations” dataset, and add a column showing GDP in trillions of dollars.

setwd("C:/Users/mayss/Desktop/Data Science/Datasets")
nations <- read_csv("nations.csv")%>%
 mutate(gdp_tn = gdp_percap*population/1000000000000) #add a column
## 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.

Consider four Gulf countries: Saudi Arabia, Qatar, Kuwait, and United Arab Emirates

# First, prepare the data using dplyr: 
Gulf <- nations %>%
 filter(iso3c == "QAT" | iso3c == "ARE" | iso3c == "SAU" | iso3c == "KWT") %>% 
  arrange(year)

Plot Using ggplot2

p1 <- ggplot(Gulf, aes(x= year, y=gdp_tn, group= country)) +
  geom_line(aes(color= country))+
  geom_point()+
scale_color_brewer(palette = "Set1")+
  xlab('Year')+
  ylab('GDP ($ Trillion)')+
  ggtitle("The Gulf Countries\n")
p1
## Warning: Removed 15 row(s) containing missing values (geom_path).
## Warning: Removed 15 rows containing missing values (geom_point).

Make it interactive using Plotly

library(plotly)
ggplotly(p1)
Looking at Saudi Arabia, one can see how the 2004 (Iraq war) and 2010 (the beginning of Arab Spring) led to high GDP due to oil sales! The rich get richer, and the poor get poorer!

Plot 2

Prepare the dataset

# prepare data
regions <- nations %>%
 group_by(year,region) %>%
 summarise(GDP = sum(gdp_tn, na.rm = TRUE)) %>% #use na.rm=True to deal with NA values
 arrange(year,region)
## `summarise()` has grouped output by 'year'. You can override using the
## `.groups` argument.

Plot the dataset

p2 <- ggplot(regions, aes(x=year, y = GDP,group = region, fill = region))+
  geom_area(color='white')+ #creates a thin white line between the regions
  scale_fill_brewer(palette = "Set2")+
  ggtitle('GDP by World Bank Regions\n')+
  ylab('GDP in $ Trillion')+
  xlab('Year')
p2

Make the ’Regions’plot interactive!

p3 <-ggplotly(data = regions,
         x = year,
         y = GDP, 
         type = 'area',
         hoverinfo= 'text',
         text= paste('Region: ',regions$region,
                     '<br>', 
                     "GDP: ",regions$GDP))
p3

For some reason, the interactive property only works when you are hovering on the white line, not on the region! Moreover, the region is showing twice!