3232# ' the medians differ.
3333# ' @param notchwidth for a notched box plot, width of the notch relative to
3434# ' the body (default 0.5)
35+ # ' @param varwidth if \code{FALSE} (default) make a standard box plot. If
36+ # ' \code{TRUE}, boxes are drawn with widths proportional to the
37+ # ' square-roots of the number of observations in the groups (possibly
38+ # ' weighted).
3539# ' @export
3640# '
3741# ' @references McGill, R., Tukey, J. W. and Larsen, W. A. (1978) Variations of
9296# ' b + geom_boxplot(stat = "identity")
9397# ' b + geom_boxplot(stat = "identity") + coord_flip()
9498# ' b + geom_boxplot(aes(fill = X1), stat = "identity")
99+ # '
100+ # ' # Using varwidth
101+ # ' p + geom_boxplot(varwidth = TRUE)
102+ # ' qplot(factor(cyl), mpg, data = mtcars, geom = "boxplot", varwidth = TRUE)
95103# ' }
96104geom_boxplot <- function (mapping = NULL , data = NULL , stat = " boxplot" , position = " dodge" ,
97105outlier.colour = " black" , outlier.shape = 16 , outlier.size = 2 ,
98- notch = FALSE , notchwidth = .5 , ... ) {
106+ notch = FALSE , notchwidth = .5 , varwidth = FALSE , ... ) {
99107 GeomBoxplot $ new(mapping = mapping , data = data , stat = stat ,
100108 position = position , outlier.colour = outlier.colour , outlier.shape = outlier.shape ,
101- outlier.size = outlier.size , notch = notch , notchwidth = notchwidth , ... )
109+ outlier.size = outlier.size , notch = notch , notchwidth = notchwidth , varwidth = varwidth , ... )
102110}
103111
104112GeomBoxplot <- proto(Geom , {
@@ -118,14 +126,27 @@ GeomBoxplot <- proto(Geom, {
118126 df $ ymax_final <- pmax(out_max , df $ ymax )
119127 }
120128
121- transform(df ,
122- xmin = x - width / 2 , xmax = x + width / 2 , width = NULL
123- )
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+ }
140+ } else {
141+ transform(df ,
142+ xmin = x - relvarwidth * width / 2 , xmax = x + relvarwidth * width / 2 , width = NULL , relvarwidth = NULL
143+ )
144+ }
124145
125146 }
126147
127148 draw <- function (. , data , ... , fatten = 2 , outlier.colour = NULL , outlier.shape = NULL , outlier.size = 2 ,
128- notch = FALSE , notchwidth = .5 ) {
149+ notch = FALSE , notchwidth = .5 , varwidth = FALSE ) {
129150 common <- data.frame (
130151 colour = data $ colour ,
131152 size = data $ size ,
0 commit comments