2009-09-16 110 views
14

我们都喜欢像中位数和四分位数范围这样的强有力的措施,但让我们面对它,在许多领域,箱形图几乎从未出现在已发表的文章中,而手段和标准错误始终如此。Boxplot schmoxplot:如何绘制由R中的一个因子调节的平均值和标准误差?

格子,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

回答

14

第一条曲线刚刚在 blog post on imachordata.com中报道。 (帽子提示David Smith on blog.revolution-computing.com)您也可以read the related documentation from Hadley on ggplot2

这里的示例代码:

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

+0

你刚才打我一个!我昨天阅读了www.imachordata.com的帖子,甚至通过电子邮件发送给了一位前同事。 – 2009-09-16 16:02:49

+0

这是R博客中的一个小世界。 :)我最近开始关注R星球(http://planetr.stderr.org/),这有点令人难以置信。 – Shane 2009-09-16 16:18:49

+0

我需要停止懒惰,并开始维护R博客列表。 – 2009-09-16 18:21:53

0

ggplot产生美观的图表,但我没有魄力去尝试和发布任何ggplot输出然而。

直到一天到来,这里是我如何制作上述图表。我使用名为'gplots'的图形包来获得标准误差线(使用我已计算的数据)。请注意,此代码为每个类/类别提供了两个或更多因素。这需要数据以矩阵形式进入,而“barplot2”函数中的“beside = TRUE”命令可以防止堆叠。

# Create the data (means) matrix 
# Using the matrix accommodates two or more factors for each class 

data.m <- matrix(c(75,34,19, 39,90,41), nrow = 2, ncol=3, byrow=TRUE, 
       dimnames = list(c("Factor 1", "Factor 2"), 
           c("Class A", "Class B", "Class C"))) 

# Create the standard error matrix 

error.m <- matrix(c(12,10,7, 4,7,3), nrow = 2, ncol = 3, byrow=TRUE) 

# Join the data and s.e. matrices into a data frame 

data.fr <- data.frame(data.m, error.m) 

# load library {gplots} 

library(gplots) 

# Plot the bar graph, with standard errors 

with(data.fr, 
    barplot2(data.m, beside=TRUE, axes=T, las=1, ylim = c(0,120), 
       main=" ", sub=" ", col=c("gray20",0), 
        xlab="Class", ylab="Total amount (Mean +/- s.e.)", 
       plot.ci=TRUE, ci.u=data.m+error.m, ci.l=data.m-error.m, ci.lty=1)) 

# Now, give it a legend: 

legend("topright", c("Factor 1", "Factor 2"), fill=c("gray20",0),box.lty=0) 

这是很简单的简,美观,但似乎是大多数期刊/老教授们希望看到的。

我会发布这些示例数据生成的图表,但这是我在网站上的第一篇文章。抱歉。一个应该能够复制粘贴整个事情(安装“gplots”包后)没有问题。

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") 

产生这种非常可行的图形,其中大部分是默认选项。请注意,默认情况下,错误栏是标准错误,但参数需要一个函数,因此它们可以是任何您想要的! sciplot bargraph.CI with mpg data

8

有点迟到游戏,但这个解决方案可能对未来的用户有用。它使用加载了R的diamond 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 + error plot http://i41.tinypic.com/ief48o.png

相关问题