|
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). 3. Projections 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 library(rgdal) 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. Here’s
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 ( library(maps) # look at the orcounty.csv data map("county",
"oregon", xlim=c(-125,-114),
ylim=c(42,47))
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 map("county"," 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 cities 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 m <- match(paste(tolower(as.character(cities$City)),tolower(as.character(cities$Country))), # assign the world.cities
location information to the large cities big.cities <- NULL big.cities$name <- cities$City big.cities$long <- world.cities$long[m] big.cities$lat <- world.cities$lat[m] big.cities # plot the map map("world") map.axes() points(big.cities$long,big.cities$lat,
col="blue") 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 m <- match(paste(tolower(as.character(cities$City)),tolower(as.character(cities$Country))), 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
# projected 7. Basemap shapefile generation using the maps package The map2SpatialLines() function in
the maptools
function can be used to transform lines extracted from the maps package
into sp()
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
there ability to visualize data rapidly. |