2
votes

I created a facet_grid with boxplots of multiple variables. To give an example, the graph can be reproduced by following dummy data

require(ggplot2)
require(plyr)
library(reshape2)

set.seed(1234)
x<- rnorm(100)
y.1<-rnorm(100)
y.2<-rnorm(100)
y.3<-rnorm(100)
y.4<-rnorm(100)

df<- (as.data.frame(cbind(x,y.1,y.2,y.3,y.4)))
dfmelt<-melt(df, measure.vars = 2:5)

and creating the resulting graph as

dfmelt$bin <- factor(round_any(dfmelt$x,0.5))
ggplot(dfmelt, aes(x=bin, y=value, fill=variable))+
  geom_boxplot()+
  facet_grid(.~bin, scales="free")+
  labs(x="X (binned)")+
  theme(axis.text.x=element_blank())

which gives the following result: enter image description here

However, I would like to redefine the boxplot whiskers so they don't represent 0.25 - 1.5 IQR / 0.75 + IQR and outliers, but (i) the complete 5th and 95th percentile or (ii) the infinum and supremum of the data.

1
@lukeA wonderfull, this does the trick indeed !ruben baetens
Cool, I post it as a possible answer.lukeA

1 Answers

5
votes

You could use stat_summary to customize the appearance, e.g.

ggplot(dfmelt, aes(x=bin, y=value, fill=variable)) + 
stat_summary(geom = "boxplot", 
             fun.data = function(x) setNames(quantile(x, c(0.05, 0.25, 0.5, 0.75, 0.95)), c("ymin", "lower", "middle", "upper", "ymax")), 
             position = "dodge")