temperature <- read.table("~/LaptopShare/Teaching/s215b.S04/UStemp.dat",sep="\t",header=T) library(maps) # blue to green to red palette pseudoPalette <-function (low = "white", high = c("green", "red"), mid = NULL, k = 50) { low <- col2rgb(low)/255 high <- col2rgb(high)/255 if (is.null(mid)) { r <- seq(low[1], high[1], len = k) g <- seq(low[2], high[2], len = k) b <- seq(low[3], high[3], len = k) } if (!is.null(mid)) { k2 <- round(k/2) mid <- col2rgb(mid)/255 r <- c(seq(low[1], mid[1], len = k2), seq(mid[1], high[1], len = k2)) g <- c(seq(low[2], mid[2], len = k2), seq(mid[2], high[2], len = k2)) b <- c(seq(low[3], mid[3], len = k2), seq(mid[3], high[3], len = k2)) } rgb(r, g, b) } # a color bar for legend pseudoColorBar <- function (x, horizontal = TRUE, col = heat.colors(50), scale = 1:length(x), k = 11, log.ticks=FALSE,...) { if (is.numeric(x)) { x <- x colmap <- col } else { colmap <- x low <- range(scale)[1] high <- range(scale)[2] x <- seq(low, high, length = length(x)) } if (length(x) > k){ x.small <- seq(x[1], x[length(x)], length = k) if (log.ticks){ x.small <- sign(x.small)*(2^abs(x.small) -1) x <- sign(x)*(2^abs(x) -1) } } else{ x.small <- x if (log.ticks){ x.small <- sign(x.small)*(2^abs(x.small) -1) x <- sign(x)*(2^abs(x) -1) } } if (horizontal) { image(x, 1, matrix(x, length(x), 1), axes = FALSE, xlab = "", ylab = "", col = colmap, ...) axis(1, at = rev(x.small), labels = signif(rev(x.small), 2), srt = 270) } if (!horizontal) { image(1, x, matrix(x, 1, length(x)), axes = FALSE, xlab = "", ylab = "", col = colmap, ...) par(las = 1) axis(4, at = rev(x.small), labels = signif(rev(x.small),2)) par(las = 0) } box() } map.palette <- pseudoPalette(low="blue",high="yellow",mid="orange",k=80) # need each temperature to map into a color temperature.colors <- map.palette[trunc((temperature$JanTemp + abs(min(temperature$JanTemp)) +1)/(range(temperature$JanTemp)[2] - range(temperature$JanTemp)[1] +1)*80)] png("TempDist1.png",height=800,width=1024) layout(matrix(c(1, 2), 1, 2), width = c(9, 1)) par(mar = c(4, 4, 5, 3)) plot(-temperature$Long,temperature$Lat,pch=20,col=temperature.colors,cex=4,xlab="Longitude",ylab="Latitude",main="Mean January Minimum Temperatures") map("usa",add=T) par(mar = c(4, 0, 5, 3)) pseudoColorBar(-10:65,horizontal=F,col=map.palette) layout(1) par(mar = c(5, 4, 4, 2) + 0.1) dev.off()