
Use ggrepel to create text annotations based on simple features
Source:R/layer_repel.R
layer_repel.RdUse ggrepel::geom_label_repel or ggrepel::geom_text_repel with
ggplot2::stat_sf_coordinates() to create a layer of textual annotations
repelled from simple feature locations.
Usage
layer_repel(
mapping = aes(),
data = NULL,
label_col = "name",
geom = c("text", "label"),
location_lims = NULL,
xlim = c(NA, NA),
ylim = c(NA, NA),
...
)
geom_sf_label_repel(mapping = aes(), data = NULL, label_col = "name", ...)
geom_sf_text_repel(mapping = aes(), data = NULL, label_col = "name", ...)Arguments
- mapping
Set of aesthetic mappings created by
aesoraes_. If specified andinherit.aes = TRUE(the default), is combined with the default mapping at the top level of the plot. You only need to supplymappingif there isn't a mapping defined for the plot.- data
A data frame. If specified, overrides the default data frame defined at the top level of the plot.
- label_col
Column name to use for label aesthetic mapping. Optional if label is provided to mapping; required otherwise.
- geom
Character vector with geom to use, "text" for
ggrepel::geom_text_repel()or "label" forggrepel::geom_label_repel().- location_lims
A
sf,sfc, orbboxobject to use in setting xlim and ylim values if no xlim and ylim value area provided. Using this parameter constrains labels to the bounding box oflocation_lims.- xlim, ylim
Limits for the x and y axes. Text labels will be constrained to these limits. By default, text labels are constrained to the entire plot area.
- ...
Arguments passed on to
ggrepel::geom_label_repelpositionPosition adjustment, either as a string, or the result of a call to a position adjustment function.
parseIf TRUE, the labels will be parsed into expressions and displayed as described in ?plotmath
box.paddingAmount of padding around bounding box, as unit or number. Defaults to 0.25. (Default unit is lines, but other units can be specified by passing
unit(x, "units")).label.paddingAmount of padding around label, as unit or number. Defaults to 0.25. (Default unit is lines, but other units can be specified by passing
unit(x, "units")).point.paddingAmount of padding around labeled point, as unit or number. Defaults to 0. (Default unit is lines, but other units can be specified by passing
unit(x, "units")).label.rRadius of rounded corners, as unit or number. Defaults to 0.15. (Default unit is lines, but other units can be specified by passing
unit(x, "units")).label.sizeSize of label border, in mm.
min.segment.lengthSkip drawing segments shorter than this, as unit or number. Defaults to 0.5. (Default unit is lines, but other units can be specified by passing
unit(x, "units")).arrowspecification for arrow heads, as created by
arrowforceForce of repulsion between overlapping text labels. Defaults to 1.
force_pullForce of attraction between a text label and its corresponding data point. Defaults to 1.
max.timeMaximum number of seconds to try to resolve overlaps. Defaults to 0.5.
max.iterMaximum number of iterations to try to resolve overlaps. Defaults to 10000.
max.overlapsExclude text labels when they overlap too many other things. For each text label, we count how many other text labels or other data points it overlaps, and exclude the text label if it has too many overlaps. Defaults to 10.
nudge_x,nudge_yHorizontal and vertical adjustments to nudge the starting position of each text label. The units for
nudge_xandnudge_yare the same as for the data units on the x-axis and y-axis.xlim,ylimLimits for the x and y axes. Text labels will be constrained to these limits. By default, text labels are constrained to the entire plot area.
na.rmIf
FALSE(the default), removes missing values with a warning. IfTRUEsilently removes missing values.show.legendlogical. Should this layer be included in the legends?
NA, the default, includes if any aesthetics are mapped.FALSEnever includes, andTRUEalways includes.direction"both", "x", or "y" – direction in which to adjust position of labels
seedRandom seed passed to
set.seed. Defaults toNA, which means thatset.seedwill not be called.verboseIf
TRUE, some diagnostics of the repel algorithm are printedinherit.aesIf
FALSE, overrides the default aesthetics, rather than combining with them. This is most useful for helper functions that define both data and aesthetics and shouldn't inherit behaviour from the default plot specification, e.g.borders.