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

时间:2017-11-22 15:21:27

标签: r date plot format axis

我目前正在R中绘制一天的几个数据集。数据集中日期的格式为yyyymmmdddhh。当我绘制这个时,格式化失败光彩照人:在x轴上,我现在有2016060125,2016060150等等,形状非常奇怪。我需要做些什么来创建一个更多" normal"日期表示法(例如6月1日,12:00或12点)?

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

数据集如下所示:

> 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 个答案:

答案 0 :(得分:0)

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

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_int列转换为包含小时的日期格式并更新数据框:

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

最后,

plot(df)

你会看到一个不错的情节!如下所示:

result plot

Ps。:请注意,您需要使用“R中的日期和时间”中指定的缩写(例如“%Y%m%d%H”)

参考:https://www.stat.berkeley.edu/~s133/dates.html

答案 1 :(得分:0)

这是一个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")

答案 2 :(得分:0)

发生的事情是日期被解释为数字类。如上所示,您需要转换。要使格式正确,您需要做更多的事情:

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

现在使用POS_ct的date_x:

#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