# Load required libraries library(readxl) library(dplyr) library(ggplot2) library(scales) library(tidyr) # Step 1: Load the cleaned temperature data df <- read_excel("D:/thesis/cleaned_temperature_data.xlsx") # Step 2: Convert 'Min_Temperature' to numeric df <- df %>% mutate(Min_Temperature = as.numeric(Min_Temperature)) # Step 3: Reshape the data to long format df_long <- df %>% pivot_longer(cols = c(Max_Temperature, Min_Temperature), names_to = "Type", values_to = "Temperature") # Step 4: Ensure Month is an ordered factor for proper sequencing df_long$Month <- factor(df_long$Month, levels = c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"), ordered = TRUE) # Step 5: Create the line graph with facets for months in order ggplot(df_long, aes(x = Year, y = Temperature, color = Type, group = Type)) + geom_line(size = 1) + # Add lines geom_point(size = 2) + # Add points facet_wrap(~ Month, scales = "free_y", nrow = 3, ncol = 4) + # Correct sequence of months labs(title = "Monthly Temperature Trends Over Years", x = "Year", y = "Temperature (°C)") + scale_y_continuous(breaks = scales::pretty_breaks(n = 10)) + # Proper Y-axis scale_x_continuous(breaks = seq(min(df$Year, na.rm = TRUE), max(df$Year, na.rm = TRUE), by = 1)) + # Remove decimals in Year scale_color_manual(values = c("Max_Temperature" = "red", "Min_Temperature" = "blue")) + # Color mapping theme_minimal() + theme(axis.text.x = element_text(angle = 45, hjust = 1)) # Rotate X-axis labels for clarity
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter