panel.directlabels: Point labels for scatterplots (using directlabels)

View source: R/panel_directlabels.R

panel.directlabelsR Documentation

Point labels for scatterplots (using directlabels)

Description

Draw text labels for all points of a scatterplot using internal functionality from the directlabels package. Note: an alternative panel function, panel.repellabels, is a wrapper using ggrepel calculated label positions instead. The same behavior can be achieved by using "ggrepel" for the positioning argument.

Usage

panel.directlabels(
  x,
  y,
  groups = NULL,
  subscripts = NULL,
  labels = NULL,
  col = NULL,
  cex = 0.8,
  x_boundary = NULL,
  y_boundary = NULL,
  selected = NULL,
  positioning = "directlabels",
  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" (default) or "ggrepel", 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

In contrast to the functionality of the original directlabels package, every point is labelled instead of groups of points. Labels are also independent from the grouping variable, so that e.g. colors indicate one grouping variable and labels another. 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.directlabels(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.directlabels(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.directlabels(x, y, draw_box = TRUE, box_line = TRUE, ...)
  }
)


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