panel.repellabels: Point labels for scatterplots (using ggrepel)

View source: R/panel_repellabels.R

panel.repellabelsR Documentation

Point labels for scatterplots (using ggrepel)

Description

Draw text labels for all points of a scatterplot using internal functionality from the ggrepel package. Note: an alternative panel function, panel.directlabels, carries most of the functionality of this function, but uses directlabels to calculate label positions instead. The same behavior can be achieved by using panel.directlabels with "ggrepel" for the positioning argument.

Usage

panel.repellabels(
  x,
  y,
  groups = NULL,
  subscripts = NULL,
  labels = NULL,
  col = NULL,
  cex = 0.8,
  x_boundary = NULL,
  y_boundary = NULL,
  selected = NULL,
  positioning = "ggrepel",
  method = directlabels::smart.grid,
  draw_text = TRUE,
  draw_line = TRUE,
  draw_box = FALSE,
  box_fill = grey(0.95),
  box_line = NULL,
  box_scale = NULL,
  ...
)

Arguments

x, y

(numeric) vectors representing x and y coordinates of points

groups

grouping variable passed down from xyplot (does not need to be specified)

subscripts

subscripts passed down from xyplot (does not need to be specified)

labels

(character) vector of labels to be plotted, if NULL, groups are used This resembles the behavior of the original directlabels functions

col

(character) color (vector) to be used for labels and lines. The default, NULL, uses colors supplied by the top level function.

cex

(numeric) size of text labels and corresponding boxes

x_boundary

(numeric) vector of length two, indicating the boundaries of x for which labels should be drawn (default: NULL)

y_boundary

(numeric) vector of length two, indicating the boundaries of y for which labels should be drawn (default: NULL)

selected

(logical) vector of the same length as input data, indicating which labels should be drawn and which not (default: NULL)

positioning

(character) One of "directlabels" or "ggrepel" (default), indicating the package used to calculate position of labels and bounding boxes

method

(list) the positioning method, default is directlabels::smart.grid

draw_text

(logical) whether to draw text labels or not (default: TRUE)

draw_line

(logical) whether to draw a line to labels or not (default: TRUE)

draw_box

(logical) whether to draw a box around labels or not (default: FALSE)

box_fill

(character) color of the box fill (default: light grey)

box_line

(character or logical) color of the box border. This parameters takes as input a color, a logical such as TRUE (uses colors supplied by top level function), or NULL (the default)

box_scale

(numeric) scalar indicating how much boxes surrounding labels should be expanded or shrunken, in fractions of current panel limits (e.g. 0.01 of X or Y range). Defaults to NULL

...

other arguments passed to the function

Details

By default, labels adapt the graphical parameters of the higher level plot, including coloring according to groups. However, many parameters can be customized.

Examples

library(grid)
library(lattice)

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

# A standard example using lattice grouping and paneling;
# We can also draw boxes around labels and change label size
xyplot(mpg ~ wt | factor(cyl), mtcars,
  groups = cyl, pch = 19, labels = mtcars$car,
  as.table = TRUE, layout = c(3, 1), cex = 0.6,
  panel = function(x, y, ...) {
    panel.xyplot(x, y, ...)
    panel.repellabels(x, y, draw_box = TRUE, box_line = TRUE, ...)
  }
)

# A similar plot with panels, but without grouping.
# This requires explicit use of subscripts
xyplot(mpg ~ wt | factor(cyl), mtcars,
  pch = 19, labels = mtcars$car,
  as.table = TRUE, layout = c(3, 1), cex = 0.6,
  panel = function(x, y, subscripts, ...) {
    panel.xyplot(x, y, ...)
    panel.repellabels(x, y, subscripts = subscripts, 
      draw_box = TRUE, box_fill = "white", ...)
  }
)

# An example without panels and more groups
xyplot(mpg ~ wt, mtcars,
  groups = hp, pch = 19, 
  labels = mtcars$wt, cex = 0.6,
  panel = function(x, y, ...) {
    panel.xyplot(x, y, ...)
    panel.repellabels(x, y, draw_box = TRUE, box_line = TRUE, ...)
  }
)


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