2010-06-11 20 views
4

我有一個看起來像這樣的數據:使用R.zoo繪製錯誤吧多個系列

> head(data) 
      groupname ob_time dist.mean dist.sd dur.mean dur.sd ct.mean ct.sd 
     1  rowA  0.3 61.67500 39.76515 43.67500 26.35027 8.666667 11.29226 
     2  rowA 60.0 45.49167 38.30301 37.58333 27.98207 8.750000 12.46176 
     3  rowA 120.0 50.22500 35.89708 40.40000 24.93399 8.000000 10.23363 
     4  rowA 180.0 54.05000 41.43919 37.98333 28.03562 8.750000 11.97061 
     5  rowA 240.0 51.97500 41.75498 35.60000 25.68243 28.583333 46.14692 
     6  rowA 300.0 45.50833 43.10160 32.20833 27.37990 12.833333 14.21800 

每個組名是一個數據系列。因爲我想單獨繪製每個系列中,我把他們分開是這樣的:

> A <- zoo(data[which(groupname=='rowA'),3:8],data[which(groupname=='rowA'),2]) 
> B <- zoo(data[which(groupname=='rowB'),3:8],data[which(groupname=='rowB'),2]) 
> C <- zoo(data[which(groupname=='rowC'),3:8],data[which(groupname=='rowC'),2]) 

ETA:

Thanks to gd047: Now I'm using this: 

    z <- dlply(data,.(groupname),function(x) zoo(x[,3:8],x[,2])) 

產生的動物園物體看起來像這樣:

> head(z$rowA) 
      dist.mean dist.sd dur.mean dur.sd ct.mean ct.sd 
    0.3 61.67500 39.76515 43.67500 26.35027 8.666667 11.29226 
    60 45.49167 38.30301 37.58333 27.98207 8.750000 12.46176 
    120 50.22500 35.89708 40.40000 24.93399 8.000000 10.23363 
    180 54.05000 41.43919 37.98333 28.03562 8.750000 11.97061 
    240 51.97500 41.75498 35.60000 25.68243 28.583333 46.14692 
    300 45.50833 43.10160 32.20833 27.37990 12.833333 14.21800 

所以,如果我想繪製dist.mean對時間幷包含等於+/- dist.sd的每個系列的誤差線:

  • 我該如何結合A,B,C dist.mean和dist.sd?
  • 我該如何製作條形圖,或者更好的,生成的對象的折線圖?
+0

你的'zoo'堅持出於某種原因,或者你只是想要使用任何可用工具的情節? – Aniko 2010-06-11 18:57:58

+0

@Aniko:我不特別喜歡任何東西。目前我正在使用'動物園',因爲那是我讀書引導我的地方。 – dnagirl 2010-06-11 19:00:31

+2

關於你的第一個問題,這有幫助嗎?庫(plyr); zoo.obj < - dlply(data,。(groupname),function(x)zoo(x [,3:8],x [,2])) – 2010-06-11 19:18:29

回答

3

我沒有看到數據分解成三塊點只有擁有它的情節結合在一起。下面是使用ggplot2庫一個情節:

library(ggplot2) 
qplot(ob_time, dist.mean, data=data, colour=groupname, geom=c("line","point")) + 
    geom_errorbar(aes(ymin=dist.mean-dist.sd, ymax=dist.mean+dist.sd)) 

此空間沿天然鱗片的時間值,你可以使用scale_x_continuous在實際時間值來定義刻度線。讓它們間隔相等會更棘手:您可以將ob_time轉換爲一個因子,但是qplot拒絕用一條線連接這些點。

溶液1 - 條形圖:

qplot(factor(ob_time), dist.mean, data=data, geom=c("bar"), fill=groupname, 
     colour=groupname, position="dodge") + 
geom_errorbar(aes(ymin=dist.mean-dist.sd, ymax=dist.mean+dist.sd), position="dodge") 

解決方案2 - 添加使用1,2-手動線,...的係數的重新編碼:

qplot(factor(ob_time), dist.mean, data=data, geom=c("line","point"), colour=groupname) + 
    geom_errorbar(aes(ymin=dist.mean-dist.sd, ymax=dist.mean+dist.sd)) + 
    geom_line(aes(x=as.numeric(factor(ob_time)))) 
3

這是我嘗試去做的一種暗示。我忽略了分組,因此您必須修改它以包含多個系列。另外我還沒有使用動物園,因爲我不太瞭解。

g <- (nrow(data)-1)/(3*nrow(data)) 

plot(data[,"dist.mean"],col=2, type='o',lwd=2,cex=1.5, main="This is the title of the graph", 
xlab="x-Label", ylab="y-Label", xaxt="n", 
ylim=c(0,max(data[,"dist.mean"])+max(data[,"dist.sd"])), 
xlim=c(1-g,nrow(data)+g)) 
axis(side=1,at=c(1:nrow(data)),labels=data[,"ob_time"]) 

for (i in 1:nrow(data)) { 
lines(c(i,i),c(data[i,"dist.mean"]+data[i,"dist.sd"],data[i,"dist.mean"]-data[i,"dist.sd"])) 
lines(c(i-g,i+g),c(data[i,"dist.mean"]+data[i,"dist.sd"], data[i,"dist.mean"]+data[i,"dist.sd"])) 
lines(c(i-g,i+g),c(data[i,"dist.mean"]-data[i,"dist.sd"], data[i,"dist.mean"]-data[i,"dist.sd"])) 
} 

alt text

3

閱讀使用讀出的數據使用split =參數的.zoo可以通過groupname來分割它。然後綁定dist,lower和upper線。最後繪製它們。

Lines <- "groupname ob_time dist.mean dist.sd dur.mean dur.sd ct.mean ct.sd 
rowA  0.3 61.67500 39.76515 43.67500 26.35027 8.666667 11.29226 
rowA 60.0 45.49167 38.30301 37.58333 27.98207 8.750000 12.46176 
rowA 120.0 50.22500 35.89708 40.40000 24.93399 8.000000 10.23363 
rowA 180.0 54.05000 41.43919 37.98333 28.03562 8.750000 11.97061 
rowB 240.0 51.97500 41.75498 35.60000 25.68243 28.583333 46.14692 
rowB 300.0 45.50833 43.10160 32.20833 27.37990 12.833333 14.21800" 

library(zoo) 
# next line is only needed until next version of zoo is released 
source("http://r-forge.r-project.org/scm/viewvc.php/*checkout*/pkg/zoo/R/read.zoo.R?revision=719&root=zoo") 
z <- read.zoo(textConnection(Lines), header = TRUE, split = 1, index = 2) 

# pick out the dist and sd columns binding dist with lower & upper 
z.dist <- z[, grep("dist.mean", colnames(z))] 
z.sd <- z[, grep("dist.sd", colnames(z))] 
zz <- cbind(z = z.dist, lower = z.dist - z.sd, upper = z.dist + z.sd) 

# plot using N panels 
N <- ncol(z.dist) 
ylab <- sub("dist.mean.", "", colnames(z.dist)) 
plot(zz, screen = 1:N, type = "l", lty = rep(1:2, N*1:2), ylab = ylab) 
2

我不認爲你需要爲這種類型的情節創建動物園對象,我會直接從數據框。當然,使用動物園對象可能還有其他原因,比如智能合併,聚合等。

一種選擇是從latticeExtra

library(latticeExtra) 
segplot(ob_time ~ (dist.mean + dist.sd) + (dist.mean - dist.sd) | groupname, 
    data = data, centers = dist.mean, horizontal = FALSE) 
## and with the latest version of latticeExtra (from R-forge): 
trellis.last.object(segments.fun = panel.arrows, ends = "both", angle = 90, length = .1) + 
    xyplot(dist.mean ~ ob_time | groupname, data, col = "black", type = "l") 

segplot功能使用的Gabor的很好,可重複的數據集,這產生:

segplot http://i49.tinypic.com/2cf9kpz.png