# Load necessary libraries
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.0 ✔ tibble 3.2.1
## ✔ lubridate 1.9.3 ✔ tidyr 1.3.1
## ✔ 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
# Load the dataset skipping the first four rows
my_data <- read_csv("GWPT.csv", skip = 4)
## New names:
## Rows: 102 Columns: 27
## ── Column specification
## ──────────────────────────────────────────────────────── Delimiter: "," chr
## (4): ...1, ...2, ...26, ...27 dbl (23): Global Energy Monitor, ...4, ...5,
## ...6, ...7, ...8, ...9, ...10, ...
## ℹ 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.
## • `` -> `...1`
## • `` -> `...2`
## • `` -> `...4`
## • `` -> `...5`
## • `` -> `...6`
## • `` -> `...7`
## • `` -> `...8`
## • `` -> `...9`
## • `` -> `...10`
## • `` -> `...11`
## • `` -> `...12`
## • `` -> `...13`
## • `` -> `...14`
## • `` -> `...15`
## • `` -> `...16`
## • `` -> `...17`
## • `` -> `...18`
## • `` -> `...19`
## • `` -> `...20`
## • `` -> `...21`
## • `` -> `...22`
## • `` -> `...23`
## • `` -> `...24`
## • `` -> `...25`
## • `` -> `...26`
## • `` -> `...27`
# Remove special characters from column names for easier handling
colnames(my_data) <- make.names(colnames(my_data))
# Rename columns to avoid conflicts
names(my_data)[2] <- "Country"
# Exclude non-numeric columns from pivot operation
my_data_long <- my_data %>%
pivot_longer(cols = -c(...1, Country, ...26, ...27), names_to = "Year", values_to = "Energy") %>%
mutate(Year = as.numeric(gsub("[^0-9]", "", Year))) # Convert Year column to numeric
# Remove rows with missing Year values
my_data_long <- my_data_long[!is.na(my_data_long$Year), ]
# Plotting with adjusted x-axis
ggplot(my_data_long, aes(x = Year, y = Energy, color = ...1)) +
geom_line() +
facet_wrap(~Country, scales = "free_y") +
labs(x = "Year", y = "Operational Capacity: MW", color = "Region/Country") +
theme_minimal()
