Nations Charts

Author

Chibogwu Onyeabo

Chart #1

#load in data
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.4     ✔ tidyr     1.3.1
✔ purrr     1.0.4     
── 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
nations <- read.csv("nations.csv")
#sorting
plot1 <- nations |>
  mutate(gdp = (gdp_percap * population)/10^12) |>
  filter(country == c("Japan", "China", "United States", "Germany")) |>
  group_by(country)
Warning: There was 1 warning in `filter()`.
ℹ In argument: `country == c("Japan", "China", "United States", "Germany")`.
Caused by warning in `country == c("Japan", "China", "United States", "Germany")`:
! longer object length is not a multiple of shorter object length
plot1
# A tibble: 25 × 11
# Groups:   country [4]
   iso2c iso3c country  year gdp_percap population birth_rate neonat_mortal_rate
   <chr> <chr> <chr>   <int>      <dbl>      <dbl>      <dbl>              <dbl>
 1 CN    CHN   China    2000      2915. 1262645000       14.0               21.2
 2 CN    CHN   China    2014     13255. 1364270000       12.4                5.9
 3 CN    CHN   China    2009      8290. 1331260000       12.1                9.1
 4 CN    CHN   China    1997      2265. 1230075000       16.6               24.4
 5 CN    CHN   China    2002      3527. 1280400000       12.9               18.6
 6 CN    CHN   China    1995      1860. 1204855000       17.1               26.9
 7 DE    DEU   Germany  1990     19033.   79433029       11.4                3.4
 8 DE    DEU   Germany  2003     29362.   82534176        8.6                2.7
 9 DE    DEU   Germany  2007     36778.   82266372        8.3                2.5
10 DE    DEU   Germany  1998     24871.   82047195        9.6                2.9
# ℹ 15 more rows
# ℹ 3 more variables: region <chr>, income <chr>, gdp <dbl>
#graph
ggplot(plot1, aes(x = year, y = gdp, group = country, color = country)) +
  geom_point() +
  geom_line() +
  scale_color_brewer(palette = "Set1")

Chart #2

#recreating gdp variable
plot2 <- nations |>
  mutate(gdp = (gdp_percap * population)/10^12)

#grouping
plot2.1 <- plot2 |>
  group_by(region, year) |>
  summarise(GDP = sum(gdp, na.rm = TRUE))
`summarise()` has grouped output by 'region'. You can override using the
`.groups` argument.
#graph
ggplot(plot2.1, aes(x = year, y = GDP, group = region, fill = region)) +
  geom_area(color = "white") +
  scale_fill_brewer(palette = "Set2")