offsetGridText: Insert offset text annotation into 'trellis' plot

Description Usage Arguments Author(s) See Also Examples

Description

This is a wrapper function around Orcs:::calcOffsetGridText and grid-based text drawing functions (currently including grid.text and grid.stext) that automatically adds offset text annotations to a 'trellis' plot.

Usage

1
2
offsetGridText(x, y = NULL, labels, xlim = NULL, ylim = NULL,
  pos = NULL, stext = FALSE, offset = 0.02, ...)

Arguments

x

A numeric vector containing x coordinates, or a 2-column matrix containing x and y coordinates.

y

A numeric vector containing y coordinates, or NULL if 'x' is a two-column matrix.

labels

The text to be written as character.

xlim, ylim

X and Y-axis limits (c(min, max)) of the current plot. If not supplied, limits are automatically calculated from supplied x and y coordinates.

pos

Text position specifier(s) as integer used by text. If not supplied, optimal text positions will be determined with respect to neighboring locations using thigmophobe.

stext

logical, defaults to FALSE. If TRUE, shadow text will be drawn around 'labels'.

offset

A numeric offset in normalized parent coordinates ("npc", see unit).

...

Additional arguments passed to the respective grid text drawing function (depends on 'stext').

Author(s)

Florian Detsch

See Also

grid.text, grid.stext, thigmophobe, Orcs::calcOffsetGridText.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
stopifnot(
  require(sf)
  , require(latticeExtra)
  , require(grid)
)

# kilimanjaro peaks
peaks = data.frame(Peak = c("Kibo", "Mawenzi", "Shira")
                   , Lon = c(37.359031, 37.455061, 37.210408)
                   , Lat = c(-3.065053, -3.095436, -3.038222))

coordinates(peaks) = ~ Lon + Lat
proj4string(peaks) = "+init=epsg:4326"

# visualization
xlim_kili <- c(37.15, 37.55)
ylim_kili <- c(-3.25, -2.9)

p = spplot(KiLi[[1]], col.regions = "transparent", colorkey = FALSE, 
           xlim = xlim_kili, ylim = ylim_kili,
           scales = list(draw = TRUE, y = list(rot = 90)), 
           sp.layout = rgb2spLayout(KiLi, quantiles = c(0, 1), alpha = .8)) + 
  layer(sp.points(peaks, cex = 1.5, pch = 20, col = "black"))

print(p)

downViewport(trellis.vpname(name = "figure"))
offsetGridText(x = coordinates(peaks), labels = peaks$Peak,  
               xlim = xlim_kili, ylim = ylim_kili, stext = TRUE, offset = .02,
               gp = gpar(fontsize = 16))
                               

Orcs documentation built on May 2, 2019, 2:37 a.m.