scalebar: scalebar

Description Usage Arguments Examples

View source: R/scalebar.R

Description

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

Usage

1
2
3
4
5
6
7
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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
## 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")

## End(Not run)

Example output

Loading required package: ggplot2
Loading required package: grid
Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1

ggsn documentation built on May 1, 2019, 8:21 p.m.