Natons 2 plots

Author

AT

library(dplyr)

Attaching package: 'dplyr'
The following objects are masked from 'package:stats':

    filter, lag
The following objects are masked from 'package:base':

    intersect, setdiff, setequal, union
library(ggplot2)
Warning: package 'ggplot2' was built under R version 4.3.3
library(readr)
library(RColorBrewer)
# Loading dataset
nations <- read_csv("nations (2).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.
# Create the new GDP variable in trillions of dollars
nations <- nations %>%
  mutate(gdp_trillions = (gdp_percap * population) / 1e12)
# First chart
desired_countries <- c("Argentina", "Brazil", "Canada", "Switzerland")
nations_filtered <- nations %>%
  filter(country %in% desired_countries)

# geom_point and geom_line
ggplot(nations_filtered, aes(x = year, y = gdp_trillions, color = country)) +
  geom_point() +
  geom_line() +
  scale_color_brewer(palette = "Set1") +
  theme_minimal() +
  labs(title = "GDP in Trillions of Dollars by Year for Selected Countries",
       x = "Year",
       y = "GDP (Trillions of Dollars)",
       color = "Country")
Warning: Removed 25 rows containing missing values or values outside the scale range
(`geom_point()`).
Warning: Removed 25 rows containing missing values or values outside the scale range
(`geom_line()`).

# Second chart
nations_summarized <- nations %>%
  group_by(region, year) %>%
  summarise(GDP = sum(gdp_trillions, na.rm = TRUE))
`summarise()` has grouped output by 'region'. You can override using the
`.groups` argument.
# geom_area
ggplot(nations_summarized, aes(x = year, y = GDP, fill = region)) +
  geom_area(color = "white", size = 0.1) +
  scale_fill_brewer(palette = "Set2") +
  theme_minimal() +
  labs(title = "Total GDP in Trillions of Dollars by Region and Year",
       x = "Year",
       y = "Total GDP (Trillions of Dollars)",
       fill = "Region")
Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
ℹ Please use `linewidth` instead.