我们都喜欢像中位数和四分位数范围这样的强有力的测量方法,但是让我们面对它,在许多领域,箱形图几乎从未在已发表的文章中出现,而手段和标准错误一直如此.
在格子,ggplot2等中绘制箱形图很简单,画廊里装满了它们.是否有一种同样直接的方法来绘制均值和标准误差,以分类变量为条件?
我正在拍摄这样的情节:
http://freakonomics.blogs.nytimes.com/2008/07/30/how-big-is-your-halo-a-guest-post/
或者JMP中所谓的"钻石"(见图3):
http://blogs.sas.com/jmp/index.php?/archives/127-What-Good-Are-Error-Bars.html
Sha*_*ane 14
第一个情节刚刚在imachordata.com的博客文章中介绍过.(在blog.revolution-computing.com上给David Smith提示)你也可以在ggplot2上阅读Hadley的相关文档.
这是示例代码:
library(ggplot2)
data(mpg)
#create a data frame with averages and standard deviations
 hwy.avg<-ddply(mpg, c("class", "year"), function(df)
 return(c(hwy.avg=mean(df$hwy), hwy.sd=sd(df$hwy))))
#create the barplot component
 avg.plot<-qplot(class, hwy.avg, fill=factor(year), data=hwy.avg, geom="bar", position="dodge")
#first, define the width of the dodge
dodge <- position_dodge(width=0.9)
#now add the error bars to the plot
avg.plot+geom_linerange(aes(ymax=hwy.avg+hwy.sd, ymin=hwy.avg-hwy.sd), position=dodge)+theme_bw()
最终看起来像这样: alt text http://www.imachordata.com/wp-content/uploads/2009/09/barplot.png
Sam*_*ift 11
这个问题现在已经快2年了,但作为实验领域的新R用户,这对我来说是一个很大的问题,这个页面在谷歌搜索结果中很突出.我刚刚发现了一个比当前设置更好的答案,所以我想我会添加它.
包sciplot使任务变得非常容易.它可以在一个命令中完成工作
#only necessary to get the MPG dataset from ggplot for direct comparison
library(ggplot2)
data(mpg)
attach(mpg)
#the bargraph.CI function with a couple of parameters to match the ggplot example
#see also lineplot.CI in the same package
library(sciplot)
bargraph.CI(
  class,  #categorical factor for the x-axis
  hwy,    #numerical DV for the y-axis
  year,   #grouping factor
  legend=T, 
  x.leg=19,
  ylab="Highway MPG",
  xlab="Class")
生成这个非常可行的图表,主要是默认选项.请注意,默认情况下,错误栏是标准错误,但参数需要一个函数,因此它们可以是您想要的任何内容!
小智 7
游戏稍晚,但这个解决方案可能对未来的用户有用.它使用diamond加载了R 的data.frame并利用stat_summary两个(超短)自定义函数.
require(ggplot2)
# create functions to get the lower and upper bounds of the error bars
stderr <- function(x){sqrt(var(x,na.rm=TRUE)/length(na.omit(x)))}
lowsd <- function(x){return(mean(x)-stderr(x))}
highsd <- function(x){return(mean(x)+stderr(x))}
# create a ggplot
ggplot(diamonds,aes(cut,price,fill=color))+
  # first layer is barplot with means
  stat_summary(fun.y=mean, geom="bar", position="dodge", colour='white')+
  # second layer overlays the error bars using the functions defined above
  stat_summary(fun.y=mean, fun.ymin=lowsd, fun.ymax=highsd, geom="errorbar", position="dodge",color = 'black', size=.5)
bar +错误情节http://i41.tinypic.com/ief48o.png