Totalmente personalizable ggplot2 boxplot ...
#bootstrap
data <- data.frame(value=rnorm(100,mean = 0.5, sd = 0.2),group=0)
#processing
metaData <- ddply(data,~group,summarise,
mean=mean(data$value),
sd=sd(data$value),
min=min(data$value),
max=max(data$value),
median=median(data$value),
Q1=0,Q3=0
)
bps <- boxplot.stats(data$value,coef=1.5)
metaData$min <- bps$stats[1] #lower wisker
metaData$max <- bps$stats[5] #upper wisker
metaData$Q1 <- bps$stats[2] # 1st Quartile
metaData$Q3 <- bps$stats[4] # 3rd Quartile
#adding outliers
out <- data.frame() #initialising storage for outliers
if(length(bps$out) > 0){
for(n in 1:length(bps$out)){
pt <-data.frame(value=bps$out[n],group=0)
out<-rbind(out,pt)
}
}
#adding labels
labels <-data.frame(value=metaData$max, label="Upper bound")
labels <-rbind(labels,data.frame(value=metaData$min, label="Lower bound"))
labels <-rbind(labels,data.frame(value=metaData$median, label="Median"))
labels <-rbind(labels,data.frame(value=metaData$Q1, label="First quartile"))
labels <-rbind(labels,data.frame(value=metaData$Q3, label="Third quartile"))
#drawing
library(ggplot2)
p <- ggplot(metaData,aes(x=group,y=mean))
p <- p + geom_segment(aes(x=c(0.1,0,0.1),y=c(0,0,1),xend=c(0,0,-0.1),yend=c(0,1,1)))
p <- p + geom_text(aes(y=c(0,1),label=c(0,1),x=0.2))
p <- p + geom_errorbar(aes(ymin=min,ymax=max),linetype = 1,width = 0.5) #main range
p <- p + geom_linerange(aes(ymin=min,ymax=max),linetype = 1,width = 0, color="white")# white line range
p <- p + geom_linerange(aes(ymin=min,ymax=max),linetype = 2) #main range dotted
p <- p + geom_crossbar(aes(y=median,,ymin=Q1,ymax=Q3),linetype = 1,fill='white') #box
if(length(out) >0) p <- p + geom_point(data=out,aes(x=group,y=value),shape=4) # drawning outliers if any
p <- p + scale_x_discrete(breaks=c(0))
p <- p + scale_y_continuous(name= "Value")
p <- p + geom_text(data=labels,aes(x=0.5,y=value,label=round(value,2)),colour="black",angle=0,hjust=0.5, vjust=0.5,size=3)
p <- p + opts(panel.background = theme_rect(fill = "white",colour = NA))
p <- p + opts(panel.grid.minor = theme_blank(), panel.grid.major = theme_blank())
p <- p + opts(axis.title.x=theme_blank())
p <- p + opts(axis.text.x = theme_blank())
p <- p + opts(axis.title.y=theme_blank())
p <- p + opts(axis.text.y = theme_blank())
p + coord_flip()
Resultado:
... el código puede ser un poco feo pero funciona de la manera correcta.
pars
argumentos para reducir su relación de aspecto (boxwex
) y el tamaño de los bigotes (staplewex
)).