[R] plotting a grid with color on a map

Jim Lemon jim at bitwrit.com.au
Wed May 13 13:49:06 CEST 2009

dxc13 wrote:
> 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?
Hi dxc13,
This might get you started:

# have to draw the map to get the user coordinates
# get the limits of the map
# transform the temperatures into colors, reversing the row order
# don't erase the current plot
# draw an empty plot with the appropriate axes (I think)
# add the map over the color squares

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.


More information about the R-help mailing list