在facet'ed geom_boxplot中更改胡须定义 [英] Changing whisker definition in facet'ed geom_boxplot

查看:366
本文介绍了在facet'ed geom_boxplot中更改胡须定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个带有多个变量箱形图的 facet_grid 。举个例子,这个图可以通过下面的虚拟数据来重现:

  require(ggplot2)
require(plyr)
library(reshape2)

set.seed(1234)
x < - rnorm(100)
y.1 <-rnorm(100)
y.2 <-rnorm(100)
y.3 <-rnorm(100)
y.4 <-rnorm(100)

df < - (as.data .frame(cbind(x,y.1,y.2,y.3,y.4)))
dfmelt< -melt(df,measure.vars = 2:5)

并将结果图创建为

  dfmelt $ bin < -  factor(round_any(dfmelt $ x,0.5))
ggplot(dfmelt,aes(x = bin,y = value,fill = variable))+
geom_boxplot )+
facet_grid(。〜bin,scales =free)+
labs(x =X(binned))+
theme(axis.text.x = element_blank() )

给出以下结果:

然而,我想重新定义boxplot胡须,所以他们不会represe nt 0.25 - 1.5 IQR / 0.75 + IQR和异常值,但是(i)完整的第5和第95百分位数或(ii)数据的无限和上位数。

解决方案您可以使用 stat_summary 自定义外观,例如

  ggplot(dfmelt,aes(x = bin,y = value,fill = variable))+ 
stat_summary (x,c(0.05,0.25,0.5,0.75,0.95)),c(ymin,lower,middle), ,upper,ymax)),
position =dodge)


I created a facet_grid with boxplots of multiple variables. To give an example, the graph can be reproduced by following dummy data

require(ggplot2)
require(plyr)
library(reshape2)

set.seed(1234)
x<- rnorm(100)
y.1<-rnorm(100)
y.2<-rnorm(100)
y.3<-rnorm(100)
y.4<-rnorm(100)

df<- (as.data.frame(cbind(x,y.1,y.2,y.3,y.4)))
dfmelt<-melt(df, measure.vars = 2:5)

and creating the resulting graph as

dfmelt$bin <- factor(round_any(dfmelt$x,0.5))
ggplot(dfmelt, aes(x=bin, y=value, fill=variable))+
  geom_boxplot()+
  facet_grid(.~bin, scales="free")+
  labs(x="X (binned)")+
  theme(axis.text.x=element_blank())

which gives the following result:

However, I would like to redefine the boxplot whiskers so they don't represent 0.25 - 1.5 IQR / 0.75 + IQR and outliers, but (i) the complete 5th and 95th percentile or (ii) the infinum and supremum of the data.

解决方案

You could use stat_summary to customize the appearance, e.g.

ggplot(dfmelt, aes(x=bin, y=value, fill=variable)) + 
stat_summary(geom = "boxplot", 
             fun.data = function(x) setNames(quantile(x, c(0.05, 0.25, 0.5, 0.75, 0.95)), c("ymin", "lower", "middle", "upper", "ymax")), 
             position = "dodge")

这篇关于在facet'ed geom_boxplot中更改胡须定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆