Skip to content

Commit 768766f

Browse files
author
Lindsay Carr
committed
Merge pull request #305 from lindsaycarr/master
edit formals call for error_bar + handling multiple legend.names
2 parents 260ce9b + b3f566b commit 768766f

File tree

3 files changed

+74
-27
lines changed

3 files changed

+74
-27
lines changed

R/callouts.R

Lines changed: 51 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,12 @@ callouts.default <- function(x, y=NULL, labels=NA, length=0.1, angle='auto', cex
4949
return()
5050
}
5151

52-
stopifnot(angle=='auto' | (angle >= 0 & angle <= 360))
52+
stopifnot(all(angle=='auto' | is.na(angle) | (angle >= 0 & angle <= 360)))
5353

5454
x <- x[!is.na(labels)]
5555
y <- y[!is.na(labels)]
5656
labels <- labels[!is.na(labels)]
57+
if(length(angle) > 1){angle[!is.na(labels)]}
5758

5859
# // to do: possibly support angle and length as vectors equal in length to x
5960
x.usr <- par("usr")[c(1,2)]
@@ -66,30 +67,58 @@ callouts.default <- function(x, y=NULL, labels=NA, length=0.1, angle='auto', cex
6667
xrange <- diff(x.usr)
6768
yrange <- diff(y.usr)
6869

69-
if (angle != "auto") {
70-
x1 <- x + length * xrange * cos(2*pi*(angle/360))
71-
y1 <- y + length * yrange * sin(2*pi*(angle/360))
70+
calc_x1y1 <- function(x, y, angle, length, xrange, yrange, x.usr, y.usr){
71+
72+
if (is.na(angle) | angle == "auto") {
73+
auto.angle <- c(30, 330, 150, 210)
74+
x1 <- sapply(auto.angle, function(a) {
75+
x + length * xrange * cos(2*pi*(a/360))
76+
})
77+
y1 <- sapply(auto.angle, function(a) {
78+
y + length * yrange * sin(2*pi*(a/360))
79+
})
80+
81+
good.y1 <- y1 >= y.usr[1] & y1 <= y.usr[2]
82+
good.x1 <- x1 >= x.usr[1] & x1 <= x.usr[2]
83+
good.pt <- good.y1 & good.x1
84+
if (!is.null(dim(good.pt))){
85+
angle <- auto.angle[apply(good.pt, 1, function(z){
86+
ifelse(!any(z), 1, min(which(z)))
87+
})]
88+
} else {
89+
angle <- auto.angle[ifelse(!any(good.pt), 1, min(which(good.pt)))]
90+
}
91+
x1 <- x + length * xrange * cos(2*pi*(angle/360))
92+
y1 <- y + length * yrange * sin(2*pi*(angle/360))
93+
} else {
94+
x1 <- x + length * xrange * cos(2*pi*(angle/360))
95+
y1 <- y + length * yrange * sin(2*pi*(angle/360))
96+
}
97+
98+
return(list(x1 = x1, y1 = y1, angle = angle))
99+
}
100+
101+
if(length(angle) == 1){
102+
x1_y1_angle <- calc_x1y1(x = x, y = y, angle = angle,
103+
length, xrange, yrange, x.usr, y.usr)
104+
x1 <- x1_y1_angle$x1
105+
y1 <- x1_y1_angle$y1
106+
angle <- x1_y1_angle$angle
72107
} else {
73-
auto.angle <- c(30, 330, 150, 210)
74-
x1 <- sapply(auto.angle, function(a) {
75-
x + length * xrange * cos(2*pi*(a/360))
76-
})
77-
y1 <- sapply(auto.angle, function(a) {
78-
y + length * yrange * sin(2*pi*(a/360))
79-
})
108+
stopifnot(length(x)%%length(angle) == 0) #stop if the defined angles aren't a multiple of the points
109+
stopifnot(!is.character(angle)) #stop if it's not a numeric vector ('auto' is not used for the vector)
110+
111+
num_pts <- length(x)
112+
x1 <- vector(mode = "numeric", length = num_pts)
113+
y1 <- vector(mode = "numeric", length = num_pts)
80114

81-
good.y1 <- y1 >= y.usr[1] & y1 <= y.usr[2]
82-
good.x1 <- x1 >= x.usr[1] & x1 <= x.usr[2]
83-
good.pt <- good.y1 & good.x1
84-
if (!is.null(dim(good.pt))){
85-
angle <- auto.angle[apply(good.pt, 1, function(z){
86-
ifelse(!any(z), 1, min(which(z)))
87-
})]
88-
} else {
89-
angle <- auto.angle[ifelse(!any(good.pt), 1, min(which(good.pt)))]
115+
for(r in seq(num_pts)){
116+
x1_y1_angle <- calc_x1y1(x = x[r], y = y[r], angle = angle[r],
117+
length, xrange, yrange, x.usr, y.usr)
118+
x1[r] <- x1_y1_angle$x1
119+
y1[r] <- x1_y1_angle$y1
120+
angle[r] <- x1_y1_angle$angle
90121
}
91-
x1 <- x + length * xrange * cos(2*pi*(angle/360))
92-
y1 <- y + length * yrange * sin(2*pi*(angle/360))
93122
}
94123

95124
pos <- rep(1, length(angle))

R/config.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ config <- function(type, ..., persist=FALSE){
6969
mtext=names(formals(graphics::mtext)),
7070
grid=names(formals(graphics::grid)),
7171
segments=names(formals(graphics::segments)),
72-
error_bar=c('x', 'y', '...', 'y.high', 'y.low', 'x.high', 'x.low', 'epsilon'),
72+
error_bar=names(formals(error_bar.default)),
7373
bgCol=names(formals(bgCol.default)),
7474
callouts=names(formals(callouts.default)),
7575
rect=names(formals(graphics::rect)),

R/set_args.R

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,23 @@ set_legend_args <- function(object, fun.name, ..., legend.name) {
5454

5555
paramsAll <- set_args(fun.name, list(...))
5656

57+
if(length(legend.name) > 1){
58+
paramsAll_df <- as.data.frame(paramsAll, stringsAsFactors = FALSE)
59+
60+
for(p in seq(nrow(paramsAll_df))) {
61+
paramsAll_list <- as.list(paramsAll_df[p,])
62+
object <- get_legend_args(object, fun.name, paramsAll_list, legend.name[p])
63+
}
64+
65+
} else {
66+
object <- get_legend_args(object, fun.name, paramsAll, legend.name)
67+
}
68+
69+
return(object)
70+
}
71+
72+
get_legend_args <- function(object, fun.name, paramsAll, legend.name){
73+
5774
fun.default <- list(legend=legend.name,
5875
fill=quote(par("bg")),
5976
col=par("col"),
@@ -68,7 +85,7 @@ set_legend_args <- function(object, fun.name, ..., legend.name) {
6885
pt.lwd=NA,
6986
text.col=par("col"),
7087
text.font=1)
71-
88+
7289
type <- paramsAll[['type']]
7390
if(!is.null(type)){
7491
type.name <- switch(type, p='p', b='bo', o='bo', l='lchsS',
@@ -81,7 +98,7 @@ set_legend_args <- function(object, fun.name, ..., legend.name) {
8198
paramsAll <- set_type_params(paramsAll, type.name, params.needed)
8299
if(type.name %in% c('p', 'lchsS')) {fun.name <- switch(type.name, p="points", lchsS="lines")}
83100
}
84-
101+
85102
if (fun.name == "points") {
86103
pt.names <- c("lwd","bg","cex")
87104
names(paramsAll) <- replace(names(paramsAll), which(names(paramsAll) %in% pt.names),
@@ -91,7 +108,7 @@ set_legend_args <- function(object, fun.name, ..., legend.name) {
91108
pt.bg=quote(par("bg")),
92109
pt.cex=par("cex"),
93110
pt.lwd=par("lwd"))
94-
111+
95112
} else if (fun.name %in% c("lines", "abline", "arrows", "segments")) {
96113
fun.specific <- list(border=quote(par("bg")),
97114
lty=1,
@@ -108,13 +125,14 @@ set_legend_args <- function(object, fun.name, ..., legend.name) {
108125
fun.all <- replace(fun.default, match(names(fun.specific), names(fun.default)), fun.specific)
109126
add.args <- fun.all[!names(fun.all) %in% names(usr.args)]
110127
fun.legend.args <- append(usr.args, add.args)
111-
128+
112129
if(!is.character(fun.legend.args$lty)){
113130
lineTypes <- c("blank", "solid", "dashed", "dotted", "dotdash", "longdash", "twodash")
114131
fun.legend.args$lty <- lineTypes[fun.legend.args$lty + 1]
115132
}
116133

117134
object[['legend']] <- append(object[['legend']], list(legend.args=fun.legend.args))
135+
118136
return(object)
119137
}
120138

0 commit comments

Comments
 (0)