Tweaking box plot design

179 views
Skip to first unread message

Shai

unread,
Nov 1, 2010, 2:01:05 AM11/1/10
to ggplot2
Hi,

Is it possible to manually change the design of the individual
components of a box plot? For example, give different line types/
colors to the median, quantiles, the line that goes to the min/max,
and add a notch to that line.

I know its possible to tweak the design of outliers, so I thought
maybe... :)

Thanks!
Shai

Brandon Hurr

unread,
Nov 1, 2010, 2:22:00 PM11/1/10
to Shai, ggplot2
I would also like to know this. I saw an S plot this afternoon that had a maroon fill with black 1st and 3rd quartiles with a thick white median. It looked nice and allowed easier comparison of medians and quartiles. 

Brandon


--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: http://gist.github.com/270442

To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

Joshua Wiley

unread,
Nov 1, 2010, 3:03:31 PM11/1/10
to Brandon Hurr, Shai, ggplot2
Hi,

I did not see any built-in functionality for this. I kind of like
simple boxplots (maybe with jittered points added), but maybe I'm old
fashioned. Anyway, it is not terribly difficult to add. An example
follows. You can see where all the options go, so if you wanted to
have different colour and style for each, just keep adding arguments
and pass them on.

Cheers,

Josh


# Data
dat <- data.frame(x = 1, y = c(1, 3, 5, 4, 5, 5, 10, 15))
# Default possibilities
ggplot(data = dat, aes(x = x, y = y)) + geom_boxplot(fill = "black",
colour = "maroon", lwd = 2)

# add another argument for the median line colour
GeomBoxplot$draw <- function(., data, ..., outlier.colour = "black",
outlier.shape = 16, outlier.size = 2, midcol = NULL) {
defaults <- with(data, data.frame(
x = x, xmin = xmin, xmax = xmax,
colour = colour, size = size,
linetype = 1, group = 1, alpha = 1,
fill = alpha(fill, alpha),
stringsAsFactors = FALSE
))
defaults2 <- defaults[c(1,1), ]
defaults3 <- defaults
if(is.null(midcol)) {midcol <- defaults[, "colour"]}
defaults3[, "colour"] <- midcol

if (!is.null(data$outliers) && length(data$outliers[[1]] >= 1)) {
outliers_grob <- with(data,
GeomPoint$draw(data.frame(
y = outliers[[1]], x = x[rep(1, length(outliers[[1]]))],
colour=I(outlier.colour), shape = outlier.shape, alpha = 1,
size = outlier.size, fill = NA), ...
)
)
} else {
outliers_grob <- NULL
}

with(data, ggname(.$my_name(), grobTree(
outliers_grob,
GeomPath$draw(data.frame(y=c(upper, ymax), defaults2), ...),
GeomPath$draw(data.frame(y=c(lower, ymin), defaults2), ...),
GeomRect$draw(data.frame(ymax = upper, ymin = lower, defaults), ...),
GeomRect$draw(data.frame(ymax = middle, ymin = middle, defaults3), ...)
)))
}

# Different colours
ggplot(data = dat, aes(x = x, y = y)) + geom_boxplot(fill = "black",
colour = "maroon", lwd = 2, midcol = "white"

--
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
http://www.joshuawiley.com/

Brandon Hurr

unread,
Nov 1, 2010, 3:26:08 PM11/1/10
to Joshua Wiley, Shai, ggplot2
Very nice Josh. Thanks for the effort. 

Brandon

Shai

unread,
Nov 2, 2010, 12:50:38 AM11/2/10
to ggplot2
thanks! I'll try that...
Reply all
Reply to author
Forward
0 new messages