Learn R Programming

ggsn (version 0.3.1)

north: North symbol

Description

Adds a north symbol to maps created with ggplot or ggmap.

Usage

north(data = NULL, location = "topright", scale = 0.1, symbol = 1, x.min, x.max, y.min, y.max, anchor = NULL)

Arguments

data
the same data.frame passed to ggplot to plot the map.
location
string indicating the symbol's location in the plot. Possible options: "topright" (default), "bottomright", "bottomleft" and "topleft".
scale
number between 0 and 1 to indicate the symbol size as a proportion of the map size (bounding box).
symbol
number between 1 and 18 to choose a symbol (see northSymbols).
x.min
if data is not defined, number with the minimum x coordinate. Useful for ggmap.
x.max
if data is not defined, number with the maximum x coordinate. Useful for ggmap.
y.min
if data is not defined, number with the minimum y coordinate. Useful for ggmap.
y.max
if data is not defined, number with the maximum y coordinate. Useful for ggmap.
anchor
named vector with coordinates to control the symbol position. For location = "topright", anchor defines the coordinates of the symbol's topright corner and so forth. The x coordinate must be named as x and the y coordinate as y.

Details

North symbols are included in the plot with the annotation_custom function, which do not works when used together with an empty call to ggplot (see last example). When it is convenient to use an empty call to ggplot, use north2 instead.

Examples

Run this code
library(rgdal); library(broom)
dsn <- system.file('extdata', package = 'ggsn')
map <- readOGR(dsn, 'sp')
map@data$id <- 1:nrow(map@data)
map.df <- merge(tidy(map), map, by = 'id')

ggplot(map.df, aes(long, lat, group = group, fill = nots)) +
    geom_polygon() +
    coord_equal() +
    geom_path() +
    north(map.df) +
    scale_fill_brewer(name = 'Animal abuse\nnotifications', palette = 8)

ggplot(data = map.df, aes(long, lat, group = group, fill = nots)) +
    geom_polygon() +
    coord_equal() +
    geom_path() +
    north(map.df, location = 'bottomleft', symbol = 8) +
    scale_fill_brewer(name = 'Animal abuse\nnotifications', palette = 8)

ggplot(map.df, aes(long, lat, group = group, fill = nots)) +
    geom_polygon() +
    coord_equal() +
    geom_path() +
    north(data = map.df, location = 'bottomright', scale = 0.2, symbol = 14,
          anchor = c(x = -46.4, y = -23.9)) +
    scale_fill_brewer(name = 'Animal abuse\nnotifications', palette = 8)

## Not run: 
# ggplot() +
#    geom_polygon(data = map.df, aes(long, lat, group = group, fill = nots)) +
#    coord_equal() +
#    geom_path() +
#    north(map.df)
# ## End(Not run)

Run the code above in your browser using DataLab