13.16 Creating a Map

13.16.1 Problem

You want to create a geographical map.

13.16.2 Solution

Retrieve map data from the maps package and draw it with geom_polygon() (which can have a color fill) or geom_path() (which can’t have a fill). By default, the latitude and longitude will be drawn on a Cartesian coordinate plane, but you can use coord_map() and specify a projection. The default projection is “mercator”, which, unlike the Cartesian plane, has a progressively changing spacing for latitude lines (Figure 13.32):

Top: a basic map with fill; bottom: with no fill, and Mercator projectionTop: a basic map with fill; bottom: with no fill, and Mercator projection

Figure 13.32: Top: a basic map with fill; bottom: with no fill, and Mercator projection

13.16.3 Discussion

The map_data() function returns a data frame with the following columns:

  • long: Longitude.
  • lat: Latitude.
  • group: This is a grouping variable for each polygon. A region or subregion might have multiple polygons, for example, if it includes islands.
  • order: The order to connect each point within a group.
  • region: Roughly, the names of countries, although some other objects are present (such as some lakes).
  • subregion: The names of subregions within a region, which can contain multiple groups. For example, the Alaska subregion includes many islands, each with its own group.

There are a number of different maps available, including world, nz, france, italy, usa (outline of the United States), state (each state in the USA), and county (each county in the USA). For example, to get map data for the world:

If you want to draw a map of a region in the world map for which there isn’t a separate map, you can first look for the region name, like so:

It’s possible to get data for specific regions from a particular map (Figure 13.33):

Specific regions from the world map

Figure 13.33: Specific regions from the world map

If there is a separate map available for a region, such as nz (New Zealand), that map data will be at a higher resolution than if you were to extract it from the world map, as shown in Figure 13.34:

New Zealand data taken from world map (left); Data from nz map (right)New Zealand data taken from world map (left); Data from nz map (right)

Figure 13.34: New Zealand data taken from world map (left); Data from nz map (right)

13.16.4 See Also

See the mapdata package for more map data sets. It includes maps of China and Japan, as well as a high-resolution world map, worldHires.

See the map() function, for quickly generating maps.

See ?mapproject for a list of available map projections.