|
8 | 8 | #' @details Additional graphical parameter inputs: |
9 | 9 | #' \itemize{ |
10 | 10 | #' \item{\code{x, y}} {location of error_bar origin} |
11 | | -#' \item{\code{y.high, y.low}} {the y-value specifying the error above the point (high) and below the point (low)} |
12 | | -#' \item{\code{x.high, x.low}} {the x-value specifying the error above the point (high) and below the point (low)} |
| 11 | +#' \item{\code{offset.up, offset.down}} {the y-value specifying the error above the point (high) and below the point (low)} |
| 12 | +#' \item{\code{offset.right, offset.left}} {the x-value specifying the error above the point (high) and below the point (low)} |
13 | 13 | #' \item{\code{epsilon}} {width of the end of the error bar (in inches)} |
14 | 14 | #' \item{\code{legend.name}} {name that appears in the legend, see \code{\link{legend}} for more legend parameters} |
15 | 15 | #' } |
|
24 | 24 | #' gsNew <- abline(gsNew, b=1, a=0, legend.name="1:1") |
25 | 25 | #' gsNew <- legend(gsNew, location = "topleft",title="Awesome!") |
26 | 26 | #' gsNew <- grid(gsNew) |
27 | | -#' gsNew <- error_bar(gsNew, 1:3, y=c(3,1,2), y.high=c(0.5,0.25,1), y.low=0.1) |
28 | | -#' gsNew <- error_bar(gsNew, x=1:3, y=c(3,1,2), x.low=c(.2,NA,.2), x.high=.2, col="red",lwd=3) |
| 27 | +#' gsNew <- error_bar(gsNew, 1:3, y=c(3,1,2), |
| 28 | +#' offset.up=c(0.5,0.25,1), offset.down=0.1) |
| 29 | +#' gsNew <- error_bar(gsNew, x=1:3, y=c(3,1,2), |
| 30 | +#' offset.left=c(.2,NA,.2), |
| 31 | +#' offset.right=.2, col="red",lwd=3) |
29 | 32 | #' gsNew <- title(gsNew, "Graphing Fun") |
30 | 33 | #' gsNew |
31 | 34 | #' |
32 | 35 | #' yData <- rnorm(100,mean=10000, sd=1000) |
33 | 36 | #' gs <- gsplot() %>% |
34 | 37 | #' points(1:100, yData, log="y") %>% |
35 | | -#' error_bar(50:60, yData[50:60], y.high=250) |
| 38 | +#' error_bar(50:60, yData[50:60], offset.up=250) |
36 | 39 | #' gs |
37 | 40 | #' |
38 | 41 | #' gs <- gsplot() %>% |
39 | 42 | #' points(1:10, 1:10) %>% |
40 | | -#' error_bar(5, 5, y.high=1, col="green") |
| 43 | +#' error_bar(5, 5, offset.up=1, col="green") |
41 | 44 | #' gs |
42 | 45 | error_bar <- function(object, ...) { |
43 | 46 | override("gsplot", "error_bar", object, ...) |
@@ -67,101 +70,101 @@ error_bar.gsplot <- function(object, ..., legend.name=NULL, side=c(1,2)){ |
67 | 70 | #' |
68 | 71 | #' @param x location in x |
69 | 72 | #' @param y location in y |
70 | | -#' @param y.high offset up |
71 | | -#' @param y.low offset down |
72 | | -#' @param x.high offset right |
73 | | -#' @param x.low offset left |
| 73 | +#' @param offset.up offset up |
| 74 | +#' @param offset.down offset down |
| 75 | +#' @param offset.right offset right |
| 76 | +#' @param offset.left offset left |
74 | 77 | #' @param epsilon width of bar in relative units |
75 | 78 | #' @param \dots additional arguments passed to \code{\link[graphics]{arrows}} |
76 | 79 | #' @export |
77 | 80 | #' @examples |
78 | 81 | #' plot(1:10, 1:10) |
79 | | -#' error_bar(5, 5, y.high=1, col="green") |
80 | | -error_bar.default <- function(x, y, y.high=0, y.low=0, x.high=0, x.low=0, epsilon=0.1, ...){ |
| 82 | +#' error_bar(5, 5, offset.up=1, col="green") |
| 83 | +error_bar.default <- function(x, y, offset.up=0, offset.down=0, offset.right=0, offset.left=0, epsilon=0.1, ...){ |
81 | 84 |
|
82 | | - data.list <- calculate_error_bars(x=x,y=y,y.high=y.high,y.low=y.low,x.high=x.high, x.low=x.low, epsilon=epsilon) |
| 85 | + data.list <- calculate_error_bars(x=x,y=y,offset.up=offset.up,offset.down=offset.down,offset.right=offset.right, offset.left=offset.left, epsilon=epsilon) |
83 | 86 |
|
84 | | - if(length(data.list[["y.low"]]) > 1){ |
85 | | - graphics::arrows(x0=data.list[["y.low"]]$x0, |
86 | | - y0=data.list[["y.low"]]$y0, |
87 | | - x1=data.list[["y.low"]]$x1, |
88 | | - y1=data.list[["y.low"]]$y1, |
89 | | - length=data.list[["y.low"]]$length, angle=90, ...) |
| 87 | + if(length(data.list[["offset.down"]]) > 1){ |
| 88 | + graphics::arrows(x0=data.list[["offset.down"]]$x0, |
| 89 | + y0=data.list[["offset.down"]]$y0, |
| 90 | + x1=data.list[["offset.down"]]$x1, |
| 91 | + y1=data.list[["offset.down"]]$y1, |
| 92 | + length=data.list[["offset.down"]]$length, angle=90, ...) |
90 | 93 | } |
91 | 94 |
|
92 | | - if(length(data.list[["y.high"]]) > 1){ |
93 | | - graphics::arrows(x0=data.list[["y.high"]]$x0, |
94 | | - y0=data.list[["y.high"]]$y0, |
95 | | - x1=data.list[["y.high"]]$x1, |
96 | | - y1=data.list[["y.high"]]$y1, |
97 | | - length=data.list[["y.high"]]$length, angle=90, ...) |
| 95 | + if(length(data.list[["offset.up"]]) > 1){ |
| 96 | + graphics::arrows(x0=data.list[["offset.up"]]$x0, |
| 97 | + y0=data.list[["offset.up"]]$y0, |
| 98 | + x1=data.list[["offset.up"]]$x1, |
| 99 | + y1=data.list[["offset.up"]]$y1, |
| 100 | + length=data.list[["offset.up"]]$length, angle=90, ...) |
98 | 101 | } |
99 | 102 |
|
100 | | - if(length(data.list[["x.low"]]) > 1){ |
101 | | - graphics::arrows(x0=data.list[["x.low"]]$x0, |
102 | | - y0=data.list[["x.low"]]$y0, |
103 | | - x1=data.list[["x.low"]]$x1, |
104 | | - y1=data.list[["x.low"]]$y1, |
105 | | - length=data.list[["x.low"]]$length, angle=90, ...) |
| 103 | + if(length(data.list[["offset.left"]]) > 1){ |
| 104 | + graphics::arrows(x0=data.list[["offset.left"]]$x0, |
| 105 | + y0=data.list[["offset.left"]]$y0, |
| 106 | + x1=data.list[["offset.left"]]$x1, |
| 107 | + y1=data.list[["offset.left"]]$y1, |
| 108 | + length=data.list[["offset.left"]]$length, angle=90, ...) |
106 | 109 | } |
107 | 110 |
|
108 | | - if(length(data.list[["x.high"]]) > 1){ |
109 | | - graphics::arrows(x0=data.list[["x.high"]]$x0, |
110 | | - y0=data.list[["x.high"]]$y0, |
111 | | - x1=data.list[["x.high"]]$x1, |
112 | | - y1=data.list[["x.high"]]$y1, |
113 | | - length=data.list[["x.high"]]$length, angle=90, ...) |
| 111 | + if(length(data.list[["offset.right"]]) > 1){ |
| 112 | + graphics::arrows(x0=data.list[["offset.right"]]$x0, |
| 113 | + y0=data.list[["offset.right"]]$y0, |
| 114 | + x1=data.list[["offset.right"]]$x1, |
| 115 | + y1=data.list[["offset.right"]]$y1, |
| 116 | + length=data.list[["offset.right"]]$length, angle=90, ...) |
114 | 117 | } |
115 | 118 |
|
116 | 119 | return() |
117 | 120 | } |
118 | 121 |
|
119 | | -calculate_error_bars <- function(x, y, y.high=0, y.low=0, x.high=0, x.low=0, epsilon=0.1, ...){ |
120 | | - y.high[is.na(y.high)] <- 0 |
121 | | - y.low[is.na(y.low)] <- 0 |
122 | | - x.high[is.na(x.high)] <- 0 |
123 | | - x.low[is.na(x.low)] <- 0 |
| 122 | +calculate_error_bars <- function(x, y, offset.up=0, offset.down=0, offset.right=0, offset.left=0, epsilon=0.1, ...){ |
| 123 | + offset.up[is.na(offset.up)] <- 0 |
| 124 | + offset.down[is.na(offset.down)] <- 0 |
| 125 | + offset.right[is.na(offset.right)] <- 0 |
| 126 | + offset.left[is.na(offset.left)] <- 0 |
124 | 127 |
|
125 | 128 | data.list <- rep(list(list()), 4) |
126 | | - names(data.list) <- c("y.low","y.high","x.low","x.high") |
| 129 | + names(data.list) <- c("offset.down","offset.up","offset.left","offset.right") |
127 | 130 |
|
128 | | - if(!all(y.low == 0)){ |
129 | | - y.low.coord <- y-y.low |
130 | | - errorIndex <- (y-y.low.coord) != 0 |
131 | | - y.low.coord <- y.low.coord[errorIndex] |
| 131 | + if(!all(offset.down == 0)){ |
| 132 | + offset.down.coord <- y-offset.down |
| 133 | + errorIndex <- (y-offset.down.coord) != 0 |
| 134 | + offset.down.coord <- offset.down.coord[errorIndex] |
132 | 135 | y.error <- y[errorIndex] |
133 | 136 | x.error <- x[errorIndex] |
134 | | - data.list[["y.low"]] <- list(x0=x.error, y0=y.error, x1=x.error, y1=y.low.coord, |
| 137 | + data.list[["offset.down"]] <- list(x0=x.error, y0=y.error, x1=x.error, y1=offset.down.coord, |
135 | 138 | length=epsilon, angle=90) |
136 | 139 | } |
137 | 140 |
|
138 | | - if(!all(y.high == 0)){ |
139 | | - y.high.coord <- y+y.high |
140 | | - errorIndex <- (y-y.high.coord) != 0 |
141 | | - y.high.coord <- y.high.coord[errorIndex] |
| 141 | + if(!all(offset.up == 0)){ |
| 142 | + offset.up.coord <- y+offset.up |
| 143 | + errorIndex <- (y-offset.up.coord) != 0 |
| 144 | + offset.up.coord <- offset.up.coord[errorIndex] |
142 | 145 | y.error <- y[errorIndex] |
143 | 146 | x.error <- x[errorIndex] |
144 | | - data.list[["y.high"]] <- list(x0=x.error, y0=y.error, x1=x.error, y1=y.high.coord, length=epsilon, |
| 147 | + data.list[["offset.up"]] <- list(x0=x.error, y0=y.error, x1=x.error, y1=offset.up.coord, length=epsilon, |
145 | 148 | angle=90) |
146 | 149 | } |
147 | 150 |
|
148 | | - if(!all(x.low == 0)){ |
149 | | - x.low.coord <- x-x.low |
150 | | - errorIndex <- (x-x.low.coord) != 0 |
151 | | - x.low.coord <- x.low.coord[errorIndex] |
| 151 | + if(!all(offset.left == 0)){ |
| 152 | + offset.left.coord <- x-offset.left |
| 153 | + errorIndex <- (x-offset.left.coord) != 0 |
| 154 | + offset.left.coord <- offset.left.coord[errorIndex] |
152 | 155 | x.error <- x[errorIndex] |
153 | 156 | y.error <- y[errorIndex] |
154 | | - data.list[["x.low"]] <- list(x0=x.error, y0=y.error, x1=x.low.coord, y1=y.error, length=epsilon, |
| 157 | + data.list[["offset.left"]] <- list(x0=x.error, y0=y.error, x1=offset.left.coord, y1=y.error, length=epsilon, |
155 | 158 | angle=90) |
156 | 159 | } |
157 | 160 |
|
158 | | - if(!all(x.high == 0)){ |
159 | | - x.high.coord <- x+x.high |
160 | | - errorIndex <- (x-x.high.coord) != 0 |
161 | | - x.high.coord <- x.high.coord[errorIndex] |
| 161 | + if(!all(offset.right == 0)){ |
| 162 | + offset.right.coord <- x+offset.right |
| 163 | + errorIndex <- (x-offset.right.coord) != 0 |
| 164 | + offset.right.coord <- offset.right.coord[errorIndex] |
162 | 165 | x.error <- x[errorIndex] |
163 | 166 | y.error <- y[errorIndex] |
164 | | - data.list[["x.high"]] <- list(x0=x.error, y0=y.error, x1=x.high.coord, y1=y.error, length=epsilon, |
| 167 | + data.list[["offset.right"]] <- list(x0=x.error, y0=y.error, x1=offset.right.coord, y1=y.error, length=epsilon, |
165 | 168 | angle=90) |
166 | 169 | } |
167 | 170 |
|
|
0 commit comments