Area Plots with Intensity Coloring ~ el nino SST anomalies w/ ggplot2

[This article was first published on mind of a Markov chain » R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

I see many economy indicator graphs that show emphasis by shading in the curve under the area (while x-axis is time). The shade is stronger at higher values (example).

I did this in R below (ggplot2). This was a little more difficult that I’d expected. The color gradients are good to color each individual points depending on the strength, but not good (AFAIK) to color regions outside the dataset. The functions, geom_area, geom_ribbon geom_area, geom_linerange and the like can color themselves for different pieces, but we can’t specify color gradients within the piece. So I basically created a grid of points with different strengths dependent on the location of the y-axis.

Data is here (NOAA, first three lines commented out + I put a space in front of all minus signs) + similar examples at Learn R and Climate Charts and Graphs.

Plot below:

Simpler and faster to just do points, but was not what I wanted.

R code:

# ---
## NINO 3.4 SST Anomaly
nino <- read.table("/./NINO 3.4 SST Anomaly.txt", head = T)
nino$Week <- strptime(nino$Week, format = "%d%b%Y") <- rep(c("Nino1+2", "Nino3", "Nino34", "Nino4"), each = 2*dim(nino)[1])
nino <- data.frame(Week = nino$Week, stack(nino[,-1]),
nino$ind <- gsub(".[0-9]", "", nino$ind)

head(nino, 2)
##         Week values ind
## 1 1990-01-03   23.4 SST   Nino1+2
## 2 1990-01-10   23.4 SST   Nino1+2

qplot(x=Week, y=values, data = subset(nino, ind == "SSTA" & == "Nino34"), geom = "point", col = values, fill = values, shape = I(15), solid = TRUE) + scale_colour_gradient2(low="blue", mid="white", high="red")

# -- fill area
nino34 <- subset(nino, ind == "SSTA" & == "Nino34")

cut <- 100
nino34.grid <- with(nino34, outer(values, seq(min(values), max(values), length=cut), function(X,Y) { (X >= 0 & X > Y & Y > 0) | (X <= 0 & X < Y & Y < 0) }))

nino34.grid <- t(apply(nino34.grid, 1, function(x) { x[x == F] <- NA; x*seq(min(nino34$values), max(nino34$values), length=cut) }))

colnames(nino34.grid) <- round(seq(min(nino34$values), max(nino34$values), length=cut), 2)
rownames(nino34.grid) <- as.character(nino34$Week)

nino34.grid <- melt(nino34.grid)

qplot(as.POSIXct(X1), X2, fill = value, data = nino34.grid, geom = "tile") + scale_fill_gradient2(low="blue", mid="white", high="red")

Filed under: Climate Change, ggplot2, R

To leave a comment for the author, please follow the link and comment on their blog: mind of a Markov chain » R. offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)