簡體   English   中英

在修改的ggplot-boxplot中繪制異常值時stat_summary(fun.y)出錯

[英]Error in stat_summary(fun.y) when plotting outliers in a modified ggplot-boxplot

我想繪制顯示95百分位而不是IQR的箱線圖,包括超過95%標准定義的異常值。 此代碼工作正常,並基於此處和網絡上的幾個答案:

f1 <- function(x) {
  subset(x, x < quantile(x, probs=0.025)) # only for low outliers
}

f2 <- function(x) {
  r <- quantile(x, probs = c(0.025, 0.25, 0.5, 0.75, 0.975))
  names(r) <- c("ymin", "lower", "middle", "upper", "ymax")
  r
}
d <- data.frame(x=gl(2,50), y=rnorm(100))

library(ggplot2)

p0 <- ggplot(d, aes(x,y)) +
        stat_summary(fun.data = f2, geom="boxplot") + coord_flip()

p1 <- p0 + stat_summary(fun.y = f1, geom="point")

d的結構是:

'data.frame':   100 obs. of  2 variables:
 $ x: Factor w/ 2 levels "1","2": 1 1 1 1 1 1 1 1 1 1 ...
 $ y: num  2.275 0.659 -0.821 -0.129 1.997 ...

現在,來看我的真實數據,其結構基本相同:

str(test)
'data.frame':   11830917 obs. of  2 variables:
 $ x: Ord.factor w/ 34 levels "SG26"<"SG22"<..: 18 18 18 18 18 18 18 18 18 18 ...
 $ y: num  84.6 84.1 93.3 84 93.2 94.3 83.3 92.5 94.5 98.8 ...

現在,如果我應用相同的繪圖命令,我得到:

    p0 <- ggplot(test, aes(x,y)) + stat_summary(fun.data = f2, geom="boxplot") +  coord_flip() 
    p1 <- p0 + stat_summary(fun.y = f1, geom="point")
    p1

Warning message:
Computation failed in `stat_summary()`:
Argumente implizieren unterschiedliche Anzahl Zeilen: 1, 0 

最后一行是德國版的“參數意味着不同的行數1 0”。 p0生產得很好。

兩個數據集之間可能有什么區別?

由@Heroka和@bdemarest確定的問題是由只有一個值的一個因子級別產生的。

我的解決方法是跳過這些因素:

f1 <- function(x) {
  if (length(x) > 7) {
    return(subset(x, x < quantile(x, probs=0.025))) # only for low outliers
  } else {
    return(NA)
  }
} 

由於未知原因,問題持續存在,直到每個因子級別至少有7個值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM