Loading in the packages and data set
nations <- read.csv("nations.csv")
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## ✔ ggplot2 3.4.0 ✔ purrr 1.0.1
## ✔ tibble 3.1.8 ✔ dplyr 1.1.0
## ✔ tidyr 1.3.0 ✔ stringr 1.5.0
## ✔ readr 2.1.3 ✔ forcats 1.0.0
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
library(RColorBrewer)
Use mutate to create the GDP in trillions of dollars variable
nations <- nations %>%
mutate(gdp_trillions = gdp_percap * population / 10^12)
Plot 1
p1 <- nations %>%
filter(country %in% c("China", "Japan", "India", "Indonesia")) %>%
ggplot(aes(x = year, y = gdp_trillions, color = country)) +
geom_point() +
geom_line() +
labs(title = "GDP of Top Economies of Asia", x = "Year", y = "GDP (trillions of dollars)")
scale_color_brewer(palette = "Set1")
## <ggproto object: Class ScaleDiscrete, Scale, gg>
## aesthetics: colour
## axis_order: function
## break_info: function
## break_positions: function
## breaks: waiver
## call: call
## clone: function
## dimension: function
## drop: TRUE
## expand: waiver
## get_breaks: function
## get_breaks_minor: function
## get_labels: function
## get_limits: function
## guide: legend
## is_discrete: function
## is_empty: function
## labels: waiver
## limits: NULL
## make_sec_title: function
## make_title: function
## map: function
## map_df: function
## n.breaks.cache: NULL
## na.translate: TRUE
## na.value: NA
## name: waiver
## palette: function
## palette.cache: NULL
## position: left
## range: <ggproto object: Class RangeDiscrete, Range, gg>
## range: NULL
## reset: function
## train: function
## super: <ggproto object: Class RangeDiscrete, Range, gg>
## rescale: function
## reset: function
## scale_name: brewer
## train: function
## train_df: function
## transform: function
## transform_df: function
## super: <ggproto object: Class ScaleDiscrete, Scale, gg>
p1

Plot 2
p2 <- nations %>%
group_by(region, year) %>%
summarise(GDP = sum(gdp_trillions, na.rm = TRUE)) %>%
ggplot(aes(x = year, y = GDP, fill = region)) +
geom_area(color = "white", linewidth = 0.1) +
scale_fill_brewer(palette = "Set2") +
labs(title = "GDP by Region", x = "Year", y = "GDP (trillions of dollars)" )
## `summarise()` has grouped output by 'region'. You can override using the
## `.groups` argument.
p2
