Skip to contents

annotation_gspace_scalebar() draws a horizontal scale bar, with its length as a label, using the unit set with gs_coords_unit.

Usage

annotation_gspace_scalebar(
  x = NULL,
  length = NULL,
  unit = NULL,
  position = c("topleft", "topright", "bottomleft", "bottomright"),
  colour = "black",
  linewidth = 1,
  label.size = 3,
  inset = 0.07,
  box = FALSE,
  box.padding = grid::unit(0.25, "lines"),
  box.r = grid::unit(0.15, "lines")
)

Arguments

x

Optional GraphSpace object with a unit. By default (NULL), the unit is taken from the plot data, as set by ggplot(gs), when the plot is drawn.

length

The length of the bar: a single named number, such as c(um = 100) or c(km = 50), or a units object. If NULL (default), the round length (1, 2 or 5 times a power of ten) closest to a fifth of the plot width is chosen.

unit

Unit of the label, such as "km" or "mi". If NULL (default), a readable metric unit is chosen. Units other than the built-in metric ones require the units package.

position

Corner of the plot: "topleft" (default), "topright", "bottomleft" or "bottomright".

colour

Colour of the bar and its label.

linewidth

Width of the bar.

label.size

Size of the label text.

inset

Distance from the plot edges, as a fraction of the plot width and height: one number for both directions, or two numbers for the horizontal and vertical distances.

box

A background box behind the bar and its label, to improve visibility over complex backgrounds: FALSE (default) for no box, TRUE for a semi-transparent white box, or an element_rect to set its fill, colour, linewidth and linetype (use alpha in fill for transparency).

box.padding

Space between the box edges and the bar and label: a grid unit, relative to the label size by default, as in geom_label; a number is taken as millimeters.

box.r

Radius of the box corners: a grid unit (or a number, in millimeters); 0 gives square corners.

Value

A 'ggplot2' layer, to add to a plot.

Details

By default, the scale bar uses the unit of the plot data, as in ggplot(gs) + .... Alternatively, the unit can be taken from x, e.g. for plots without GraphSpace plot data, such as those of plotGraphSpace.

Examples

library(RGraphSpace)
data("gtoy1", package = "RGraphSpace")
gs <- GraphSpace(gtoy1)
#> Validating the 'igraph' object...
#> Ignoring graph-level attributes: 'name', 'mode', 'center'
#> Creating a 'GraphSpace' object...
gs_coords_unit(gs) <- c(um = 10)
gs <- normalizeGraphSpace(gs)
#> Normalizing node coordinates to graph space...

# A default scale bar, describing the plot data
ggplot(gs) + geom_edgespace() + geom_nodespace() +
  annotation_gspace_scalebar()


# Adjust the scale bar length and position
ggplot(gs) + geom_edgespace() + geom_nodespace() +
  annotation_gspace_scalebar(length = c(um = 30), 
  position = "bottomright")


# Include a background box, for complex backgrounds
ggplot(gs) + geom_edgespace() + geom_nodespace() +
  annotation_gspace_scalebar(box = TRUE)


# Plots without GraphSpace plot data need the object
plotGraphSpace(gs) + 
annotation_gspace_scalebar(gs, colour = "white",
  box = element_rect(fill = ggplot2::alpha("black", 0.5)))