Draw maps in R

Use R to draw maps for your paper

Very often I have found researchers using google map or low resolution map to show population distributions (or survey regions). They usually don’t appear that great in a research journal atleast when printed out.

This is a step by step approach to draw maps in R. I demonstrate how to use functions in PBSmapping package of R. You can draw any map with two small piece of R code. The following are required (Assuming you have Windows):

1. R installed in your computer

2. PBSmapping – R package (and all its dependencies)

3. GSHHS Database file

I will skip step 1 and 2 since the tutorial for installations have been covered in my earlier posts (Install R, Install Package).

Import GSHHS Database

GSHHS refers to “Global Self-consistent, Hierarchical, High-resolution Shoreline”.  They present a high-resolution shoreline data set maintained by NOAA and are available to download for free. More details are available at : http://www.soest.hawaii.edu/wessel/gshhs/

The shorelines come in five resolutions:

  1. full resolution: Original data resolution.
  2. high resolution: About 80 % reduction.
  3. intermediate resolution: Another ~80 % reduction.
  4. low resolution: Another ~80 % reduction.
  5. crude resolution: Another ~80 % reduction.

They are each organized into 4 hierarchical levels:

  1. L1: boundary between land and ocean.
  2. L2: boundary between lake and land.
  3. L3: boundary between island-in-lake and lake.
  4. L4: boundary between pond-in-island and island.

STEP 1 (Download data)

Find the root directory of PBSmapping R package in your Windows. For example in my computer, the location is ‘C:\Users\Pazhayamadom\Documents\R\win-library\2.15\PBSmapping’

Download ‘GSHHS database in binary format’ (zip file) from the website.

Extract the contents into the root directory of PBSmapping.

The contents include database files (Data for world map) – gshhs_f.b, gshhs_h.b, gshhs_i.b ….etc.

STEP 2 (Load PBSmapping package)

Use the following code to load PBSmapping package in R

STEP 3 (Import data into R)

To import the required data (for map), you should have

1. xlim= range of X-coordinates or Longitudes (for clipping). The range should be between 0 and 360.

2. ylim= range of Y-coordinates or Latitudes (for clipping).

3. maxlevel= maximum level of polygons to import: 1 (land), 2 (lakes on land), 3 (islands in lakes), or 4 (ponds on islands).

The importGSHHS( ) function can be used for importing data (demonstrated with Map of India)

polys <- importGSHHS("C:\\.....\\R\\win-library\\2.15\\PBSmapping\\gshhs_h.b" , xlim=c(63, 93) , ylim=c(5, 30.5) , maxLevel=4)

In the above function, there are 4 inputs or arguments seperated by commas. The data file for high resolution map is specified as gshhs_h.b with the source path or location (use gshhs_f.b file for full resolution). xlim are the minimum and maximum longitudes required. Similarly, ylim are the minimum and maximum latitudes required. maxlevel=4 means to import data for lakes, island and ponds along with land.

This process may take some time to read required data from the file and you should be getting the following message:

importGSHHS status:
--> Pass 1: complete: 1493 bounding boxes within limits.
--> Pass 2: complete.
--> Clipping...

STEP 4 (Draw the map)

The final step is to plot the data which is now imported from the database file.

The plotMap ( ) function is used for this purpose.

plotMap (polys)

R have the option of saving pictures in the format of JPEG, PNG, TIFF, Metafile, Postscript, Bmp and PDF. I prefer Metafile for MS Word, PNG for Latex and Postscript for graphical works (the best choices on my experience).

There are many optional arguments (inputs) for the functions mentioned above. I leave this for you to explore. But a few examples are given below:

To get grey shades for the land, use the plotMap( ) function with an extra argument col=’beige’.

plotMap (polys, col="beige")

If you would like the ocean (background) with a blue shade, use the following code:

plotMap (polys, col="beige", bg="lightblue")

You can add text, pointers and captions with other functions or extra arguments. Since they are beyond the scope of this post, please refer to the graph chapters in any R book.

Hope this was helpful. Thanks for reading the post.

P.S. – Have fun with changing the colour of your choice 🙂


Add Rivers and Borders

rivers <- importGSHHS("C:\\....\\PBSmapping\\wdb_rivers_f.b" , xlim=c(63, 93) , ylim=c(5, 30.5))
borders <- importGSHHS("C:\\....\\PBSmapping\\wdb_borders_f.b" , xlim=c(63, 93) , ylim=c(5, 30.5))

Here we import the database for rivers and borders (wdb_rivers_f.b and wdb_borders_f.b ).

addLines (rivers, col="lightblue")
addLines (borders, col="red")

The above function add rivers and borders to the map. This works only if the map is already plotted using plotMap( ) function.

Add text into the map

You can add labels into the map using text ( ) function. Again this will work only if the map is already plotted.

text (70, 10, "Arabian Sea")

Here 70 and 10 are the x and y coordinates in the map / graph.


About Deepak George Pazhayamadom

I'm a fish biologist and a mathematical modeller. I have a wide range of research interests, mostly centered on fisheries resource management.

Posted on September 15, 2012, in Fisheries Management, Marine Science. Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: