R Code for Climate Mathematics:

Theory and Applications

 

A Cambridge University Press book By

SSP Shen and RCJ Somerville

 

 

Version 1.0 released in July 2019 and coded by Dr. Samuel Shen, Distinguished Professor
San Diego State University, California, USA
https://shen.sdsu.edu
Email:

 

Version 2.0 compiled and updated by Momtaza Sayd
San Diego State University May 2021
Version 3.0 compiled and updated by Joaquin Stawsky
San Diego State University June 2022

 

 

 

Chapter 9: R Graphics for Climate Science

Plot Fig. 9.1

#setEPS()
#postscript("fig0901.eps", height = 4, width = 8)
par(mar = c(4.2,4.2,2.5,4.1))
Time <- 2001:2010
Tmean <- c(12.06, 11.78,11.81,11.72,12.02,12.36,12.03,11.27,11.33,11.66)
Prec <- c(737.11,737.87,774.95,844.55,764.03,757.43,741.17,793.50,820.42,796.80)
plot(Time,Tmean,type = "o",col = "red", lwd = 1.5, xlab = "Year",cex.axis = .85,
     ylab = expression(paste(T[mean]," [", degree,"C]")),
     main = "Contiguous U.S. Annual Mean\nTemperature and Total Precipitation")
legend(2000.5,12.42, col = c("red"),lty = 1,lwd = 2.0,
       legend = c(expression(paste(bold(T[mean])))),bty = "n",text.font = 2,cex = 1)
#Allows a figure to be overlaid on the first plot
par(new = TRUE)
plot(Time, Prec,type = "o",col = "blue",lwd = 1.5,axes = FALSE,xlab = "",ylab = "")
legend(2000.5,839, col = c("blue"),lty = 1,lwd = 2.0,
       legend = c("Prec"),bty = "n",text.font = 2,cex = 1.0)
#Suppress the axes and assign the y-axis to side 4
axis(4)
mtext("Precipitation [mm]",side = 4,line = 3)

# legend("topleft",col = c("red","blue"),lty = 1,legend = c("Tmean","Prec"),cex = 0.6)
#Plot two legends at the same time make it difficult to adjust the font size
#because of different scale
#dev.off()

 

Plot Fig. 9.2

#Margins, math symbol, and figure setups
#setEPS()
#postscript("fig0902.eps", height = 4, width = 8)
#Margins, math symbol, and figure setups
par(mar = c(5,4.5,2.5,2.5))
x <- 0.25*(-30:30)
y <- sin(x)
x1 <- x[which(sin(x) >= 0)] 
y1 <- sin(x1)
x2 <- x[which(sin(x) < 0)]
y2 <- sin(x2)
plot(x1,y1,xaxt = "n", xlab = "",ylab = "",lty = 1,type = "h",
     lwd = 3, tck = -0.02, ylim = c(-1,1), col = "red",
     col.lab = "purple",cex.axis = 1.4)
lines(x2,y2,xaxt = "n", xlab = "",ylab = "",lty = 3,type = "h",
      col = "blue",lwd = 8, tck = -0.02)
axis(1, at = seq(-6,6,2),line = 3, cex.axis = 1.8)
axis(4, at = seq(-1,1,0.5), lab = c("A", "B", "C", "D","E"), 
     cex.axis = 2,las = 2)
text(0,0.7,font = 3,cex = 6, "Sine waves", col = "darkgreen") #Itatlic font
mtext(side = 2,line = 2, expression(y == sin(theta-hat(phi))),cex = 1.5, col = "blue")
mtext(font = 2,"Text outside of the figure on side 3",side = 3,line = 1, cex = 1.5)#Bold font
mtext(font = 1, side = 1,line = 1, 
      expression(paste("Angle in radians: ", theta-phi[0])),cex = 1.5, col = "red")

#dev.off()

 

Plot Fig. 9.3

par(mar = c(8,6,3,2))
par(mgp = c(2.5,1,0))
plot(1:200/20, rnorm(200),sub = "Subtitle: 200 Random Values",
     xlab = "Time", ylab = "Random Values", main = "Normal Random Values", 
     cex.lab = 1.75, cex.axis = 1.5, cex.main = 2.0, cex.sub = 1.5)

#Adjust positions of axis labels
par(mgp = c(2,1,0))
plot(sin,xlim = c(10,20),cex.lab = 1.2,cex.axis = 1.2)

 

Plot Fig. 9.4

#A fancy plot of the NOAAGlobalTemp time series
setwd("∼/sshen/climmath")
NOAATemp <- read.table("data/aravg.ann.land_ocean.90S.90N.v4.0.1.2016.txt", header = F)
par(mar = c(4,4,3,1))
x <- NOAATemp[,1]
y <- NOAATemp[,2]
z <- rep(-99,length(x))
for (i in 3:length(x)-2) z[i] <- mean(c(y[i-2],y[i-1],y[i],y[i+1],y[i+2]))
n1 <- which(y >= 0)
x1 <- x[n1]
y1 <- y[n1]
n2 <- which(y<0)
x2 <- x[n2]
y2 <- y[n2]
x3 <- x[2:length(x)-2]
y3 <- z[2:length(x)-2]
plot(x1,y1,type = "h",xlim = c(1880,2016),lwd = 3, 
     tck = 0.02, ylim = c(-0.7,0.7), #tck > 0 makes ticks inside the plot
     ylab = "Temperature [°C]",
     xlab = "Time",col = "red",
     main = "NOAA Global Average Annual Mean Temperature Anomalies",
     cex.lab = 1.2,cex.axis = 1.2)
lines(x2,y2,type = "h",
      lwd = 3, tck = -0.02,  col = "blue")
lines(x3,y3,lwd = 2)

 

Plot Fig. 9.5

#Plot US temp and prec times series on the same figure
par(mfrow = c(2,1))
par(mar = c(0,5,3,1)) #Zero space between (a) and (b)
Time <- 2001:2010
Tmean <- c(12.06, 11.78,11.81,11.72,12.02,12.36,12.03,11.27,11.33,11.66)
Prec <- c(737.11,737.87,774.95,844.55,764.03,757.43,741.17,793.50,820.42,796.80)
plot(Time,Tmean,type = "o",col = "red",xaxt = "n", xlab = "",ylab = expression(paste("T"[mean]," [°C]")))
text(2006, 12,font = 2,"U.S. Annual Mean Temperature", cex = 1.5)
text(2001,12.25,"(a)")
#Plot the panel on row 2
par(mar = c(3,5,0,1))
plot(Time, Prec,type = "o",col = "blue",xlab = "Time",ylab = "Prec. [mm]")
text(2006, 800, font = 2, "U.S. Annual Total Precipitation", cex = 1.5)
text(2001,840,"(b)")

 

Figure layout for multiple panels

layout(matrix(c(1,1,2,3), 2, 2, byrow = TRUE), 
       widths = c(3,3), heights = c(2,2))
plot(sin,type = "l", xlim = c(0,20),cex.lab = 1.2,cex.axis = 1.2)
plot(sin,xlim = c(0,10),cex.lab = 1.2,cex.axis = 1.2)
plot(sin,xlim = c(10,20),cex.lab = 1.2,cex.axis = 1.2)

 

Contours and color-filled contours

x <- y <- seq(-1, 1, len = 25)
z <- matrix(rnorm(25*25),nrow = 25)
contour(x,y,z, main = "Contour Plot of Normal Random Values")

filled.contour(x,y,z, main = "Filled Contour Plot of Normal Random Values")