Nations Charts Assignment

Author

Ask Moystad

DATA 110: Nations Charts Assignment

Both graphs have been rendered as close to the originals as possible, interactive graphs differ somewhat but have been added as practice.

Packages:

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.4.4     ✔ 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("ggplot2")
library("RColorBrewer")
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
getwd()
[1] "/Users/gimle/Desktop/Data 110/Datasets 110"
setwd("/Users/gimle/Desktop/Data 110/Datasets 110")
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.
nations_gpdpop <- nations |> mutate(gdp = (gdp_percap * population)/10^12)

Graph 1:

top_4 <- nations_gpdpop |>
  filter(country %in% c("China", "Germany", "Japan", "United States")) |> 
  select(country, gdp, year, neonat_mortal_rate)
plot_top_4 <- top_4 |>
  ggplot(aes( x = year, y = gdp, color = country)) +
  geom_line() + 
  geom_point() +
  theme_bw() +
  scale_color_brewer(palette = "Set1") +
  labs(title = "China's Rise to Become the Largest Economy",
       y = "GDP ($ trillion)", 
       x = "year",
       color = NULL) +
  theme(panel.border = element_blank())
plot_top_4

plotly_top_4 <- ggplotly(plot_top_4)
plotly_top_4

Graph 2:

regions_gdp <- nations_gpdpop |> 
  group_by(region, year) |> 
  summarise(sum_GDP = sum(gdp, na.rm = TRUE)) 
`summarise()` has grouped output by 'region'. You can override using the
`.groups` argument.
plot_regions_gdp <- regions_gdp |> 
  ggplot(aes( x = year , y = sum_GDP, fill = region), ) + 
  geom_area(color = "white") + 
  theme_bw() + 
  scale_fill_brewer(palette = "Set2") + 
  labs(title = "GDP by World Bank Region", x = "year", y = "GDP ($ trillion)" ) +
  theme(panel.border = element_blank())
plot_regions_gdp

plotly_regions_gdp <- ggplotly(plot_regions_gdp)
plotly_regions_gdp