The need for relatively easy-to-use tools in the R statistical environment for mapping data on Nigeria was a motivation for the development of this package. Not so much of low-level programming needs to be done by the user; rather a collection of pre-existing packages and idioms are brought together to enable a focus on actual data visualization.
The main function for drawing Nigeria maps is
map_ng. It uses data from the CIA World Database provided by the
mapdata package as well as ESRI shapefiles from the World Bank to draw a simple maps of Nigeria.
To show the map without the state borders, set the first argument (
NULL. Thus, with all other arguments set to default values, that would be
All the Local Government Areas can equally be displayed, using a map that, by default, does not display text, so as to avoid cluttering. This kind of map is useful for charting choropleth maps, as shown below for the States. To this, we pass the value produced by
It is also possible to make maps of sub-national divisions by defining them via
region. For example,
states("sw") returns a character vector of the states in the South-West geopolitical zone (see
?states). To display them on a map with a few embellishments like labelling and colour, write
A single state can also be drawn:
Choropleth maps that display differences among regions can also be created with
map_ng. There are a number of ways this can be done, depending on the kind of arguments supplied to the function:
This map is built on categorical data (i.e.
factors); atomic vectors that are coercible to this class are also accepted. In the case of numeric data, notably those of type
double, they can be coaxed into categories only if class boundaries are provided via the
To demonstrate this, we will create a data frame with some meaningless data:
# Create variables ss <- states() numStates <- length(ss) vv <- sample(LETTERS[1:5], numStates, TRUE) # Create a data frame and view top rows dd <- data.frame(state = ss, var = vv, stringsAsFactors = FALSE) head(dd) #> state var #> 1 Abia D #> 2 Adamawa A #> 3 Akwa Ibom C #> 4 Anambra B #> 5 Bauchi D #> 6 Bayelsa D
To draw this choropleth, we will use the data frame approach first:
map_ng(data = dd, x = var, show.text = FALSE)
Note the following:
varis the name of the column of our data frame that has the variable to be mapped. It is found via the mechanism called quasiquotation; to explore this further see
varis actually a character vector, but internally is converted to a factor.
We can use the same data to make the same map, but with a different coding approach:
map_ng(region = ss, x = vv, col = "red", show.text = FALSE)
It’s exactly same map as before, but this time, we used 2 vectors to provide the data – one for the regions (in this case the 36 States and FCT) and another for the categorization. We have also introduced some colouring with the
col argument. Although it has the same data, if we try to use the column name from
dd, we have an error.
map_ng(region = ss, x = var) #> Error: One or more inputs for generating choropleth options are invalid
When the data to be categorized are numeric vectors, the categories have to be defined. To achieve this, the
breaks argument has to be supplied. This is a numeric vector that describes class limits for grouping the values. The number of classes is
length(breaks) - 1.
To get a more meaningful and interpretable legend, use the
Note that various colour arguments have been used, some of them kind of cryptic. More details on how to use the colouring schemes, check the help page of
To show the distribution of point data on any the maps such as GIS location data, a different set of arguments is employed. Since point data requires a pair of coordinates to be accurate, both the
y arguments MUST be provided. Once the function receives both coordinates, either as separate atomic vectors or as columns from a data frame,
map_ng defaults to the mapping of points only in within the boundaries indicated by
region. It looks like this:
map_ng carries out a bounds check on point data and returns an error, and the points are not plotted, when the coordinates are beyond the region. For instance, using the same points provided earlier, if we try to plot them on a map of Kwara State,
map_ng("Kwara", x = x, y = y) #> Error in map_ng("Kwara", x = x, y = y): Coordinates are out of bounds of the map