2012-04-26 229 views
1

我有可以用箱形圖繪製的數據,但每個箱子的n只有3個。我想用ggplot2中的一個點範圍圖繪製它們。默認情況下,它們相互重疊。我怎樣才能將我的點並排分組在boxplot中?箱型圖分組的點列線圖

library(ggplot2) 

x <- rnorm(12, 3,5) # Real data are not always normally distributed. 
y <- c(rep("T1", 6), rep("T2", 6)) 
z <- rep(c(10,20),6) 

dat <- data.frame(Treatment = y, Temp = z, Meas = x) 

p <- ggplot(dat, aes(Treatment, Meas)) 
p + geom_boxplot(aes(fill=factor(Temp))) 

編輯:我更新的問題,以排除引導的建議(最初的想法是使用置信區間爲誤差線一個問題,問題太多= d)。更詳細的引導問題給出here

enter image description here

回答

5

你有兩個問題(儘量避免)。

  1. Bootstrapping。如何從3分的樣本中引導,你不知道底層分佈?

  2. 線範圍。我已經使用您的原始數據來構建線範圍。對於一個行範圍,你只需要一個最小值,最大值和中間值:

    ##First rearrange your data frame 
    dat = with(dat, dat[order(Treatment, Temp, Meas),]) 
    dat$type = c("min", "mid", "max") 
    
    library(reshape2) 
    dat1 = dcast(dat, Treatment + Temp ~ type, value.var = "Meas") 
    

然後繪製照例:

p = ggplot(dat1) + 
     geom_pointrange(aes(ymin=min, ymax=max, 
          y=mid,x=Treatment, group=Temp), 
      position=position_dodge(width=0.20)) 

位置參數停止被放置在每個頂部的線其他。這給出:

Example line range plot

+0

啊,position_dodge是命令。謝謝!我想我已經找到了引導'(函數(a,b)mean(a [b])'而不是僅僅使用「a」。「boot」包中的'boot.ci()'給了我5種不同類型的引導對於這種情況,「基本」是正確的嗎? – Mikko 2012-04-26 12:01:23

+0

對不起,從來沒有使用過這個包。 – csgillespie 2012-04-26 12:06:58

+1

我不得不第二個關於bootstrapping的警告/關注有三點...! – 2012-04-26 14:01:36