ggplot2盒须图:显示95%置信区间&删除异常值 [英] ggplot2 box-whisker plot: show 95% confidence intervals & remove outliers

查看:889
本文介绍了ggplot2盒须图:显示95%置信区间&删除异常值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个盒子情节,看起来就像下面的情节。但不是缺省值,我想提出(1)95%置信区间(2)没有异常值

95%的置信区间可能意味着(i)延长盒子和除去胡须,或者(ii)只有一个胡须和胡须,然后取出盒子。或者如果人们有其他想法在这样的情节中呈现95%的置信区间,我愿意提供建议。最终目标是显示同一图上多个类别数据的平均值和conf间隔。

  set.seed(1234) 
df < - data.frame(cond = factor(rep(c(A,B),each = 200)),
rating = c(rnorm(200),rnorm 200,mean = .8))
ggplot(df,aes(x = cond,y = rating,fill = cond))+ geom_boxplot()+
guides(fill = FALSE)+ coord_flip()



图片和源代码: http://www.cookbook-r.com/Graphs/Plotting_distributions_(ggplot2)/

解决方案

您可以将大小设置为0来隐藏异常值:

  ggplot(df,aes (x = cond,y = rating,fill = cond))+ 
geom_boxplot(outlier.size = 0)+
guides(fill = FALSE)+ coord_flip()

您可以使用 stat_summary 函数将平均值添加到图中:


$ b $

  ggplot(df,aes(x = cond,y = rating,fill = cond))+ 
geom_boxplot(outlier.size = 0)+
stat_summary(fun.y =mean,geom =point,shape = 23,size = 4,fill =white)+
guides(fill = FALSE)+
coord_flip()


I'd like a box plot that looks just like the one below. But instead of the default, I'd like to present (1) 95% confidence intervals and (2) without the outliers.

The 95% confidence intervals could mean (i) extending the boxes and removing the whiskers, or (ii) having just a mean and whiskers, and removing the boxes. Or if people have other ideas for presenting 95% confidence intervals in a plot like this, I'm open to suggestions. The final goals is to show mean and conf intervals for data across multiple categories on the same plot.

set.seed(1234)
df <- data.frame(cond = factor( rep(c("A","B"), each=200) ), 
                   rating = c(rnorm(200),rnorm(200, mean=.8))
ggplot(df, aes(x=cond, y=rating, fill=cond)) + geom_boxplot() + 
    guides(fill=FALSE) + coord_flip()

Image and code source: http://www.cookbook-r.com/Graphs/Plotting_distributions_(ggplot2)/

解决方案

You can hide the outliers by setting the size to 0:

ggplot(df, aes(x=cond, y=rating, fill=cond)) + 
  geom_boxplot(outlier.size = 0) + 
  guides(fill=FALSE) + coord_flip()

You can add the mean to the plot with the stat_summary function:

ggplot(df, aes(x=cond, y=rating, fill=cond)) + 
  geom_boxplot(outlier.size = 0) + 
  stat_summary(fun.y="mean", geom="point", shape=23, size=4, fill="white") +
  guides(fill=FALSE) + 
  coord_flip()

这篇关于ggplot2盒须图:显示95%置信区间&amp;删除异常值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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