用R中的列绘制四分位范围的平均和垂直误差条的时间序列

用R中的列绘制四分位范围的平均和垂直误差条的时间序列,r,ggplot2,time-series,errorbar,quartile,R,Ggplot2,Time Series,Errorbar,Quartile,我有一个矩阵,想要绘制四分位范围的垂直误差条,并从矩阵中按列平均。请问,在R-2里我该怎么做?下面给出了一个示例矩阵: ##Create matrix B = matrix (c(2,4,3,1,5,7,5,3,8,3,7,3),nrow=4,ncol=3) ##Create zoo object B2<-as.zoo(B) colnames(B2)<- c("A","B","C") B2 A B C 2 5 8

我有一个矩阵,想要绘制四分位范围的垂直误差条,并从矩阵中按列平均。请问,在R-2里我该怎么做?下面给出了一个示例矩阵:

 ##Create matrix
 B = matrix (c(2,4,3,1,5,7,5,3,8,3,7,3),nrow=4,ncol=3) 
 ##Create zoo object
 B2<-as.zoo(B)
 colnames(B2)<- c("A","B","C")

 B2
          A B C
          2 5 8
          4 7 3
          3 5 7
          1 3 3
##The Dates for the columns:
Date<-as.yearmon (seq(as.Date("2000/1/1"), by = "month", length.out = 3))
我想要一个时间序列图,但是每个时间戳都有一个基于行的垂直IQR错误条。这是我试图实现的结果的一个示例,但是,我将使用行id或日期,而不是将城镇放在x轴上。

我不得不花很长的时间来处理这个问题,下面是我使用25%和75%的百分位数所做的:

##Create matrix
B = matrix (c(2,4,3,1,5,7,5,3,8,3,7,3),nrow=4,ncol=3) 
##Create dataframe
B2<-as.data.frame(B)
colnames(B2)<- c("A","B","C")
##Create date sequence
##To compute quantile by row
D<-apply(B2,2,quantile)
##Select 1st and 3rd quartile (25% and 75%) and transpose to make them columns.
D2<-t(D[c(2,4),])
##Mean
CO<-apply(B2,2,mean)
DM<-as.data.frame(cbind(D2,CO))
##Create dates
Date<-as.character(as.yearmon (seq(as.Date("2000/1/1"), by = "month", length.out = 3)))
##Add to dataframe 
DM$Date<-Date
colnames(DM)<-c("Q1","Q3","CO","Date")

##Plot using ggplot2 
library(ggplot2) 
ggplot(DM, aes(x=Date, y=CO,group=1,colour="CO")) + 
geom_errorbar(aes(ymin=Q1, ymax=Q3), width=.1) +
    geom_point(size=3) +
geom_line(aes())  
以下是时间序列线连接每个月平均值的结果:


如果有什么想法的话,我不介意用一种更简单的方法来做这件事。

那么你基于时间的列是什么?它是一个动物园对象,所以行名是日期。是的,所以这应该更接近你的目标,不确定如何在ggplot中处理这个问题,但动态图是好的。libraryxts librarydygraphs ts_对象在ggplot2中绘制垂直线查看几何线或几何线段,也许您可以通过某种方式调整每个条的高度?看看这个链接docs.ggplot2.org/0.9.3.1/geom_vline.htmltanks,@InfiniteFlashChess。然而,动态图将其绘制为三个不同的时间序列。我想要一个例子,它是一个具有4个时间戳的单一时间序列,但是在每个时间戳处有一个错误条,包括每个时间戳的a、B和C的平均值和错误条。