Learn R Programming

ggsn (version 0.3.1)

scalebar: Scale bar

Description

Adds a scale bar to maps created with ggplot or ggmap.

Usage

scalebar(data = NULL, location = "bottomright", dist, height = 0.02, st.dist = 0.02, st.bottom = TRUE, st.size = 5, dd2km = NULL, model, x.min, x.max, y.min, y.max, anchor = NULL, facet.var = NULL, facet.lev = 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".
dist
distance in km to represent with each segment of the scale bar.
height
number between 0 and 1 to indicate the height of the scale bar, as a proportion of the y axis.
st.dist
number between 0 and 1 to indicate the distance between the scale bar and the scale bar text, as a proportion of the y axis.
st.bottom
logical. If TRUE (default) the scale bar text is displayed at the bottom of the scale bar, if FALSE, it is displayed at the top.
st.size
number to indicate the scale bar text size. It is passed to the size argument of annotate function.
dd2km
logical. If TRUE dist it is assumed that map coordinates are in decimal degrees, if FALSE, it assumed they are in meters.
model
choice of ellipsoid model ("WGS84", "GRS80", "Airy", "International", "Clarke", "GRS67") Used when dd2km is TRUE.
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.
facet.var
if faceting, variable name used for faceting. This is useful for placing the scalebar only in one facet and must be used together with facet.lev.
facet.lev
one of the level names in facet.var. The scale bar will be drawn only in the facet.lev facet.

Examples

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

ggplot(data = map.df, aes(long, lat, group = group, fill = nots)) +
    geom_polygon() +
    coord_equal() +
    geom_path() +
    scale_fill_brewer(name = 'Animal abuse\nnotifications', palette = 8) +
    scalebar(map.df, dist = 5, dd2km = TRUE, model = 'WGS84')

Run the code above in your browser using DataLab