2017-09-21 4 views
0

Ich habe einen Datenrahmen von 10 Variablen und habe ihn in zwei Spalten gezeichnet. Aber ggplot definiert Whisker als 5. und 95. Perzentil. Ich möchte Whiskerlängen wie Q1 - 1.5*IQR/Q3 + 1.5*IQR für jedes dieser Diagramme und Ausreißer wie üblich. Eine ähnliche Frage wurde in diesem link veröffentlicht, aber ich konnte es nicht verwenden. Jede Hilfe wird geschätzt !!Ändern der Whiskerlänge von mehreren Boxplots in R

library(ggplot2) 
library(tidyr) 

df <- data.frame(matrix(rnorm(2000), ncol = 10)) 
plot.data <- gather(df, variable, value) 
# plot.data$out <- as.numeric(rep(input_data, each = nrow(x_train))) 
p <- ggplot(plot.data, aes(x = 0, y=value)) 
p <- p + geom_boxplot() 
#p <- p + geom_point(aes(x = 0, y = test_data), color = "red") 
p <- p + facet_wrap(~variable, scales = "free_x", strip.position = 'top', ncol = 2) 
p <- p + coord_flip() 
p <- p + xlab("") + ylab("") 
p <- p + theme(legend.position="none") + theme_bw() 
p <- p + theme(axis.text.y=element_blank(), 
      axis.ticks.y=element_blank()) 
p 
+0

Die untere Antwort in der Zeit nach Einstellung gefunden Sie scheint einfach: stackoverflow.com/a/38504938/3124909 – CMichael

Antwort

2

standardmäßig (= FALSE gekerbt), die geom_boxplot() sollten Sie die Whisker Sie geben (Q1 - 1,5 * IQR/Q3 + 1,5 * IQB). Siehe eine aktuelle Frage link. Dies unterliegt zwar dem Quantil, der IQR-Definition.

Wenn Sie darauf bestehen, sie manuell mit stat_summary

# geom_boxplot parameters with stat summary 
f <- function(x) { 
    r <- quantile(x, probs = c(0.25, 0.25, 0.5, 0.75, 0.75)) 
    r[[1]]<-r[[1]]-1.5*IQR(x) #ymin lower whisker, as per geom_boxplot 
    r[[5]]<-r[[5]]+1.5*IQR(x) #ymax upper whisker 
    names(r) <- c("ymin", "lower", "middle", "upper", "ymax") 
    r 
} 

# To subset the outlying points for plotting, 
o <- function(x) { 
    r <- quantile(x, probs = c(0.25, 0.75)) 
    r[[1]]<-r[[1]]-1.5*IQR(x) 
    r[[2]]<-r[[2]]+1.5*IQR(x) 
    subset(x, x < r[[1]] | r[[2]] < x) 
} 

# added seed for consistency 
set.seed(123)  

df <- data.frame(matrix(rnorm(2000), ncol = 10)) 
plot.data <- gather(df, variable, value) 
# plot.data$out <- as.numeric(rep(input_data, each = nrow(x_train))) 
p <- ggplot(plot.data, aes(x = 0, y=value)) 
p <- p + stat_summary(fun.data = f, geom="boxplot")+ 
    stat_summary(fun.y = o, geom="point") 
#p <- p + geom_point(aes(x = 0, y = test_data), color = "red") 
p <- p + facet_wrap(~variable, scales = "free_x", strip.position = 'top', ncol = 2) 
p <- p + coord_flip() 
p <- p + xlab("") + ylab("") 
p <- p + theme(legend.position="none") + theme_bw() 
p <- p + theme(axis.text.y=element_blank(), 
       axis.ticks.y=element_blank()) 
Verwandte Themen