2009-09-16 111 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

相關問題