4

我的数据如下所示:

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

预计到达时间:

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?
  • 如何制作条形图,或者更好的是结果对象的折线图?
4

4 回答 4

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

替代文字

于 2010-06-12T10:00:06.630 回答
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))))
于 2010-06-13T17:34:14.337 回答
3

使用带有 split= 参数的 read.zoo 读取数据以按组名拆分。然后将 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)
于 2010-06-14T03:38:10.587 回答
2

我认为您不需要为这种类型的绘图创建动物园对象,我会直接从数据框中进行操作。当然,使用 zoo 对象可能还有其他原因,比如智能合并、聚合等。

一种选择是segplot来自 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")

使用 Gabor 的重现性良好的数据集,这会产生:

分段图

于 2010-06-14T12:59:46.513 回答