如何从用ggplot()构建的箱形图图形中删除胡须

时间:2015-07-06 09:15:21

标签: r plot ggplot2 boxplot

我想从使用ggplot()包创建的boxplot图形中删除胡须。箱形图用于使用自定义函数MinMeanSEMMax绘制均值和标准误差。我非常感谢你的帮助! 这是我使用的脚本:

MinMeanSEMMax <- function(x) {
  v <- c(min(x), mean(x) - sd(x)/sqrt(length(x)), mean(x), mean(x) + sd(x)/sqrt(length(x)), max(x))
  names(v) <- c("ymin", "lower", "middle", "upper", "ymax")
  v
}

ggplot(mtcars, aes(factor(am), mpg, fill=factor(am))) +
  stat_summary(fun.data=MinMeanSEMMax, geom="boxplot", colour="black") + 
  ggtitle("mtcars boxplot") + scale_fill_brewer(palette="OrRd")

1 个答案:

答案 0 :(得分:4)

您可以重新定义您的功能,以便ymin = lowerymax = upper

MinMeanSEMMax <- function(x) {
  v <- c(mean(x) - sd(x)/sqrt(length(x)), mean(x) - sd(x)/sqrt(length(x)), mean(x),
         mean(x) + sd(x)/sqrt(length(x)), mean(x) + sd(x)/sqrt(length(x)))
  names(v) <- c("ymin", "lower", "middle", "upper", "ymax")
  v
}

ggplot(mtcars, aes(factor(am), mpg, fill=factor(am))) +
  stat_summary(fun.data=MinMeanSEMMax, geom="boxplot", colour="black") + 
  ggtitle("mtcars boxplot") + scale_fill_brewer(palette="OrRd")

enter image description here