Module06_Exercise: Climate Data Analysis

Author

Cienna Kim

Introduction

This report presents the analysis of the GISS global temperature dataset (giss_temp.csv) using tasks completed during Module 5. The primary objective is to demonstrate how advanced R control structures, such as for loops and conditional statements (ifelse), can be integrated into an automated report using Quarto.

  • Part 1 focuses on constructing a for loop to iterate across years and prepare monthly temperature plot generations.
  • Part 2 utilizes an ifelse conditional statement to visually differentiate global warming trends before and after the critical threshold year of 1980.

Part 1: Loop Across Years

In this section, we modify the standard monthly loop to iterate across all unique years in the dataset. This automated process isolates the 12 monthly temperature anomalies for each individual year and saves them as separate PNG files.

Note: The code below is displayed with the evaluation option turned off (eval: false) to prevent rendering a vast number of image files simultaneously.

## Import File
giss <- read.csv("./data/giss_temp.csv")

## Define Loop Variables
allyears <- unique(giss$Year)
nyears <- length(allyears)

## Run Loop to Save Yearly Temperature Plots
for (i in 1:nyears) {
  yearID = which(giss$Year == allyears[i])
  png(paste("giss_year_", allyears[i], ".png", sep=''))
  plot(giss$Month[yearID], giss$TempAnom[yearID],
       xlab = "Month", ylab = "Temperature Anomaly", main = "Loops Across All Years")
  dev.off()
}

Part 2: Conditional Coloring with if/ifelse Statements

In the second section, we investigate the chronological shift in global temperatures. To visualize the divergence clearly, we calculate the annual mean temperature deviations and use an ifelse statement to assign a conditional color vector based on whether the year is before or after 1980.

  • Years before or equal to 1980 are colored blue (representing baseline levels).
  • Years after 1980 are colored red (representing the accelerating warming trend).
## Import File
giss <- read.csv("./data/giss_temp.csv")
allyears <- unique(giss$Year)
  
## Calculate Annual Average Temperature
annual_temp = tapply(giss$TempAnom, giss$Year, mean)

## Set Color Vector Based on Year 1980
mycols = ifelse(allyears <= 1980, "blue", "red")

## Make a Plot of Annual Trend with Conditional Colors
plot(allyears, annual_temp, 
     type = 'h', 
     col = mycols, 
     lwd = 3, 
     xlab = "Year", 
     ylab = "Temperature Anomaly", 
     main = "Annual Temperature Anomalies (Before and After 1980)")