Load and Inspect Data

setwd("~/Downloads/Intro to R/Module 6")
giss <- read.csv("giss_temp.csv")
names(giss) <- tolower(names(giss))
head(giss)
##   year month  decdate tempanom
## 1 1881     1 1881.042    -0.20
## 2 1881     2 1881.125    -0.24
## 3 1881     3 1881.208    -0.01
## 4 1881     4 1881.292    -0.05
## 5 1881     5 1881.375    -0.06
## 6 1881     6 1881.458    -0.32
str(giss)
## 'data.frame':    1572 obs. of  4 variables:
##  $ year    : int  1881 1881 1881 1881 1881 1881 1881 1881 1881 1881 ...
##  $ month   : int  1 2 3 4 5 6 7 8 9 10 ...
##  $ decdate : num  1881 1881 1881 1881 1881 ...
##  $ tempanom: num  -0.2 -0.24 -0.01 -0.05 -0.06 -0.32 -0.14 -0.12 -0.26 -0.29 ...

Clean Data

giss_clean <- giss[complete.cases(giss$decdate, giss$tempanom), ]
giss_clean$col <- ifelse(giss_clean$year < 1980, "blue", "red")

Scatter Plot of Temperature Anomaly

plot(giss_clean$decdate, giss_clean$tempanom,
     col = giss_clean$col,
     xlab = "Decimal Date",
     ylab = "Temperature Anomaly",
     main = "GISS Temperature Anomalies (Colored by Year < 1980)")
legend("topleft", legend = c("Before 1980", "1980 and After"), 
       col = c("blue", "red"), pch = 1)

Save Plot to File

png("giss_year_color_by_1980.png", width = 800, height = 600)
plot(giss_clean$decdate, giss_clean$tempanom,
     col = giss_clean$col,
     xlab = "Decimal Date",
     ylab = "Temperature Anomaly",
     main = "GISS Temperature Anomalies (Colored by Year < 1980)")
legend("topleft", legend = c("Before 1980", "1980 and After"), 
       col = c("blue", "red"), pch = 1)
dev.off()