Set the working directory to the location where your data file is
located
setwd("C:/Users/MCuser/Desktop/data")
Load the necessary libraries
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)
Load the dataset
nations <- read.csv("nations.csv")
Mutate the data to calculate GDP in trillions of dollars
nations <- nations %>%
mutate(GDP_trillions = gdp_percap * population / 1e12)
First chart: GDP over time for selected countries
selected_countries <- c("China", "United States", "India", "Japan")
chart1_data <- nations %>%
filter(country %in% selected_countries)
chart1 <- ggplot(chart1_data, aes(x = year, y = GDP_trillions, color = country)) +
geom_point() +
geom_line() +
scale_color_brewer(palette = "Set1") +
labs(title = "GDP Over Time for Selected Countries",
x = "Year",
y = "GDP (Trillions of Dollars)",
caption = "Source: Nations dataset")
Second chart: GDP by region over time
chart2_data <- nations %>%
group_by(region, year) %>%
summarise(GDP = sum(gdp_percap * population / 1e12, na.rm = TRUE))
## `summarise()` has grouped output by 'region'. You can override using the
## `.groups` argument.
chart2 <- ggplot(chart2_data, aes(x = year, y = GDP, fill = region)) +
geom_area(color = "white", size = 0.5) +
scale_fill_brewer(palette = "Set2") +
labs(title = "GDP by Region Over Time",
x = "Year",
y = "GDP (Trillions of Dollars)",
caption = "Source: Nations dataset")
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
Print both charts
print(chart1)

print(chart2)
