R 更改geom_箱线图中的晶须定义

R 更改geom_箱线图中的晶须定义,r,ggplot2,statistics,boxplot,ggproto,R,Ggplot2,Statistics,Boxplot,Ggproto,我试图使用ggplot2/geom_箱线图生成一个箱线图,其中胡须被定义为第5和第95百分位,而不是0.25-1.5 IQR/0.75+IQR,并且这些新胡须的异常值像往常一样被绘制。我可以看到geom_箱线图美学包括ymax/ymin,但我不清楚如何在这里输入值。似乎: stat_quantile(quantiles = c(0.05, 0.25, 0.5, 0.75, 0.95)) 应该能够提供帮助,但我不知道如何关联此统计的结果以设置适当的geom_boxplot()美学: geom_b

我试图使用ggplot2/geom_箱线图生成一个箱线图,其中胡须被定义为第5和第95百分位,而不是0.25-1.5 IQR/0.75+IQR,并且这些新胡须的异常值像往常一样被绘制。我可以看到geom_箱线图美学包括ymax/ymin,但我不清楚如何在这里输入值。似乎:

stat_quantile(quantiles = c(0.05, 0.25, 0.5, 0.75, 0.95))
应该能够提供帮助,但我不知道如何关联此统计的结果以设置适当的geom_boxplot()美学:

geom_boxplot(aes(ymin, lower, middle, upper, ymax))

我看过其他帖子,其中人们提到基本上是手动构建一个类似于箱线图的对象,但我宁愿保持整个箱线图完形不变,只需修改正在绘制的两个变量的含义。

geom\u箱线图和stat\u summary可以做到:

# define the summary function
f <- function(x) {
  r <- quantile(x, probs = c(0.05, 0.25, 0.5, 0.75, 0.95))
  names(r) <- c("ymin", "lower", "middle", "upper", "ymax")
  r
}

# sample data
d <- data.frame(x=gl(2,50), y=rnorm(100))

# do it
ggplot(d, aes(x, y)) + stat_summary(fun.data = f, geom="boxplot")

# example with outliers
# define outlier as you want    
o <- function(x) {
  subset(x, x < quantile(x)[2] | quantile(x)[4] < x)
}

# do it
ggplot(d, aes(x, y)) + 
  stat_summary(fun.data=f, geom="boxplot") + 
  stat_summary(fun.y = o, geom="point")
#定义摘要函数

f现在可以在
ggplot2_2.1.0
中指定胡须端点。从
?geom_箱线图中的示例复制:

 # It's possible to draw a boxplot with your own computations if you
 # use stat = "identity":
 y <- rnorm(100)
 df <- data.frame(
   x = 1,
   y0 = min(y),
   y25 = quantile(y, 0.25),
   y50 = median(y),
   y75 = quantile(y, 0.75),
   y100 = max(y)
 )
 ggplot(df, aes(x)) +
   geom_boxplot(
    aes(ymin = y0, lower = y25, middle = y50, upper = y75, ymax = y100),
    stat = "identity"
  )
library(ggplot2)
y <- rnorm(100)
df <- data.frame(x = 1, y = y)
# whiskers extend to 5/95th percentiles by default
ggplot(df, aes(x = x, y = y)) +
  stat_boxplot_custom()
# or extend the whiskers to min/max
ggplot(df, aes(x = x, y = y)) +
  stat_boxplot_custom(qs = c(0, 0.25, 0.5, 0.75, 1))
#如果您愿意,您可以使用自己的计算绘制箱线图
#使用stat=“identity”:

y基于@konvas的答案,从
ggplot2.0.x
开始,您可以使用
ggproto
系统定义自己的统计数据

通过复制ggplot2
stat\u-boxplot
代码并进行一些编辑,您可以快速定义一个新的stat(
stat\u-boxplot\u-custom
),该stat(
stat\u-boxplot
)将您想要用作参数(
qs
)的百分位数,而不是
stat\u-boxplot
使用的
coef
参数。新的统计数据定义如下:

# modified from https://github.com/tidyverse/ggplot2/blob/master/R/stat-boxplot.r
library(ggplot2)
stat_boxplot_custom <- function(mapping = NULL, data = NULL,
                     geom = "boxplot", position = "dodge",
                     ...,
                     qs = c(.05, .25, 0.5, 0.75, 0.95),
                     na.rm = FALSE,
                     show.legend = NA,
                     inherit.aes = TRUE) {
  layer(
      data = data,
      mapping = mapping,
      stat = StatBoxplotCustom,
      geom = geom,
      position = position,
      show.legend = show.legend,
      inherit.aes = inherit.aes,
      params = list(
      na.rm = na.rm,
      qs = qs,
      ...
      )
  )
}
还有一个,包含此代码

然后,可以调用
stat\u boxplot\u custom
,就像调用
stat\u boxplot

 # It's possible to draw a boxplot with your own computations if you
 # use stat = "identity":
 y <- rnorm(100)
 df <- data.frame(
   x = 1,
   y0 = min(y),
   y25 = quantile(y, 0.25),
   y50 = median(y),
   y75 = quantile(y, 0.75),
   y100 = max(y)
 )
 ggplot(df, aes(x)) +
   geom_boxplot(
    aes(ymin = y0, lower = y25, middle = y50, upper = y75, ymax = y100),
    stat = "identity"
  )
library(ggplot2)
y <- rnorm(100)
df <- data.frame(x = 1, y = y)
# whiskers extend to 5/95th percentiles by default
ggplot(df, aes(x = x, y = y)) +
  stat_boxplot_custom()
# or extend the whiskers to min/max
ggplot(df, aes(x = x, y = y)) +
  stat_boxplot_custom(qs = c(0, 0.25, 0.5, 0.75, 1))
库(ggplot2)

y kohske,这确实改变了胡须(谢谢!),但异常值消失了。该示例已更新:有多种方法可以做到这一点,但也许这是在geom_point中绘制异常值的最简单方法。太棒了!o函数可能应使用相同的probs=c(0.05,0.95)[1]/[2],以便排除的点与晶须匹配。再次感谢。看起来我需要了解更多关于统计摘要的信息。有可能在ymin和ymax中添加胡须吗?这个答案非常好!上述方法不适用于facet_网格。这是完美的。非常感谢你!!