textrepel: textrepel - Plot text with non-overlapping labels

textrepelR Documentation

textrepel - Plot text with non-overlapping labels

Description

This function plots text with x and y coordinates, forcing overlapping labels to not overlap

Usage

textrepel(
  x,
  y,
  labels = NULL,
  padding = " ",
  rstep = 0.1,
  tstep = 0.1,
  vertical = FALSE,
  textSize = 1,
  showLines = TRUE,
  lineColor = "#00000066",
  lineWidth = 2,
  showPoints = TRUE,
  pointColor = "#00000033",
  pointSize = 2,
  pointPch = 16,
  add = FALSE,
  ...
)

Arguments

x

A numeric vector of x coordinates

y

A numeric vector of y coordinates (must have the same length of x)

labels

A vector of labels associated with x and y (must have the same length of x)

padding

A character object specifying left and right padding for words. Default is a single whitespace " "

rstep

Decimal numeric specifying the lateral step length for label distancing. Default is 0.1

tstep

Decimal numeric specifying the theta step length for label distancing. Default is 0.1

vertical

Boolean. If FALSE (default), the labels are plotted horizontally. If TRUE, vertically

textSize

Numeric. Size of text. Default is 1

showLines

Boolean. Whether to show lines connecting displaced labels to their original plot. Default is TRUE

lineColor

String indicating the color of the connecting line

lineWidth

Numeric indicating the width of the connecting line

showPoints

Boolean. Whether to show points over original x-y coordinates

pointColor

String indicating the color of the point

pointSize

Numeric indicating the size of the point

pointPch

Integer applying to shape of points. Default is 16 (filled circle)

add

Boolean. If FALSE (default), a new plot is generated. If TRUE, the textrepel labels are plotted over the existing plot

...

Arguments to be passed to the core _plot_ function

Value

A plot

Examples

# Simple example, generating a new plot, taking care of some overlapping labels
set.seed(1)
x<-rnorm(100)
y<-abs(x)+rnorm(100)
names(x)<-names(y)<-paste0("OBJ",1:length(x))
labels<-names(x)
textrepel(x,y,labels)
# More advanced example, adding textrepel over an existing plot
set.seed(1)
x<-rnorm(1000)
y<-abs(x)+rnorm(1000)
names(x)<-names(y)<-paste0("GENE",1:length(x))
labels<-names(x)
plot(x,y,pch=16,col="#00000066",xlim=1.3*c(min(x),max(x)))
subset1<-which(x<(-2.2))
textrepel(x[subset1],y[subset1],labels[subset1],add=TRUE,pointCol="cornflowerblue")
subset2<-which(x>(+2.2))
textrepel(x[subset2],y[subset2],labels[subset2],add=TRUE,pointCol="salmon")

federicogiorgi/corto documentation built on April 24, 2023, 1:23 a.m.