Skip to content

plotting a grid with color on a map

6 messages · dxc13, Ray Brownrigg, Jim Lemon

#
Hi all,
I have posted similar questions regarding this topic, but I just can't seem
to get over the hump and find a straightforward way to do this.  I have
attached my file as a reference.
Basically, the attached file is a 5 degree by 5 degree grid of the the world
(2592 cells), most of them are NA's.  I just want to be able to plot this
grid over a world map and color code the cells.  For example, if a cell has
a temperature less than 20 degrees it will be blue, 21 to 50 green color,
51-70 orange, 71+ red colored cells.  For any NAs, they should be colored
white.

I know how to create a map of the world using map() and add a grid to it
using map.grid(), but I can't color code the cells the way I need.  Is there
a way to do this in R?

Thanks again.
dxc13 http://www.nabble.com/file/p23514804/time1test.txt time1test.txt
#
dxc13 wrote:
How about the following, which doesn't need a grid at all?

library(maps)
temp <- as.matrix(read.table("time1test.txt"))
xvals <- c(0, 0, 5, 5, 0)
yvals <- c(0, 5, 5, 0, 0)
map("world")
palette(rainbow(50))
for (lat in seq(-90, 85, 5))
   for (lon in seq(-180, 175, 5)) {
     col <- temp[(lat + 95)/5, (lon + 185)/5]
     if (!is.na(col)) polygon(lat + xvals, lon + yvals, col=col, border=NA)
   }
palette("default")

HTH
Ray Brownrigg
#
dxc13 wrote:
Hi dxc13,
This might get you started:

temp1<-read.table("time1test.dat",header=TRUE)
mapcol<-color.scale(as.matrix(temp1[36:1,]),c(0.5,1),c(0.5,0),c(1,0))
# have to draw the map to get the user coordinates
map()
# get the limits of the map
maplim<-par("usr")
# transform the temperatures into colors, reversing the row order
color2D.matplot(temp1[36:1,],cellcolors=mapcol,axes=FALSE)
# don't erase the current plot
par(new=TRUE)
# draw an empty plot with the appropriate axes (I think)
plot(0,xlim=maplim[1:2],ylim=maplim[3:4],type="n")
# add the map over the color squares
map(add=TRUE)

This seems a bit wonky, probably because I haven't adjusted the 
coordinates. Also, I'm only getting grayscale colors, even though the 
colors in "mapcol" aren't gray. Don't know why yet.

Jim
#
It was the NAs that fooled color2D.matplot. This gets your colors, 
although not exactly what you want. Look at the help for color2D.matplot 
to get that. I think fiddling with the x and y limits on the map() call 
will get the positions right.

temp1<-read.table("time1test.dat",header=TRUE)
library(plotrix)
# reverse the row order, as color2D.matplot reverses it
color2D.matplot(temp1[36:1,],c(0.5,1),c(0.5,0),c(1,0),axes=FALSE)
# don't erase the above plot
par(new=TRUE)
# do a "ghost" plot with just the axes
plot(0,xlim=maplim[1:2],ylim=maplim[3:4],type="n")
# add the map on top in black
map(add=TRUE,col="black")

Jim
#
Thanks, Jim.  This seems to be what I am looking for.  Just have to fine tune
the colors to get some distinctive greens, blues, yellows and oranges in
there and I should be good to go.
Jim Lemon-2 wrote: