0

I'm trying to change the whisker definition to extend to the minimum and maximum (i.e., not to consider anything as an outlier), as in this question, but only for a single level of the factor that is mapped to the x-axis. The code in that answer will change the whisker definition for the entire plot.

What's the proper way, if any, to go about this?

Community
  • 1
  • 1
Will
  • 4,241
  • 4
  • 39
  • 48
  • 1
    One way would be to create two data.frames or subset the data. Something like `geom_boxplot(data = full_df, ...) + stat_summary(data = single_factor_df, ...)`. – JasonAizkalns Jan 18 '16 at 19:12
  • Or create the required dimensions yourself first and then use `geom_boxplot(..., stat = 'identity')`. It does seem like this approach would be confusing, if not misleading, to the audience. – Axeman Jan 18 '16 at 19:14
  • @JasonAizkalns, that's what came to mind, but when I tried it, I got `Error: Aesthetics must be either length 1 or the same as the data (60): x, y, fill`. Not sure what the problem is. – Will Jan 18 '16 at 19:20
  • 1
    @Will this is why its best to provide sample data or make your question reproducible. – JasonAizkalns Jan 18 '16 at 19:21

1 Answers1

3

Extending the example linked in the question, you could do something like:

f <- function(x) {
  r <- quantile(x, probs = c(0.05, 0.25, 0.5, 0.75, 0.95))
  names(r) <- c("ymin", "lower", "middle", "upper", "ymax")
  r
}

# sample data
d <- data.frame(x = gl(2,50), y = rnorm(100))

# do it
ggplot(d, aes(x, y)) + 
  stat_summary(data = subset(d, x == 1), fun.data = f, geom = "boxplot") +
  geom_boxplot(data = subset(d, x == 2))

In this case, factor x == 2 gets the "regular" geom_boxplot, but factor x == 1 is the "extended".


In your case, and being a little more abstract, you probably want to do something like this:

ggplot(d, aes(x, y)) + 
  stat_summary(data = subset(d, x == "special_factor"), fun.data = f, geom = "boxplot") +
  geom_boxplot(data = subset(d, x != "special_factor"))
JasonAizkalns
  • 20,243
  • 8
  • 57
  • 116
  • Thanks! I figured out my problem, and now this works beautifully. However, now the factors are not ordered in the same way as in the original data. Is there a way of fixing that? – Will Jan 18 '16 at 19:57
  • @Will not sure? Maybe create a reproducible example and post it as a new question. – JasonAizkalns Jan 18 '16 at 20:30