|
35 | 35 | #' @param varwidth if \code{FALSE} (default) make a standard box plot. If |
36 | 36 | #' \code{TRUE}, boxes are drawn with widths proportional to the |
37 | 37 | #' square-roots of the number of observations in the groups (possibly |
38 | | -#' weighted). |
| 38 | +#' weighted, using the \code{weight} aesthetic). |
39 | 39 | #' @export |
40 | 40 | #' |
41 | 41 | #' @references McGill, R., Tukey, J. W. and Larsen, W. A. (1978) Variations of |
@@ -126,23 +126,20 @@ GeomBoxplot <- proto(Geom, { |
126 | 126 | df$ymax_final <- pmax(out_max, df$ymax) |
127 | 127 | } |
128 | 128 |
|
129 | | - # if varwidth not requested or not available, don't use it |
130 | | - if(is.null(params) || is.null(params$varwidth) || !params$varwidth || is.null(df$relvarwidth)) { |
131 | | - if (is.null(df$relvarwidth)) { |
132 | | - transform(df, |
133 | | - xmin = x - width / 2, xmax = x + width / 2, width = NULL |
134 | | - ) |
135 | | - } else { |
136 | | - transform(df, |
137 | | - xmin = x - width / 2, xmax = x + width / 2, width = NULL, relvarwidth = NULL |
138 | | - ) |
139 | | - } |
| 129 | + # if `varwidth` not requested or not available, don't use it |
| 130 | + if (is.null(params) || is.null(params$varwidth) || !params$varwidth || is.null(df$relvarwidth)) { |
| 131 | + df$xmin <- df$x - df$width / 2 |
| 132 | + df$xmax <- df$x + df$width / 2 |
140 | 133 | } else { |
141 | | - transform(df, |
142 | | - xmin = x - relvarwidth * width / 2, xmax = x + relvarwidth * width / 2, width = NULL, relvarwidth = NULL |
143 | | - ) |
| 134 | + # make `relvarwidth` relative to the size of the largest group |
| 135 | + df$relvarwidth <- df$relvarwidth / max(df$relvarwidth) |
| 136 | + df$xmin <- df$x - df$relvarwidth * df$width / 2 |
| 137 | + df$xmax <- df$x + df$relvarwidth * df$width / 2 |
144 | 138 | } |
| 139 | + df$width <- NULL |
| 140 | + if (!is.null(df$relvarwidth)) df$relvarwidth <- NULL |
145 | 141 |
|
| 142 | + df |
146 | 143 | } |
147 | 144 |
|
148 | 145 | draw <- function(., data, ..., fatten = 2, outlier.colour = NULL, outlier.shape = NULL, outlier.size = 2, |
|
0 commit comments