W6 Assignment

Author

M Loukinov

library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.5.1     ✔ tibble    3.2.1
✔ lubridate 1.9.3     ✔ tidyr     1.3.1
✔ purrr     1.0.2     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(zoo)

Attaching package: 'zoo'

The following objects are masked from 'package:base':

    as.Date, as.Date.numeric
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
setwd("C:/Users/Thecr/Downloads")
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.
nations1 <- nations |>
mutate(gdp = (gdp_percap * population) / 1e12 )
countries = c("China", "Germany", "Japan", "United States") #Used ChatGPT and google for help with syntax
nations2 <- nations1 |>
  filter(country %in% countries)
p1 <- nations2 |>
  ggplot(aes(x=year, y = gdp, color = country)) +
  geom_point() +
  geom_line() +
  labs(x = "Year", y = "GDP ($trillion)" , title = "China's Rise to Become the Largest Economy", caption = "From World Bank Data") +
  theme_bw()+
  scale_color_brewer(palette = "Set1")
  
ggplotly(p1)
nationsg <- nations1 |>
group_by(region, year)
nationsg <- nationsg |>
summarise(gdp = sum(gdp, na.rm = TRUE))
`summarise()` has grouped output by 'region'. You can override using the
`.groups` argument.
str(nationsg)
gropd_df [175 × 3] (S3: grouped_df/tbl_df/tbl/data.frame)
 $ region: chr [1:175] "East Asia & Pacific" "East Asia & Pacific" "East Asia & Pacific" "East Asia & Pacific" ...
 $ year  : num [1:175] 1990 1991 1992 1993 1994 ...
 $ gdp   : num [1:175] 5.52 6.03 6.5 7.04 7.64 ...
 - attr(*, "groups")= tibble [7 × 2] (S3: tbl_df/tbl/data.frame)
  ..$ region: chr [1:7] "East Asia & Pacific" "Europe & Central Asia" "Latin America & Caribbean" "Middle East & North Africa" ...
  ..$ .rows : list<int> [1:7] 
  .. ..$ : int [1:25] 1 2 3 4 5 6 7 8 9 10 ...
  .. ..$ : int [1:25] 26 27 28 29 30 31 32 33 34 35 ...
  .. ..$ : int [1:25] 51 52 53 54 55 56 57 58 59 60 ...
  .. ..$ : int [1:25] 76 77 78 79 80 81 82 83 84 85 ...
  .. ..$ : int [1:25] 101 102 103 104 105 106 107 108 109 110 ...
  .. ..$ : int [1:25] 126 127 128 129 130 131 132 133 134 135 ...
  .. ..$ : int [1:25] 151 152 153 154 155 156 157 158 159 160 ...
  .. ..@ ptype: int(0) 
  ..- attr(*, ".drop")= logi TRUE
p2 <- nationsg |>
  ggplot(aes(x = year, y = gdp, fill = region))+
  geom_area(position = "stack", color = "white", linewidth = 0.2, alpha = 0.8)+ #Added stack via ChatGPT to assist with year displays on interactive (failed)
  scale_fill_brewer(palette = "Set2")+
  labs(x = "Year", y = "GDP ($Trillions)", title = "GDP By World Bank Region", caption = "data by World Bank Data")+
  theme_bw() 
  
p2