代码之家  ›  专栏  ›  技术社区  ›  Maaike300694

x轴图上奇怪的日期时间格式

  •  0
  • Maaike300694  · 技术社区  · 7 年前

    我目前正在用R绘制几组一天的数据集。数据集中的日期格式是YYYYMMDDDHH。当我绘制这幅图时,格式化失败了:在x轴上,我现在有2016060125、2016060150等等,还有一个形状非常奇怪的图。我需要做什么才能创建一个具有更“正常”日期符号(例如6月1日、12:00或仅12:00)的绘图??

    编辑:这些数据集的日期是整数

    数据集如下所示:

    > event_1
             date    P ETpot       Q    T     fXS GRM_SintJorisweg
    1  2016060112  0.0 0.151 0.00652 19.6 0.00477          0.39250
    2  2016060113  0.0 0.134 0.00673 20.8 0.00492          0.38175
    3  2016060114  0.0 0.199 0.00709 22.6 0.00492          0.36375
    4  2016060115  0.0 0.201 0.00765 21.2 0.00492          0.36850
    5  2016060116 19.4 0.005 0.00786 19.5 0.00492          0.36900
    6  2016060117  2.8 0.005 0.00824 18.1 0.00492          0.36625
    7  2016060118  2.6 0.017 0.00984 18.0 0.00508          0.35975
    8  2016060119  9.7 0.000 0.01333 16.7 0.00555          0.34750
    9  2016060120  7.0 0.000 0.01564 16.8 0.00524          0.33550
    10 2016060121  4.1 0.000 0.01859 17.1 0.00524          0.32000
    11 2016060122  9.5 0.000 0.02239 17.2 0.00539          0.30250
    12 2016060123  2.6 0.000 0.03330 17.5 0.00555          0.27050
    13 2016060200 11.6 0.000 0.03997 17.4 0.00555          0.23800
    14 2016060201  0.9 0.000 0.04928 17.3 0.00555          0.21725
    15 2016060202  0.0 0.000 0.05822 17.2 0.00555          0.20350
    16 2016060203  2.3 0.002 0.06547 16.4 0.00555          0.18575
    17 2016060204  0.0 0.016 0.07047 16.5 0.00555          0.16950
    18 2016060205  0.0 0.027 0.07506 16.7 0.00555          0.16475
    19 2016060206  0.0 0.070 0.07762 18.0 0.00555          0.16525
    20 2016060207  0.0 0.285 0.08006 19.5 0.00555          0.14500
    21 2016060208  0.0 0.224 0.08109 20.3 0.00555          0.15875
    22 2016060209  0.0 0.362 0.07850 21.3 0.00555          0.17825
    23 2016060210  0.0 0.433 0.07441 22.0 0.00524          0.19175
    24 2016060211  0.0 0.417 0.07380 23.9 0.00492          0.19050
    

    我想在x轴上绘制日期,在y轴上绘制Q

    3 回复  |  直到 7 年前
        1
  •  0
  •   Luis Martins    7 年前

    用您的数据创建一个最小的可验证示例:

    date_int <- c(2016060112,2016060113,2016060114,2016060115,2016060116,2016060117,2016060118,2016060119,2016060120,2016060121,2016060122,2016060123,2016060200,2016060201,2016060202,2016060203,2016060204,2016060205,2016060206,2016060207,2016060208,2016060209,2016060210,2016060211)
    Q <- c(0.00652,0.00673,0.00709,0.00765,0.00786,0.00824,0.00984,0.01333,0.01564,0.01859,0.02239,0.0333,0.03997,0.04928,0.05822,0.06547,0.07047,0.07506,0.07762,0.08006,0.08109,0.0785,0.07441,0.0738)
    df <- data.frame( date_int, Q)
    

    现在我们有了一个数据帧“df”

    使用数据框“df”,您可以将date\u int列转换为带有小时数的日期格式,并更新数据框:

    date_time <- strptime(df$date_int, format = '%Y%m%d%H', tz= "UTC")
    df$date_int <- date_time
    

    最后

    plot(df)
    

    你会看到一个很好的情节!如下所示:

    result plot

    注:请注意,您需要使用“R中的日期和时间”上指定的缩写(例如,在这种情况下为%Y%m%d%H)

    裁判。: https://www.stat.berkeley.edu/~s133/dates.html

        2
  •  0
  •   J_F    7 年前

    这里是a lubridate 答案:

    library(lubridate)
    event_1$date <- ymd_h(event_1$date)
    

    或基数R:

    event_1$date <- as.POSIXct(event_1$date, format = "%Y%d%d%H")
    
        3
  •  0
  •   James Thomas Durant    7 年前

    现在的情况是,日期被解释为数字类。如前所述,您需要转换。要获得正确的格式,您需要执行更多操作:

    set.seed(123)
    library(lubridate)
    ##     date
    x <- ymd_h(2016060112)
    y <- ymd_h(2016060223)
    
    dfx <- data.frame(
      date = as.numeric(format(seq(x, y, 3600), "%Y%m%d%H")),
      yvar = rnorm(36))
    
    
    dfx$date_x <- ymd_h(dfx$date)
    
    
    # plot 1
    plot(dfx$date, dfx$yvar)
    

    xaxis numeric

    现在使用date_x,即POSIXct:

    #plot 2
    # converted to POSIXct
    class(dfx$date_x)
    ## [1] "POSIXct" "POSIXt"
    plot(dfx$date_x, dfx$yvar)
    

    x-axis is date

    您需要固定日期轴以获得所需的格式:

    #plot 3
    # using axis.POSIXct to help things
    
    with(dfx, plot(date_x, yvar, xaxt="n"))
    r <- round(range(dfx$date_x), "hours")
         axis.POSIXct(1, at = seq(r[1], r[2], by = "hour"), format = "%b-%d %H:%M")
    

    enter image description here