Use facet_grid to create small multiple plots for electricity usage by sector and month
# Load librarieslibrary(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.1 ✔ tibble 3.2.1
✔ lubridate 1.9.4 ✔ tidyr 1.3.1
✔ purrr 1.0.4
── 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(lubridate)# Read the CSV filesetwd("C:/Users/adity/Downloads")data <-read_csv("MER_T02_01A.csv")
Rows: 10515 Columns: 6
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (3): MSN, Description, Unit
dbl (3): YYYYMM, Value, Column_Order
ℹ 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.
# Clean and transform the datadata_clean <- data %>%mutate(Year =floor(YYYYMM /100),MonthNum = YYYYMM %%100 ) %>%filter(MonthNum >=1& MonthNum <=12) %>%# ✅ Filter out invalid monthsmutate(Date =ymd(paste0(Year, "-", MonthNum, "-01")),Month =month(Date, label =TRUE, abbr =TRUE),Sector =str_extract(Description, "(?<=by the ).*?(?= Sector)") %>%str_to_title() ) %>%filter(!is.na(Sector))# Plot with facet_gridggplot(data_clean, aes(x = Date, y = Value)) +geom_line(color ="steelblue", linewidth =0.5) +facet_grid(Sector ~ Month, scales ="free_y") +labs(title ="Electricity Usage by Sector and Month",x ="Date",y =paste0("Usage (", unique(data_clean$Unit), ")") ) +theme_minimal(base_size =11) +theme(axis.text.x =element_text(angle =45, hjust =1, size =6),strip.text =element_text(size =8),panel.spacing =unit(0.5, "lines") # ✅ Completed )