Wednesday, 12 March 2014

Base map: countries

To produce a base map, with coastal line and filled country, there are many options.

Some of them are:
1) Using map_data- good for large scale maps.

require(ggplot2)
 
world <- map_data("world")
ggplot() + 
 coord_fixed(xlim=c(-100,25), ylim=c(-57,87))+
 geom_polygon(data = world, aes(long, lat, group = group))+
 scale_x_continuous(breaks = seq(-100, 25, by = 20)) + 
 ylab("Latitude")+xlab("Longitude")+
 scale_y_continuous(breaks = seq(-100, 100, by = 20))
Created by Pretty R at inside-R.org






2) Using google maps figures (library ggmap, function qmap)

require(ggmap)
require(ggplot2)
 
# this requires internet
map.worl2 = get_map('Portugal',zoom=7, maptype = c("satellite")) 
ggmap(map.worl2) 
# This map is a ggplot2 type   
Created by Pretty R at inside-R.org



3) for finner resolution (smaller scale), using GADM database, accessed by raster library.

require(raster); require(ggplot2)
 
pt = getData('GADM', country=c('PT'), level=0)
# Crop the polygon for our area of interest
alg.pol <- as(extent(-10, -5, 43, 36), 'SpatialPolygons') # create the spatial polygon, with the extend we want
crs(alg.pol) <- crs(pt) # put into the same geographic coordinate system
pt <- crop(pt, alg.pol) # finally, cut the raster
 
f_pt= fortify(pt) # convert to the data frame format (to plot in ggplot2)
 
ggplot() + 
 theme_bw() + #ggplot white theme
 coord_fixed()+ # avoid ratio distortions
 geom_polygon(data=f_pt, aes(x=long,y=lat,group=group), colour=1, alpha=0.5)+
 ylab("Latitude (N)")+xlab("Longitude (W)")+ # plot labels
 scale_y_continuous(breaks = seq(36, 43, by = .5)) + # add thinner gridlines
 scale_x_continuous(breaks = seq(-10, -5, by = .5)) 
Created by Pretty R at inside-R.org



Another example, changing the coastal limits (the coast of Algarve):




See also http://osmar.r-forge.r-project.org/

No comments:

Post a Comment