R spells

Mapping in R

This post will be a significant consideration for ecologists/biologists or beginners in geoinformatics to plot their study locations with less effort. The same thing can be done on surfer, qgis, or in Arcgis, but here in R you can fine tune your charts with multiple layers. Before we start, we have to set  basic knowledge in mapping and geographical information systems.

Vector and raster data

You might have heard this, like. . . .”this is a vector data, or you have to extract the raster data points”. What it really is a simple logic. All the maps are in a 2D plane, such as x and y, like images. Images/maps have pixel values to represent the points/grids/cells are called raster data, whereas vector data are pairs of values which represent particular shapes such as roads, country borders. In mapping we use both layer by layer to satisfy the data visualization.

Download your data

There are plenty of open source data sets readily available, most of them are free too. Natural earth and open street maps are the most popular among these. However, the best way to start mapping is by using the data sets in R itself, which can be accessed by the package Rworldmap. The Rworld map plots are more similar to surfer maps(lines with filled colours). For more asthetic look you have to incorporate the multiple raster images from above mention sources. Any way lets start with Rworld map.

image.pngIt is a better option to install both rworldmap and rworldextra , for higher resolution maps and you can install it by  a single click using  spaces in between them.

Install the  module ‘maps’

you can easily install the ‘maps’ module by type inside repository dialogue shown above. ‘maps’ are the easy way to plot filled, or nonfilled maps.

Mount all the  modules  by

library(rworldmap)
library(rworldextra)
library(maps)

Now you can plot the world map by simply typing

map()

wmap.png

This is a full representation of available map data in the package Rworldmap. Most often you need not plot the entire world, but a part of it. This could be achieved by different ways.Suppose you want to plot India, type the code:

#Simply plot India
map("world","India")
#Make it more awesome
map("world","India",col = "grey",fill = TRUE)

 

 

Researchers mostly need to specify the coordinates, which make more detailed representation of study area/Area of interest. To attain this, create a dataset using map.

#Create location from map
loc<- map("world","India",col = "grey",fill = TRUE)
#plot the location with labels
plot(loc,type="l",xlab="Longitude",ylab="Latitude")
#Add grey
map("world","India",col = "grey",fill = TRUE,add=TRUE)

 

 

To get more on realistic mapping representaion we need to use x and y limits. Suppose if we want to plot south eastern part of India.

plot(loc,type="l",xlab="Longitude",ylab="Latitude",xlim=c(70,80),ylim=c(8,22))
map("world","India",col = "grey",fill = TRUE,add=TRUE)

Rplot.jpg

Plotting your locations

Now your map is ready! and this is the time to add your sample points/locations. For this you need to have the coordinates: latitudes and Longitudes. As you are plotting the locations on a 2D plain, you need to convert it to decimals, and this is very important. Here we are going to done this with a dummy data.

longitude latitude
1   73.67016 13.84322
2   72.67729 13.84322
3   71.59416 13.84322
4   70.48094 13.84322
5   74.51259 11.97489
6   72.94807 11.97489
7   72.37642 11.93514
8   70.93225 11.93514
9   76.04702 10.18606
10  75.02407 10.18606
11  73.12859 10.30531
12  71.74460 10.06680

  You could see that the data points are in decimals, and be sure it is. Let’s start the add the points to map(I expect that your map is still on your map panel).

points(locations$longitude,locations$latitude,col="orange",pch=16)

mappoin.jpgThat’s it, your sampling locations ready. But still you find some asthetic emptiness in your map. This could be overcome by a adding various datasets like bathymetry, remote sensing data sets or your own gridded/contour data sets. Lets have look what we can do.

#Load the bathymetry data from NOAA using marmap
library(marmap)#if it is not in your package list install it simply by marmap on the dialog box
bathydata<- getNOAA.bathy(70,80,8,22)#Be sure the locations are same
#Make some colour combinations.Here I took blues and greys

blues <- c("lightsteelblue4", "lightsteelblue3",
 "lightsteelblue2", "lightsteelblue1")
greys <- c(grey(0.6), grey(0.93), grey(0.99))
#Now plot the data 'bathydata' with the colours we created
plot(bathydata, image = TRUE, land = TRUE, lwd = 0.03,
 bpal = list(c(0, max(bathydata), greys),
 c(min(bathydata), 0, blues)))
#Add the map overlay
map("world","India",col = "grey",fill = TRUE,add=TRUE,xlim=c(70,80),ylim=c(8,22))
#make thickened contour lines
plot(bathydata, n = 4, lwd = 0.4, add = TRUE)
#Add your sampling points(pch=25 makes it inverted triangles)
points(locations$longitude,locations$latitude,pch=25,col="black",bg="orange")
title("My Sampling Locations")

mapcont.jpg

Yes, now this is a publication quality location map. As I have explained it in a very few lines you will be confused about using the bathymetry datasets, . But I assure that the coming series of my tutorials  will make you able to use external datasets in maps .

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