addTextLabels: Add non-overlapping text labels to plot

Description Usage Arguments Examples

View source: R/addNonOverlappingTestLabelsOrPoints.R

Description

This function is similar to the text() function but it will attempt to re-locate labels that will overlap

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
addTextLabels(
  xCoords,
  yCoords,
  labels,
  cex.label = 1,
  col.label = "red",
  col.line = "black",
  col.background = NULL,
  lty = 1,
  lwd = 1,
  border = NA,
  avoidPoints = TRUE,
  keepLabelsInside = TRUE,
  cex.pt = 1
)

Arguments

xCoords

A vector containing the X coordinates for labels

yCoords

A vector containing the Y coordinates for labels

labels

A vector containing the labels to be plotted

cex.label

A number to scale the size of the plotted labels. Defaults to 1

col.label

The colour of the plotted labels. Defaults to "red". Multiple colours can be provided. If more colours than labels provided colours will be recycled.

col.line

The colour of the line to plot from relocated labels to original location. Defaults to "black". Multiple colours can be provided. If more colours than labels provided colours will be recycled.

col.background

An optional colour for a background polygon plotted behind labels. Defaults to NULL - won't be plotted. Multiple colours can be provided. If more colours than labels provided colours will be recycled.

lty

A number detailing the type of line to plot from relocated labels to original location. 0: blank, 1: solid, 2: dashed, 3: dotted, 4: dotdash, 5: longdash, and 6: twodash. Defaults to 1. Multiple line types can be provided. If more options than labels provided types will be recycled.

lwd

A number to scale the size of line from relocated labels to original location. Defaults to 1. Multiple line widths can be provided. If more options than labels provided widths will be recycled.

border

The colour of the border to be plotted around the polygon. Defaults to NA - won't be plotted. Multiple colours can be provided. If more colours than labels provided colours will be recycled.

avoidPoints

A logical variable indicating whether labels shouldn't be plotted on top of points. Defaults to TRUE

keepLabelsInside

A logical variable indicating whether the labels shouldn't be plotted outside of plotting region. Defaults to TRUE

cex.pt

A number used to scale the points plotted on the graph that labels are to be added to. Defaults to 1

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
# Create some random points
n <- 50
coords <- data.frame(X=runif(n), Y=runif(n), Name="Test Label")

# Plot them without labels
plot(x=coords$X, y=coords$Y, pch=19, bty="n", xaxt="n", yaxt="n", col="red", xlab="X", ylab="Y")

# With potentially overlapping labels
plot(x=coords$X, y=coords$Y, pch=19, bty="n", xaxt="n", yaxt="n", col="red", xlab="X", ylab="Y")
text(coords$X, coords$Y, labels=coords$Name, xpd=TRUE)

# Plot them with non-overlapping labels
plot(x=coords$X, y=coords$Y, pch=19, bty="n", xaxt="n", yaxt="n", col="red", xlab="X", ylab="Y")
addTextLabels(coords$X, coords$Y, coords$Name, cex.label=1, col.label="black")

# Plot them with non-overlapping labels
plot(x=coords$X, y=coords$Y, pch=19, bty="n", xaxt="n", yaxt="n", col="red", xlab="X", ylab="Y")
addTextLabels(coords$X, coords$Y, coords$Name, cex.label=1, col.background=rgb(0,0,0, 0.75), col.label="white")

JosephCrispell/plotteR documentation built on June 6, 2021, 7:24 p.m.