Skip to content

Commit 7182ad1

Browse files
committed
Merge pull request #227 from lindsaycarr/master
multiple callouts not broken
2 parents 48e57d9 + 8ec6b51 commit 7182ad1

File tree

2 files changed

+37
-37
lines changed

2 files changed

+37
-37
lines changed

DESCRIPTION

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: gsplot
22
Type: Package
33
Title: Geological Survey Plotting
4-
Version: 0.2.2
4+
Version: 0.2.3
55
Date: 2015-07-22
66
Authors@R: c( person("Jordan", "Read", role = "aut",
77
email = "jread@usgs.gov"),
@@ -13,7 +13,7 @@ Authors@R: c( person("Jordan", "Read", role = "aut",
1313
email = "thongsav@usgs.gov "),
1414
person("Megen","Hines", role="aut",
1515
email = "mhines@usgs.gov"),
16-
person("Lindsey","Carr", role="aut",
16+
person("Lindsay","Carr", role="aut",
1717
email = "lcarr@usgs.gov"))
1818
License: CC0
1919
Description: Workflow and plotting style defaults created for the U.S. Geological Survey.

R/callouts.R

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@ callouts.gsplot <- function(object, ..., side=c(1,2)){
4747
#' @export
4848
callouts.default <- function(x, y=NULL, labels=NA, length=0.1, angle='auto', ...){
4949

50+
if (is.null(y)) {
51+
warning("y=NULL not currently supported in callouts.default")
52+
return()
53+
}
54+
55+
stopifnot(angle=='auto' | (angle >= 0 & angle <= 360))
56+
5057
x <- x[!is.na(labels)]
5158
y <- y[!is.na(labels)]
5259
labels <- labels[!is.na(labels)]
@@ -62,44 +69,37 @@ callouts.default <- function(x, y=NULL, labels=NA, length=0.1, angle='auto', ...
6269
xrange <- diff(x.usr)
6370
yrange <- diff(y.usr)
6471

65-
try.angle <- function() {
66-
x1 = x + length * xrange * cos(2*pi*(angle/360));
67-
y1 = y + length * yrange * sin(2*pi*(angle/360));
68-
return(c(x1,y1))
69-
}
70-
71-
if (angle=='auto') {
72-
angle <- 30
73-
x.y <- try.angle()
74-
75-
if(x.y[2] > y.usr[2]){
76-
angle <- 330
77-
x.y <- try.angle()
78-
if(x.y[1] > x.usr[2]){angle <- 210}
79-
}
72+
if (angle != "auto") {
73+
x1 <- x + length * xrange * cos(2*pi*(angle/360))
74+
y1 <- y + length * yrange * sin(2*pi*(angle/360))
75+
} else {
76+
auto.angle <- c(30, 330, 150, 210)
77+
x1 <- sapply(auto.angle, function(a) {
78+
x + length * xrange * cos(2*pi*(a/360))
79+
})
80+
y1 <- sapply(auto.angle, function(a) {
81+
y + length * yrange * sin(2*pi*(a/360))
82+
})
8083

81-
if(x.y[1] > x.usr[2]) {
82-
angle <- 210
83-
x.y <- try.angle()
84-
if(x.y[2] < y.usr[1]){angle <- 150}
84+
good.y1 <- y1 >= y.usr[1] & y1 <= y.usr[2]
85+
good.x1 <- x1 >= x.usr[1] & x1 <= x.usr[2]
86+
good.pt <- good.y1 & good.x1
87+
if (!is.null(dim(good.pt))){
88+
angle <- auto.angle[apply(good.pt, 1, function(z){
89+
ifelse(!any(z), 1, min(which(z)))
90+
})]
91+
} else {
92+
angle <- auto.angle[ifelse(!any(good.pt), 1, min(which(good.pt)))]
8593
}
94+
x1 <- x + length * xrange * cos(2*pi*(angle/360))
95+
y1 <- y + length * yrange * sin(2*pi*(angle/360))
8696
}
87-
88-
stopifnot(angle >= 0, angle <= 360)
89-
x.y <- try.angle()
90-
x1 <- x.y[1]
91-
y1 <- x.y[2]
92-
93-
if (angle >= 315 | angle <= 45){
94-
pos = 4
95-
} else if (angle > 45 & angle <= 135) {
96-
pos = 3
97-
} else if (angle > 135 & angle <= 225){
98-
pos = 2
99-
} else {
100-
pos = 1
101-
}
102-
97+
98+
pos <- rep(1, length(angle))
99+
pos[angle >= 315 | angle <= 45] <- 4
100+
pos[angle > 45 & angle <= 135] <- 3
101+
pos[angle > 135 & angle <= 225] <- 2
102+
103103
segments(x0=x, y0=y, x1=x1, y1=y1, ...)
104104
text(x=x1, y=y1, labels=labels, pos=pos,...)
105105

0 commit comments

Comments
 (0)