Nations Charts Assignment

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(ggplot2)
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(readr)
library(viridis)
Loading required package: viridisLite
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.

Creating a new variable

nations <- nations %>%
  mutate(gdp_trillion = gdp_percap * population / 10^12)

Filtering data

(changed the original countries to african ones)

desired_countries <- c("Cameroon", "Nigeria", "Ghana", "Senegal")
filtered_data <- nations %>%
  filter(country %in% desired_countries)

Creating ggplot for first graph

gg1 <- ggplot(filtered_data, aes(x = year, y = gdp_trillion, color = country)) +
  geom_point() +
  geom_line() +
  scale_color_brewer(palette = "RdPu") +
  labs(x = "Year", y = "GDP ($ trillions)", title = "GDP Trends Across African Countries Over Time") +
  theme_minimal()

Converting ggplot to plotly

ggplotly(gg1)

Creating the second graph

(Calculating GDP by region)
region_gdp <- nations %>%
  group_by(region, year) %>%
  summarise(sum_GDP = sum(gdp_trillion, na.rm = TRUE), .groups = "drop")

Creating ggplot for second graph

gg2 <- ggplot(region_gdp, aes(x = year, y = sum_GDP, fill = region)) +
  geom_area(color = "white", size = 0.2) +
  scale_fill_viridis(discrete = TRUE, option = "magma") +  
  labs(x = "Year", y = "GDP ($ trillion)", title = "GDP by World Bank Region") +
  theme_minimal()
Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
ℹ Please use `linewidth` instead.

Plotting the second graph

plot(gg2)