Plot maps with ‘base’ mapping tools in R

Understanding what kind of data you have (polygons or points?) and what you want to map is pivotal to start your mapping.

  1. First you need a shapefile of the area you want to plot, such as metropolitan France. There are various resources where to get them from: DIVA-GIS and EUROSTAT are those that I use the most. It’s always important to have a .prj file included, as your final map ‘should’ be projecte. I say “should” as sometimes it is just not possible, especially if you work with historical maps.

  2. Upload libraries

library(maps) #for creating geographical maps
library(maptools) #tools for handling spatial objects
library(RColorBrewer) #contains color palettes
library(classInt) #defines the class intervals for the color palettes
library(rgdal) #reads proj files
  1. Read the shapefiles

shape file:

france <- readShapePoly("./FRA_adm2.shp", IDvar = "ID_2")

projected map:

fr.prj <- readOGR(".", "FRA_adm2")
## OGR data source with driver: ESRI Shapefile 
## Source: ".", layer: "FRA_adm2"
## with 96 features
## It has 18 fields

Here it’s how they plot:

op <- par(mfrow=c(1,2))
plot(france, main="Shp", line=-2)
plot(fr.prj, main="Prj", line=-2)

par(op)
  1. Data:

Here I create a variable as I don’t have any at hand… If you do have a dataset, check that your data matches exactly the shapefile (this is very important). To create “fake” data, first look at how many polygons are in the shapefile:

length(france$ID_2)
## [1] 96

and generate a variable:

set.seed(100)
myvar <- rnorm(1:96)

Color palettes

For more on color palettes styles see classInt package and ColorBrewer2 where you can try color palettes.

nclassint <- 5 #number of colors to be used in the palette
cat <- classIntervals(myvar, nclassint,style = "quantile") #style refers to how the breaks are created
colpal <- brewer.pal(nclassint,"Greens") #sequential palette
color <- findColours(cat,colpal) 
bins <- cat$brks
lb <- length(bins)
# pdf("France.pdf")
plot(fr.prj, col=color,border=T) 
legend("bottomleft",fill=colpal,legend=paste(round(bins[-length(bins)],1),":",round(bins[-1],1)),cex=1, bg="white")

# dev.off()