Maps in R – Examples (Part 2)
This part of the examples web page describes map projection done using the rgdal package (and the sp class and maptools packages).
The sp class and maptools package provide a mechanism for doing projected maps. (Note that the projection parameters used in the example here are not really appropriate for the area being plotted, but were chosen to make the fact that the data are projected evident.)
First, load the rgdal package
This example plots a projected map of
# equal-frequency class intervals -- spplot & projected
The next block adds “projection string” (i.e. the name of the projection that the data are currently in to the shape file (in case it’s not already there), and does the projection. First a long string of information in specified in the first statement, then the spTransform() function does the actual conversion of latitudes and longitudes into x, y coordinates. Next, the gridlines() function creates a graticule in latitude and longitude, and the spTransform() function projects those. Finally, the spplot() function makes the map.
Here’s the map:
Note that the map is a little different from that made using the standard plot() function. spplot()uses the “lattice package” for plotting, and (in these examples) automatically adds a legend.
a second example,
# project Oregon county data
Note that the code for producing maps using the spplot() function is much like that using plot(): some setup work first, then determination of the actual colors or symbols that will be plotted, then finally creation of the map.
4. Examples using the maps package
The R maps
package provides a means of mapping data that are not necessarily components
of a shapefile. The package provides a way of plotting choropleth maps
using polygons that it contains (
# look at the orcounty.csv data
map("county", "oregon", xlim=c(-125,-114),
The map() function does the main work of making the map, but has some other applications. When the data are contained in a shape file, the correct attributes (one hopes) are automatically associated with each point or polygon. In this example, the data come from a .csv file, and so the first thing to do is to make sure that the right lines in the .csv file are being matched with the right polygons that the map() function uses. The two lines of code
print out the names of the polygons, and the county names in the spreadsheet. The map() function assumes that the data/polygons are in alphabetical order. Next the usual stepup work is done, the basemap is plotted using the map() function, and axes are labeled using the map.axes() function. Finally, the “bubbles” are added using the points() function. Note that this map is unprojected.
5. Basemaps using the maps package
The maps package provides a means of constructing basemaps for plotting the locations of points, which can be decorated with text, symbols, and so on -- most of the things that be done on scatter plots.
The following example plots the location of large cities. A built-in data set world.cities is made available using the data() function, and some information describing each of the cities in the cities dataframe is gotten using the match() function, with looks at the name of a city in the cities2 dataframe, and looks in world.cities for the information (latitude and longitude in this case).
# map of large cities
data(world.cities) # make the world cities location data set from the maps package available
# match the large cities with those in the database
# assign the world.cities location information to the large cities
big.cities <- NULL
big.cities$name <- cities2$City
big.cities$long <- world.cities$long[m]
big.cities$lat <- world.cities$lat[m]
# plot the map
text(big.cities$long, big.cities$lat, big.cities$name, col="red", cex=.5)
The next example generates a projected map of large cities. The projection is done using the mapproject() function.
map of large cities
big.cities <- NULL
6. Further examples illustrating map projection using the maps package
The maps package can provide projected base maps, which can provide less
distorted views of a data set. Here are some examples that plot the
locations of the
unprojected maps (left map below)
projected maps (right map below)
7. Basemap shapefile generation using the maps package
function in the maptools
function can be used to transform lines extracted from the maps package
package-compatible format to provide basemaps for plotting other data sets.
Here is an example for the
#library(maptools) # also loads sp package
Here’s what you get:
There’s a lot more that can be accomplished, but remember that R is not really a cartographic design-type of package (if there even is one). The main utility of the mapping tools is their ability to visualize data rapidly.