在R中绘制四分位间距的平均和垂直误差条的时间序列 [英] Plot time series with mean and vertical error bars of interquartile range by column in R

查看:594
本文介绍了在R中绘制四分位间距的平均和垂直误差条的时间序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个矩阵,想要绘制四分位数范围的垂直误差条和矩阵的列平均值。我如何在R中执行此操作,尤其是ggplot2?示例矩阵如下:

  ##创建矩阵
B =矩阵(c(2,4,3) ,1,5,7,5,3,8,3,7,3),nrow = 4,ncol = 3)
##创建动物园对象
B2 <-as.zoo(B)
colnames(B2)< - c(A,B,C)

B2
ABC
2 5 8
4 7 3
3 5 7
1 3 3
##列的日期:
日期< -as.yearmon(seq(as.Date(2000/1 / 1),by =month,length.out = 3))

时间序列图,但每个时间戳都有一个基于行的垂直IQR误差条。以下是我尝试实现的结果示例'但是,不是在x轴上有城镇,而是我将有行ID或日期。

解决方案

我不得不走很长的路这就是我所做的(使用25%和75%的百分点):

  ##创建矩阵
B =矩阵(c(2,4,3,1,5,7,5,3,8,3,7,3),nrow = 4,ncol = 3)
##创建数据帧
B2 <-as.data.frame(B)
colnames(B2)<-c(A,B,C)
##创建日期序列
# #按行计算分位数
D <-apply(B2,2,quantile)
##选择第一和第三四分位数(25%和75%)并转置以使它们成为列。
D2< -t(D [c(2,4),])
##平均值
CO< -apply(B2,2,mean)
DM< -as .data.frame(cbind(D2,CO))
##创建日期
Date< -as.character(as.yearmon(seq(as.Date(2000/1/1), (DM)< -c(Q1,=,length使用ggplot2
library(ggplot2)
ggplot(DM,aes(x =日期,y = CO)中的Q3,CO,Date)

## ,group = 1,color =CO))+
geom_errorbar(aes(ymin = Q1,ymax = Q3),width = .1)+
geom_point(size = 3)+
geom_line(aes())

下面是连接每月平均值的时间序列线的结果:



如果有任何想法,我不会介意这样做的更简单的方法。 / p>

I have a matrix and want to plot vertical error bars of the interquartile range and mean by column from the matrix. How do I do this in R especially ggplot2, please? A sample matrix is given below:

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

I want a time series plot but with a row based vertical IQR error bar for each timestamp. Here's a sample of the outcome I am trying to achieve' However, rather than have towns on the x-axis, I will have the row id or date.

解决方案

I had to go the long way round with this so here's what I did (Using the 25% and 75% percentile):

##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())  

Here's the result with the time series line connecting the mean for each month:

I won't mind an easier way of doing this if there are any ideas.

这篇关于在R中绘制四分位间距的平均和垂直误差条的时间序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆