annotation_gspace_scalebar() draws a horizontal scale bar, with its
length as a label, using the unit set with gs_coords_unit.
Arguments
- x
Optional GraphSpace object with a unit. By default (
NULL), the unit is taken from the plot data, as set byggplot(gs), when the plot is drawn.- length
The length of the bar: a single named number, such as
c(um = 100)orc(km = 50), or aunitsobject. IfNULL(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". IfNULL(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,TRUEfor a semi-transparent white box, or anelement_rectto set itsfill,colour,linewidthandlinetype(usealphainfillfor transparency).- box.padding
Space between the box edges and the bar and label: a grid
unit, relative to the label size by default, as ingeom_label; a number is taken as millimeters.- box.r
Radius of the box corners: a grid
unit(or a number, in millimeters);0gives square corners.
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)))