在geom_boxplot中更改whisker定义

csw*_*gle 28 statistics r ggplot2 boxplot ggproto

我试图使用GGPLOT2/geom_boxplot以产生其中晶须被定义为在5位和第95百分位,而不是0.25的箱线图 - 1.5 IQR/0.75 + IQR并从这些新的晶须离群值被绘制如常.我可以看到geom_boxplot美学包括ymax/ymin,但我不清楚如何将值放在这里.这好像是:

stat_quantile(quantiles = c(0.05, 0.25, 0.5, 0.75, 0.95))
Run Code Online (Sandbox Code Playgroud)

应该能够提供帮助,但我不知道如何将此stat的结果与设置相应的geom_boxplot()美学联系起来:

geom_boxplot(aes(ymin, lower, middle, upper, ymax))
Run Code Online (Sandbox Code Playgroud)

我已经看过其他帖子,其中人们提到基本上手动构建一个类似boxplot的对象,但我宁愿保持整个boxplot格式塔完整,只是修改两个正在绘制的变量的含义.

koh*_*ske 41

带stat_summary的geom_boxplot可以做到:

# 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")
Run Code Online (Sandbox Code Playgroud)


r_a*_*anb 6

基于@ konvas的答案,从中开始ggplot2.0.x,你可以使用系统扩展ggplotggproto并定义你自己的stat.

通过复制ggplot2 stat_boxplot代码并进行一些编辑,您可以快速定义一个新的stat(stat_boxplot_custom),它将您想要用作参数(qs)的百分位数而不是使用的coef参数stat_boxplot.这里定义了新的统计数据:

# 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,
      ...
      )
  )
}
Run Code Online (Sandbox Code Playgroud)

然后,定义层功能.注意b/c我是直接复制的stat_boxplot,你必须使用几个内部的ggplot2函数:::.这包括直接复制的很多东西StatBoxplot,但关键区域是直接从qs参数计算统计数据:函数stats <- as.numeric(stats::quantile(data$y, qs))内部compute_group.

StatBoxplotCustom <- ggproto("StatBoxplotCustom", Stat,
  required_aes = c("x", "y"),
  non_missing_aes = "weight",

  setup_params = function(data, params) {
    params$width <- ggplot2:::"%||%"(
      params$width, (resolution(data$x) * 0.75)
    )

    if (is.double(data$x) && !ggplot2:::has_groups(data) && any(data$x != data$x[1L])) {
      warning(
        "Continuous x aesthetic -- did you forget aes(group=...)?",
        call. = FALSE
      )
    }

    params
  },

  compute_group = function(data, scales, width = NULL, na.rm = FALSE, qs = c(.05, .25, 0.5, 0.75, 0.95)) {

    if (!is.null(data$weight)) {
      mod <- quantreg::rq(y ~ 1, weights = weight, data = data, tau = qs)
      stats <- as.numeric(stats::coef(mod))
    } else {
    stats <- as.numeric(stats::quantile(data$y, qs))
    }
    names(stats) <- c("ymin", "lower", "middle", "upper", "ymax")
    iqr <- diff(stats[c(2, 4)])

    outliers <- (data$y < stats[1]) | (data$y > stats[5])

    if (length(unique(data$x)) > 1)
    width <- diff(range(data$x)) * 0.9

    df <- as.data.frame(as.list(stats))
    df$outliers <- list(data$y[outliers])

    if (is.null(data$weight)) {
      n <- sum(!is.na(data$y))
    } else {
      # Sum up weights for non-NA positions of y and weight
      n <- sum(data$weight[!is.na(data$y) & !is.na(data$weight)])
    }

    df$notchupper <- df$middle + 1.58 * iqr / sqrt(n)
    df$notchlower <- df$middle - 1.58 * iqr / sqrt(n)

    df$x <- if (is.factor(data$x)) data$x[1] else mean(range(data$x))
    df$width <- width
    df$relvarwidth <- sqrt(n)
    df
  }
)
Run Code Online (Sandbox Code Playgroud)

这里还有一个要点,包含此代码.

然后,stat_boxplot_custom可以像下面这样调用stat_boxplot:

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))
Run Code Online (Sandbox Code Playgroud)

示例扩展到5/95


kon*_*vas 5

现在可以在中指定晶须端点ggplot2_2.1.0。从以下示例复制?geom_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"
  )
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明