2011-01-22 29 views
22

我正在嘗試使用ggplot2/geom_boxplot生成鬍鬚定義爲第5和第95百分位數而不是0.25 - 1.5 IQR/0.75 + IQR的盒圖,並繪製了這些新鬍鬚的異常值照常。我可以看到,geom_boxplot美學包括ymax/ymin,但我不清楚我是如何在這裏放置數值的。這似乎是:在geom_boxplot中更改鬍鬚定義

stat_quantile(quantiles = c(0.05, 0.25, 0.5, 0.75, 0.95)) 

應能有所幫助,但我不知道如何與這個屬性來設置相應的geom_boxplot的)結果(美學:

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

我已經看到其他人提到基本上手動建立一個boxplot樣物體,但我寧願保持整個boxplot格式完整,只是修改了兩個被繪製的變量的含義。

回答

34

geom_boxplot與stat_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") 
+0

kohske,這確實改變晶須(謝謝!),但異常消失。 – cswingle 2011-01-22 01:49:12

+0

該示例已更新:有多種方法可以完成此操作,但也許這是在geom_point中繪製異常值的最簡單方法。 – kohske 2011-01-22 02:11:29

2

現在可以在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" 
) 

enter image description here

3

大廈@ konvas的答案,使用ggproto系統和定義自己的統計中ggplot2.0.x,可以extend ggplot開始。

通過複製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, 
     ... 
    ) 
) 
} 

然後,定義圖層函數。請注意,我直接從stat_boxplot複製了B/C,您必須使用:::訪問一些內部ggplot2函數。這包括從StatBoxplot直接複製的大量內容,但關鍵區域是直接根據compute_group函數內的qs參數:stats <- as.numeric(stats::quantile(data$y, qs))計算統計信息。

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 
    } 
) 

還有一個gist here,包含此代碼。

然後,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)) 

Example extending to 5/95th