# R Script to add change periods to a trend chart link <- c("C:\\R_Home\\Charts & Graphs Blog\\Trend_Chart_w_Change_Pts\\Trend_w_Change_pt_enso.csv") # Setup plot margins par(oma=c(0,0,0,0)); par(mar=c(3,3,1,0)); par(mgp=c(1.5,.4, 0)) par(pty="m"); par(ps=9); par(cex.axis=c(0.9)); par(cex.lab=c(0.9)); par(tcl="NA"); par(tck=-0.01) my_Data <- read.csv(link, skip = 1, sep = ",", dec=".", row.names = NULL, header = FALSE, colClasses = c("character", "character"), comment.char = "#", col.names = c("CP", "Code" ) ) attach(my_Data); colr <- NULL # Convert CP from character class to date class CP <- as.Date(CP,"%m/%d/%Y" ); periods<- nrow(my_Data) -1 col1 <- c("lightblue"); col2 <- c("white"); col3<- c("pink") yyb <- c(-0.4); yyt <- c(1) min_d <- as.Date("1/1/1950", "%m/%d/%Y"); max_d <- as.Date("12/31/2010", "%m/%d/%Y") plot(c(min_d, max_d),c(yyb, yyt),type="n", las=1, col = "blue", ylab = expression(paste("Temperature Anomaly ( ", degree, "C)")), main =expression(paste("GISS Temperature Anomaly ( ", degree, "C) and ENSO Condition")), xaxs="i", yaxs="i", bty= "n") for (i in 1:periods) { colr[i] <- ifelse(Code[i] == "-1", col1, ifelse(Code[i] == "0", col2, col3)) rect(CP[i], yyb, CP[i+1],yyt, col = colr[i], border=NA)} link_2 <-"C:\\R_Home\\Charts & Graphs Blog\\Trend_Chart_w_Change_Pts\\GISS_global_temp_trend.csv" GISS_Data <- read.csv(link_2,skip = 0, sep = ",", dec=".", row.names = NULL, header = FALSE, colClasses = c("character","numeric"),comment.char="#", col.names = c("my_dt", "AnomC")) attach(GISS_Data); my_dt <- as.Date(my_dt, "%m/%d/%Y") points(AnomC ~ my_dt, type = "l", col = "grey40"); abline(h=0, col = "grey90") el_dt <- as.Date(c("9/1/1960"),"%m/%d/%Y"); el_dt2 <-as.Date(c("8/1/1957"), "%m/%d/%Y") la_dt <- as.Date(c("9/15/1960"), "%m/%d/%Y") ; la_dt2 <- as.Date(c("6/1/1955"), "%m/%d/%Y") text(el_dt, 0.62, "El Nino") ; text(la_dt, 0.52, "La Nina") arrows(el_dt, 0.6, el_dt2, 0.55, length = 0.0, col = "grey") arrows(la_dt, 0.5, la_dt2, 0.45, col = "grey", length = 0.0) mtext("Source: GISS Temp - NASA, El Nino - La Nina - NOAA ONI index", 1, line = 1.5, outer = FALSE, adj=1, cex=0.9) axis(2, labels=FALSE);detach(GISS_Data)