r - Error in stat_summary(fun.y) when plotting outliers in a modified ggplot-boxplot -
i want plot boxplots showing 95 percentile instead of iqr, including outliers defined exceeding 95% criterion. code working fine, , based on several answers found here , on web:
f1 <- function(x) { subset(x, x < quantile(x, probs=0.025)) # 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")
the structure of d is:
'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 ...
now, coming real data, structured same:
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 ...
now, if applying same plot command, get:
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
the final line german version of "arguments imply differing number of rows 1 0". p0 produced fine.
what difference between 2 datasets?
the problem, identified @heroka , @bdemarest, arose 1 factor level having 1 value.
my workaround skip factors:
f1 <- function(x) { if (length(x) > 7) { return(subset(x, x < quantile(x, probs=0.025))) # low outliers } else { return(na) } }
for unknown reasons, problem persisted until there @ least 7 values per factor level.
Comments
Post a Comment