【问题标题】:plot Matrix data in R在 R 中绘制矩阵数据
【发布时间】:2014-12-01 13:18:42
【问题描述】:

我有整数值的数据(矩阵)。 列名是 "00","01","02","03" 行名是“01042014”、“02042014”

                          00       01       02    Total
           01042014 53114424 28401012 16445913 14235413
           02042014 53114424 28401012 16445913 14235413

这就是我所做的,plot(rownames(data),data[,"Total")),但是情节看起来难以理解。 实际上,rownames(data) 是日期,我想将它与 Total 列进行对比。

【问题讨论】:

    标签: r


    【解决方案1】:

    您可以创建一个日期向量:

    vec.dates<-rownames(data_mat)
    vec.dates<-as.Date(as.character(vec.dates), "%d%m%Y") # This is assuming your dates 
                        ## are dd-mm-YYYY. if mm-dd-YYYY, change the last part to "%m%d%Y"
    
    # Then plot against this new vector
    plot(vec.dates,data[,"Total"])
    

    拥有vec.dates 后,您可以将其附加到数据框。

    G.

    【讨论】:

      【解决方案2】:

      你可以试试:

      首先,按日期对数据进行排序:

      data_mat<-data_mat[order(strptime(rownames(data_mat),format="%d%m%Y")),]
      # this code supposes that your dates are with day first, then month and year. In case the 2 first figures are for the month, replace "%d%m%Y" by "%m%d%Y".
      

      然后,绘制您的数据:

      plot(1:nrow(data_mat),data_mat$Total,axes=F,xlab="Date",ylab="Total",xlim=c(0,nrow(data_mat)+1))
      box()
      axis(1,at=1:nrow(data_mat),labels=rownames(data_mat))
      axis(2,las=1)
      

      【讨论】:

      • 如果 df 未按日期排序,这会起作用吗?恐怕它会以错误的顺序绘制点。
      • @PavoDive,你是对的,我的代码假设数据是按日期排序的。我将其更新为首先按日期对数据进行排序,以防它们尚未排序。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-12-05
      • 1970-01-01
      • 1970-01-01
      • 2020-10-22
      • 1970-01-01
      • 2017-09-16
      • 1970-01-01
      相关资源
      最近更新 更多