This tutorial will be about you doing stuff. I will ask you to try things probably before you understand fully what they are. No need to worry.

  1. It doesn't matter if you make mistakes.
  2. We will come back to some of the concepts later.
  3. Questions are encouraged.
  4. This should at least give you a start to follow up on later.

The beauty (& sometimes otherwise) of R is that there are usually multiple ways of doing the same thing. Here I will introduce you to some, there are others.

I suggest you copy and paste code chunks from this document into the R console and then modify them.

1 the tmap package

tmap is one package for making maps in R.

Install and load the tmap package.

#install if not installed already
if (require(tmap)) install.packages("tmap")
library("tmap")

1.1 Getting started with polygon maps

# load data from tmap
data(World)
# set shapes and fill them
tm_shape(World) +
  tm_fill("green")

# check what data are associated with a map using 'str(*@data)', str stands for structure
str(World@data)

# check the contents of Factor variables using 'levels'
levels(World$continent)

# plot a subset of the map using '[which(*),]'
tm_shape(World[which(World$continent=='Africa'),]) +
  tm_fill("red")

# check the contents of numeric variables using 'hist'
hist(World$pop_est_dens)

# fill polygons on a map based on data variables
# factor
tm_shape(World) +
  tm_fill("continent")

# numeric
tm_shape(World[which(World$continent=='Africa'),]) +
  tm_fill("pop_est_dens")

# to add a histogram to the legend
tm_shape(World[which(World$continent=='Africa'),]) +
  tm_fill("pop_est_dens", legend.hist = TRUE)

# the previous commands used default colours, you can modify these with 'palette'
# and make continuous with style='cont'
tm_shape(World[which(World$continent=='Africa'),]) +
  tm_fill("pop_est_dens", palette = "YlGnBu", style='cont')

# to see available palettes 
RColorBrewer::display.brewer.all()

# maps are stored as a SpatialPolygonsDataFrame class from the package sp
# you can find this out by typing
class(World)

1.2 Exercise : can you plot different areas, different variables, different colours ...

# to see help use '?'
?tm_fill

# syntax is like ggplot2 (e.g. + to add layers)

# start with one of the code examples above and change something
# e.g. change tm_fill("pop_est_dens") to tm_fill("area") 
# or one of the other fields shown in str(World@data)

2.1 Getting started with point maps

# load some tmap point data for metropolitan areas
data(metro)
# ?metro gives information about the data
# this is also an sp object with associated variables
class(metro)
str(metro@data)

# plot points as bubbles
tm_shape(metro) +
    tm_bubbles("pop2010", legend.size.show = FALSE)

# plot points on top of a map
tm_shape(World[which(World$continent=='Africa'),]) +
  tm_borders() +
tm_shape(metro) + 
  tm_bubbles("pop2010")

2.2 Exercise - can you change one of the point maps ?

...

3.1 Getting started with raster maps

# load some data from tmap
data(land)

class(land)

str(land@data)

# plot land and add raster of trees
tm_shape(land) + 
    tm_raster("trees", breaks=seq(0, 100, by=20) )

# plot land and add categorical land cover
tm_shape(land) + 
    tm_raster("cover")


# just for a selected country
tm_shape(World[which(World$name=='South Africa'),], is.master=TRUE) +
    tm_borders() +
tm_shape(land) + 
    tm_raster("cover")


# using faceting to create multiple plots

# facet by cover to get one map for each cover type
tm_shape(land) + 
    tm_raster("cover", legend.show = FALSE) +
    tm_facets("cover", free.coords=TRUE, drop.units=TRUE)   


# facet by country to get one cover map per country
tm_shape(land) + 
    tm_raster("cover") + 
tm_shape(World[which(World$continent=='Africa'),]) +
    tm_borders() +
    tm_facets("name", free.coords = TRUE)

4 map your own data in tmap

You can use both your own geometry and/or attribute data in tmap and other mapping options.

# create a very simple data frame as an example
dF <- data.frame( country=c("South Africa", "United Kingdom"),
                  weather=c("hot", "cold") )

# tmap can join this onto a map
World_and_dat <- append_data(World, dF, key.shp="name", key.data="country")

tm_shape(World_and_dat) +
  tm_fill("weather", palette=c("blue", "red"))

# Can you change this to plot a different map ? 
# Maybe add other countries or a different variable.

5 Interactive web maps with leaflet

leaflet is an R package that links to a web-mapping tool also called leaflet, with this you can create interactive maps.

#install if not installed already
if (require(leaflet)) install.packages("leaflet")
library(leaflet)
# to see help on the package
?leaflet

# create a default map
mymap = leaflet() %>% addTiles()

# use %>% (called a 'pipe') to modify the map

# set view by a point (long, lat) and zoom level
mymap %>% setView(32, -26, zoom=10)

# set view by the edges
#fitBounds(lng1, lat1, lng2, lat2)
mymap %>% fitBounds(30, -29, 35, -24)

6 A short ggmap example for Thoyoyando

#install if not installed already
if (require(ggmap)) install.packages("ggmap")
library(ggmap)
mymap <- get_map("thoyoyando, south africa")
ggmap(mymap)

mymap <- get_map("thoyoyando, south africa", maptype='satellite', zoom=15)
ggmap(mymap)

#creating a dataframe with a point in to add to the map
tho_points <- data.frame(lon=c(30.48),lat=c(-22.88),class=c("house"))

ggmap(mymap) +
  geom_point( aes(x = lon, y = lat, colour=class), data = tho_points)

Going further, other useful resources and packages

tmap in a nutshell

A more advanced mapping tutorial

# a new package for getting world bank data
install.packages("wbstats")
# http://www.r-bloggers.com/new-r-package-to-access-world-bank-data/

# raster package, great for mapping satellite data etc.
install.packages("raster")
library(raster)

# rmapshaper for simplifying polygon boundaries
install.packages("rmapshaper")
library(rmapshaper)

# leaflet a package for creating interactive maps
install.packages("leaflet")

Acknowledgements :

Thankyou to all the package developers on whose work this tutorial is based.



AndySouth/rmapteach documentation built on May 5, 2019, 6:01 a.m.