-
Notifications
You must be signed in to change notification settings - Fork 97
/
Copy pathlinestack.R
64 lines (64 loc) · 1.96 KB
/
linestack.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
`linestack` <-
function (x, labels, cex = 0.8, side = "right", hoff = 2, air = 1.1,
at = 0, add = FALSE, axis = FALSE, ...)
{
if (length(at) > 1 || length(hoff) > 1 || length(air) > 1 || length(cex) > 1)
stop("only one value accepted for arguments 'cex', 'hoff', 'air' and 'at'")
x <- drop(x)
n <- length(x)
misslab <- missing(labels)
if (misslab) {
labels <- names(x)
}
if (!is.expression(labels) && !is.character(labels)) {
labels <- as.character(labels) # coerce to character only if not expressions
}
nlab <- length(labels)
if (!misslab && n != nlab) {
stop(gettextf(
"wrong number of supplied 'labels: expected %d, got %d", n, nlab))
}
side <- match.arg(side, c("right", "left"))
op <- par(xpd = TRUE)
on.exit(par(op))
ord <- order(x)
x <- x[ord]
labels <- labels[ord]
pos <- numeric(n)
if (!add) {
plot(pos, x, type = "n", axes = FALSE, xlab = "", ylab = "", ...)
}
hoff <- hoff * strwidth("m")
ht <- air * strheight(labels, cex = cex)
mid <- (n + 1)%/%2
pos[mid] <- x[mid]
if (n > 1) {
for (i in (mid + 1):n) {
pos[i] <- max(x[i], pos[i - 1] + ht[i])
}
}
if (n > 2) {
for (i in (mid - 1):1) {
pos[i] <- min(x[i], pos[i + 1] - ht[i])
}
}
segments(at, x[1], at, x[n])
## plot text in the original order for correct matching of vectors
## of graphical parameters (...).
o <- order(ord)
if (side == "right") {
text(at + hoff, pos[o], labels[o], pos = 4, cex = cex, offset = 0.2,
...)
segments(at, x, at + hoff, pos)
}
else if (side == "left") {
text(at - hoff, pos[o], labels[o], pos = 2, cex = cex, offset = 0.2,
...)
segments(at, x, at - hoff, pos)
}
if (axis)
axis(if (side == "right")
2
else 4, pos = at, las = 2)
invisible(pos[o])
}