在geom_boxplot中更改whisker end

时间:2017-05-25 08:54:02

标签: r boxplot outliers

geom_boxplot

我正在尝试使用geom_boxplot来绘制图片。但是,我想更改whisker的定义,这意味着whisker的结尾是我的数据的最大值和最小值,并且应该删除异常值。基于我现有的代码会很好。

非常感谢。

con.for2=data.frame(d=as.character(gl(9,20)),close=exp(rnorm(180)),open=exp(rnorm(180)))
concentration=melt(con.for2)
colnames(concentration)=c("location","Condition","formaldehyde")
p=ggplot(data=concentration,aes(factor(location), formaldehyde),ylim=c(0,0.15),cex.axis=1.5,cex.lab=15
         ) + geom_boxplot(aes(fill = Condition))+xlab("Location") + ylab("Formaldehyde concentration (mg/m3)")

1 个答案:

答案 0 :(得分:2)

改编自答案Changing whisker definition in geom_boxplot

 p <- ggplot(data=concentration,aes(factor(location), formaldehyde),ylim=c(0,0.15),cex.axis=1.5,cex.lab=15)

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

p + stat_summary(fun.data=f, aes(fill= Condition), geom="boxplot", position="dodge")

enter image description here