get_repelled_labels: Get bounding boxes for repelled text labels

View source: R/get_repelled_labels.R

get_repelled_labelsR Documentation

Get bounding boxes for repelled text labels

Description

This function uses repel_boxes() from the 'ggrepel' package to obtain coordinates and bounding boxes for repelled text labels. Internally, all units are converted to relative plot coordinates ('npc', between 0 and 1) and before evoking the C++ function repel_boxes() to carry out the repelling placement. This function is only intended for internal use, but also works for any type of grid or base graphics plot, as long as a graphics device is open, see examples.

Usage

get_repelled_labels(
  x,
  y,
  labels,
  cex = 1,
  xlim = c(0, 1),
  ylim = c(0, 1),
  point_size = 0.001,
  box_padding_x = 0.005,
  box_padding_y = 0.005,
  point_padding_x = 0.01,
  point_padding_y = 0.01,
  force_push = 1e-05,
  force_pull = 1e-05,
  transform_coord = "npc",
  ...
)

Arguments

x, y

(numeric) vectors representing x and y coordinates of points

labels

(character) vector of labels

cex

(numeric) size of text labels

xlim

numeric vector representing the limits on the x axis like c(xmin, xmax)

ylim

numeric vector representing the limits on the y axis like c(ymin, ymax) ‘force_push’ ‘force_pull’ ‘transform_coord’

point_size

(numeric) approximate size of points in npc relative cooordinates. Can be small, is used in conjunction with "point_padding" options

box_padding_x

(numeric) extra margin around text bounding boxes

box_padding_y

(numeric) extra margin around text bounding boxes

point_padding_x

(numeric) extra margin around data points

point_padding_y

(numeric) extra margin around data points

force_push

(numeric) magnitude of the push force (defaults to 1e-5)

force_pull

(numeric) magnitude of the pull force (defaults to 1e-5)

transform_coord

(character) the coordinate system used for calculating bounding boxes. The default, "npc", is to transfrom all cordinates to 'Normalised Parent Coordinates'. When calling externally, use "native" instead, see examples.

...

other arguments passed to the function

Examples

data("mtcars")
mtcars$car <- rownames(mtcars)

# with base graphics
# ---------------------------
plot(mtcars$mpg ~ mtcars$wt, pch = 19)

coords <- get_repelled_labels(
  x = mtcars$wt, y = mtcars$mpg, 
  labels = mtcars$car,
  cex = 0.7, point_size = 0.01,
  xlim = range(mtcars$wt),
  ylim = range(mtcars$mpg),
  box_padding_x = 0.05,
  box_padding_y = 0.05,
  point_padding_x = 0.01,
  point_padding_y = 0.2,
  force_push = 1e-05,
  force_pull = 1e-05,
  transform_coord = "native"
)

with(coords, rect(x1_box, y1_box, x2_box, y2_box, col = "white"))
with(coords, text(x, y, labels = label, cex = 0.7))

# with ggplot2
# ---------------------------

## Not run: 
library(ggplot2)
ggplot(mtcars, aes(y = mpg, x = wt)) +
  geom_point() +
  geom_label(aes(x = coords$x, y = coords$y, label = coords$label))

## End(Not run)


m-jahn/lattice-tools documentation built on Dec. 3, 2023, 11:14 p.m.