Nations Dataset Assignment

Author

Adi Ve

Load Libraries

suppressWarnings(library(tidyverse))
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.2     ✔ readr     2.1.4
✔ forcats   1.0.0     ✔ stringr   1.5.0
✔ ggplot2   3.4.3     ✔ tibble    3.2.1
✔ lubridate 1.9.2     ✔ tidyr     1.3.0
✔ 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(ggfortify)
Warning: package 'ggfortify' was built under R version 4.2.3
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.

Plot 1

# add a variable GDP computed by multiplying the gdp per capita by the
# population, and normalize to trillions by dividing by 10 to the 12th
nations <- mutate(nations, GDP = gdp_percap*population*10^(-12))

# make a new dataset with just the data from the four countries of interest
nationsP1 <- filter(nations, country == "China" | country == "United States" |
                      country == "Japan" | country == "Germany")
P1 <- ggplot(nationsP1, aes(x=year, y=GDP, group=country, color=country)) +
  # make sure to include group and color with country in the aesthetics above
  # to make the lines separately graphed and colored for each country
  geom_line() +
  # add the lines
  geom_point() +
  # add the points
  scale_color_brewer(palette = "Set1") +
  # change the palette
  labs(x="year",y="GDP ($ trillion)" ) +
  # add labels for the axes
  labs(title = "China's Rise to Become the Largest Economy") +
  # add a title
  theme_classic() +
  # change the theme up for different visuals
  theme(text = element_text(family = "serif")) +
  # change the font to Times New Roman, you can check what the fonts available
  # are initialized as by typing windowsFonts() in the console
  theme(plot.title = element_text(hjust = 0.5))
  # center justify the title

P1 # call the plot

Plot 2

nationsP2 <- nations |>
  group_by(region, year) |>
  summarise(GDP = sum(GDP, na.rm = TRUE))
`summarise()` has grouped output by 'region'. You can override using the
`.groups` argument.
P2 <- ggplot(nationsP2, aes(x=year, y=GDP, group=region, fill=region)) +
  # again, make sure to include aesthetics group and fill with region
  geom_area(color="white") +
  # make an area graph with white borders between regions
  scale_fill_brewer(palette = "Set2") +
  # change the palette to Set2
  labs(x="year",y="GDP ($ trillion)" ) +
  # add axis labels
  labs(title = "GDP by World Bank Region") +
  # add a title
  theme_bw() +
  # change up the theme for different visuals
  theme(text = element_text(family = "mono")) +
  # change the font to Courier New, as a note, this command must come
  # AFTER the theme_bw call, or any theme-style call, because that will
  # override the font settings
  theme(plot.title = element_text(hjust = 0.5))
  # center justify the title

P2 # call the plot

Comments and References

I varied the fonts using information on this stackoverflow thread:
https://stackoverflow.com/questions/34522732/changing-fonts-in-ggplot2

I found this article helpful in plotting the time series:
https://www.geeksforgeeks.org/plotting-multiple-time-series-on-the-same-plot-using-ggplot-in-r/

I found this article helpful in plotting the area graph:
https://community.rstudio.com/t/is-there-a-way-to-order-colors-in-the-ggplot-area-map/160915

And I found this post helpful in center justifying the title:
https://stackoverflow.com/questions/40675778/center-plot-title-in-ggplot2