Assignment 6 - Statistical Charts

Author

G Oliveira

Use dplyr and ggplot2 to process data and draw these two charts (shown below) from the Nations dataset. You do NOT need to incorporate interactivity, but you can, if you want to challenge yourself.

Load the library

library(dplyr)
library(ggplot2)
library(tidyverse)

Load the data

data <- read_csv("/Users/giovanarodrigues/Desktop/DATA 110/dataset/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.
glimpse(data)
Rows: 5,275
Columns: 10
$ iso2c              <chr> "AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD…
$ iso3c              <chr> "AND", "AND", "AND", "AND", "AND", "AND", "AND", "A…
$ country            <chr> "Andorra", "Andorra", "Andorra", "Andorra", "Andorr…
$ year               <dbl> 1996, 1994, 2003, 1990, 2009, 2011, 2004, 2010, 200…
$ gdp_percap         <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,…
$ population         <dbl> 64291, 62707, 74783, 54511, 85474, 82326, 78337, 84…
$ birth_rate         <dbl> 10.900, 10.900, 10.300, 11.900, 9.900, NA, 10.900, …
$ neonat_mortal_rate <dbl> 2.8, 3.2, 2.0, 4.3, 1.7, 1.6, 2.0, 1.7, 2.1, 2.1, 2…
$ region             <chr> "Europe & Central Asia", "Europe & Central Asia", "…
$ income             <chr> "High income", "High income", "High income", "High …

Process the data

colSums(is.na(data))
             iso2c              iso3c            country               year 
                25                  0                  0                  0 
        gdp_percap         population         birth_rate neonat_mortal_rate 
               766                 14                295                525 
            region             income 
                 0                  0 
country <- data |>
  filter(!is.na(gdp_percap), !is.na(population)) |>
  mutate(gdp = gdp_percap * population/10^12)
four_countries <- country |>
  filter(country %in% c("China", "Brazil", "Japan", "United States"))

ggplot(four_countries, aes(x = year, y = gdp, color = country)) +
  geom_point() +
  geom_line() +
  scale_color_brewer(palette = "Set1") +
  labs(title = "GDP of four countries, 1990-2014",
       x = "Year",
       y = "GDP ($ trillion)",
       color = "Country") +
  theme_minimal()

gdp_region <- country |>
  group_by(region, year) |>
  summarise(GDP = sum(gdp, na.rm = TRUE))
`summarise()` has regrouped the output.
ℹ Summaries were computed grouped by region and year.
ℹ Output is grouped by region.
ℹ Use `summarise(.groups = "drop_last")` to silence this message.
ℹ Use `summarise(.by = c(region, year))` for per-operation grouping
  (`?dplyr::dplyr_by`) instead.
ggplot(gdp_region, aes(x = year, y = GDP, fill = region)) +
  geom_area(color = "white", linewidth = 0.2) +
  scale_fill_brewer(palette = "Set2") +
  labs(title = "GDP by region, 1990-2014",
       x = "Year",
       y = "GDP ($ trillion)",
       fill = "Region") +
  theme_minimal()