ggplot2 - 在最小/最大的Boxplot威士忌 [英] ggplot2 - Boxplot Whiskers at Min/Max

查看:380
本文介绍了ggplot2 - 在最小/最大的Boxplot威士忌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在ggplot2中,我希望将胡须延伸到数据集的最小值和最大值,并且不显示异常值。我发现了隐藏异常值的方法,但是我一直无法让晶须延伸到每个群组的最小值和最大值。

In ggplot2, I would like to have the whiskers extend to the min and max values for a data set and not show the outliers. I've found the method to hide the outliers but I have been unable to get the whiskers to extend to the min and max for each group.

a <- data.frame(group = "a", value = rnorm(10))
b <- data.frame(group = "b", value = rnorm(100))
c <- data.frame(group = "c", value = rnorm(1000))

data <- rbind(a, b, c)

ggplot(data, aes(x=group, y=value)) + 
  stat_boxplot(geom ='errorbar') +
  geom_boxplot() #geom_boxplot(outlier.shape = NA)






问:设置ggplot2盒图的正确方法是什么让胡须延伸到最小和最大值?




Q: What is the correct way to setup ggplot2 boxplots so that the whiskers extend to the min and max?

推荐答案

按照LJW的评论,我认为这是你想要的:

Following LJW's comment I think this is what you want:

a <- data.frame(group = "a", value = rnorm(10))
b <- data.frame(group = "b", value = rnorm(100))
c <- data.frame(group = "c", value = rnorm(1000))

data <- rbind(a, b, c)

o <- function(x) {
  subset(x, x == max(x) | x == min(x))
}

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

ggplot(data, aes(x=group, y=value)) + 
  stat_summary(fun.data=f, geom="boxplot") + 
  stat_summary(fun.y = o, geom="point") +
  stat_boxplot(geom='errorbar',coef=10) #just give an arbitrarily big number here

更新
您可以在stat_boxplot函数中添加coef参数的胡须:

UPDATE You can add the whiskers with the coef argument in the stat_boxplot function:

这篇关于ggplot2 - 在最小/最大的Boxplot威士忌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆