In this tutorial we’ll cover the following topics:
There is no function for displaying maps in the base R functionality. To display a simple map, we use the maps
package.
library(maps)
For most of our examples we will be using Indiana air pollutant monitor locations.
monitors <- read.table(header=T, text='
monitorid lat long datum name
1 41.60668 -87.304729 WGS84 Gary-IITRI
2 39.811097 -86.114469 WGS84 Indpls-Washington-Park
3 39.749019 -86.186314 WGS84 Indpls-Harding-St.
4 38.013248 -87.577856 WGS84 Evansville-Buena-Vista
5 39.159383 -86.504762 WGS84 Bloomington
6 39.997484 -86.395172 WGS84 Whitestown
')
First, let’s create a simple Indiana map.
map(database = 'state', regions = 'indiana')
Now we can use our monitor lats and longs to add points to the map using the base points()
function.
points(x = monitors$long, y = monitors$lat)
We can jazz it up a bit by making it a county map and changing the symbol type and color.
map(database = 'county', regions = 'indiana')
points(x = monitors$long, y = monitors$lat, pch = 19, col = 'red')
We can also select specific counties to look at, and give the map a title.
map(database = 'county', regions = c('indiana,marion', 'indiana,boone'))
points(x = monitors$long, y = monitors$lat, pch = 19, col = 'red')
title(main = "Air Monitor Locations")
If we want the counties to be automatically labeled, we use the map.text
function.
map.text(database = 'county', regions = c('indiana,marion', 'indiana,boone'))
points(x = monitors$long, y = monitors$lat, pch = 19, col = 'red')
title(main = "Air Monitor Locations")
And if we want to label the monitors, we use the text()
function.
map(database = 'county', regions = c('indiana,marion', 'indiana,boone'))
points(x = monitors$long, y = monitors$lat, pch = 19, col = 'red')
title(main = "Air Monitor Locations")
text(x = monitors$long, y = monitors$lat, labels = monitors$name, pos = 2)
To make interactive Google maps, we can use the googleVis
package.
library(googleVis)
We have to do some formatting first. The coordinates must be in the form lat:long
so we need to create a data frame with a variable in that format.
google.location <- paste(monitors$lat, monitors$long, sep = ":")
monitors.google <- data.frame(monitors, google.location)
g.inter.map <- gvisMap(data = monitors.google, locationvar = "google.location",
tipvar = "name")
plot(g.inter.map)
Leaflet is another interactive map that can be created in R using the leafletR
package.
library(leafletR)
The coordinates must first be converted to a GeoJSON file using the toGeoJSON()
function.
leaf.data <- toGeoJSON(data = monitors[, -1], dest = tempdir(), name = "monitors")
And we create the interactive map using the leaflet()
function.
leaf.map <- leaflet(data = leaf.data, popup = "name", dest = tempdir())
browseURL(leaf.map)
If your coordinates are in UTMs, you need to transform them to lat/longs to plot them on a projected map. For this example, we’ll use the deep river chemistry data from the IDEMdata
package on GitHub. First we need to load the devtools
package and use it to download the IDEMdata
package from GitHub.
library(devtools)
install_github("InDEM/IDEMdata")
Now we load the IDEMdata
package and the deep_river_chemistry
data frame.
library(IDEMdata)
data(deep_river_chemistry)
To transform the UTMs to lat/longs, we use the rgdal
package.
library(rgdal)
Now we create a UTM coordinates matrix using the SpatialPoints()
function and transform the coordinates using the spTransform()
function.
utms <- SpatialPoints(deep_river_chemistry[, c("UTM_EAST", "UTM_NORTH")],
proj4string=CRS("+proj=utm +zone=16"))
longlats <- spTransform(utms, CRS("+proj=longlat"))
Now we can plot these locations on a county map.
map(database = 'county', regions = c('indiana,lake', 'indiana,porter'))
points(longlats, pch = 18, col = 'blue')
This tutorial was created using RStudio’s R Markdown. The code can be found on GitHub.