Learn R Programming

ggsn (version 0.5.0)

scalebar: scalebar

Description

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

Usage

scalebar(data = NULL, location = "bottomright", dist = NULL,
  dist_unit = NULL, transform = NULL, dd2km = NULL, model = NULL,
  height = 0.02, st.dist = 0.02, st.bottom = TRUE, st.size = 5,
  st.color = "black", box.fill = c("black", "white"),
  box.color = "black", border.size = 1, x.min = NULL, x.max = NULL,
  y.min = NULL, y.max = NULL, 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 scale bar's location in the plot. Possible options: "topright" (default), "bottomright", "bottomleft" and "topleft".

dist

distance to represent with each segment of the scale bar.

dist_unit

unit of measurement for dist. Possbile values: "km" (kilometers) and "m" (meters), "nm" (nautical miles) and "mi" (statue miles).

transform

If TRUE, it is assumed that coordinates are in decimal degrees. If FALSE, it assumed that they are in meters.

dd2km

deprecated. Use transform instead.

model

choice of ellipsoid model ("WGS84", "GRS80", "Airy", "International", "Clarke", or "GRS67") Used when transform is TRUE.

height

number between 0 and 1 to indicate the scale bar's height, 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's text, as a proportion of the y axis.

st.bottom

logical. If TRUE (default) the scale bar's 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's size. It is passed to size in annotate function.

st.color

color of the scale bar's text. Default is black.

box.fill

fill color of the box. If vector of two colors, the two boxes are filled with a different color. Defaults to black and white.

box.color

color of the box's border. If vector of two colors, the borders of the two boxes are colored differently. Defaults to black.

border.size

number to define the border size.

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's 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, character vector of variable names used for faceting. This is useful for placing the scalebar in only one facet and must be used together with facet.lev.

facet.lev

character vector with the name of one level for each variable in facet.var. The scale bar will be drawn only in the facet.lev facet.

...

further arguments passed to geom_text.

Examples

Run this code
# NOT RUN {
library(sf)
data(domestic_violence)

# Map in geographic coordinates
ggplot(domestic_violence, aes(fill = Scaled)) +
    geom_sf() +
    scalebar(domestic_violence, dist = 4, dist_unit = "km",
             transform = TRUE, model = "WGS84") +
    blank() +
    scale_fill_continuous(low = "#fff7ec", high = "#7F0000")

# Map in projected coordinates
domestic_violence2 <- st_transform(domestic_violence, 31983)
ggplot(domestic_violence2, aes(fill = Scaled)) +
    geom_sf() +
    scalebar(domestic_violence2, dist = 4, dist_unit = "km",
             transform = FALSE, model = "WGS84") +
    blank() +
    scale_fill_continuous(low = "#fff7ec", high = "#7F0000")
# }

Run the code above in your browser using DataLab