使用 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?
- 如何制作条形图,或者更好,结果对象的折线图?
I have data that looks like this:
> 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
Each groupname is a data series. Since I want to plot each series separately, I've separated them like this:
> 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]))
The resulting zoo objects look like this:
> 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
So if I want to plot dist.mean against time and include error bars equal to +/- dist.sd for each series:
- how do I combine A,B,C dist.mean and dist.sd?
- how do I make a bar plot, or perhaps better, a line graph of the resulting object?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我不认为将数据分成三部分只是为了将它们组合在一起以绘制图表有什么意义。这是使用
ggplot2
库绘制的图:它沿着自然比例间隔时间值,您可以使用
scale_x_continuous
来定义实际时间值处的刻度线。让它们等间距比较棘手:您可以将 ob_time 转换为一个因子,但 qplot 拒绝用线连接这些点。解决方案 1 - 条形图:
解决方案 2 - 使用因子的 1,2,... 重新编码手动添加线条:
I don't see the point of breaking up the data into three pieces only to have to combine it together for a plot. Here is a plot using the
ggplot2
library:This spaces the time values along the natural scale, you can use
scale_x_continuous
to define the tickmarks at the actual time values. Having them equally spaced is trickier: you can convertob_time
to a factor, but thenqplot
refuses to connect the points with a line.Solution 1 - bar graph:
Solution 2 - add lines manually using the 1,2,... recoding of the factor:
这是我尝试这样做的方式的提示。我忽略了分组,因此您必须修改它以包含多个系列。我也没有去过动物园,因为我了解不多。
This is a hint of the way I would try to do it. I have ignored grouping, so you'll have to modify it to include more than one series. Also I haven't used zoo cause I don't know much.
使用带有 split= 参数的 read.zoo 读取数据,以按组名拆分数据。然后将距离、下线和上线绑在一起。最后绘制它们。
Read the data in using read.zoo with the split= argument to split it by groupname. Then bind together the dist, lower and upper lines. Finally plot them.
我认为您不需要为这种类型的图创建动物园对象,我会直接从数据框中创建动物园对象。当然,可能还有其他原因需要使用 Zoo 对象,例如智能合并、聚合等。
一种选择是latticeExtra 中的 segplot 函数,
使用 Gabor 的可重现性良好的数据集会生成:
I don't think you need to create zoo objects for this type of plot, I would do it directly from the data frame. Of course, there may be other reasons to use zoo objects, such a smart merging, aggregation, etc.
One option is the
segplot
function from latticeExtraUsing Gabor's nicely-reproducible dataset this produces: