简体   繁体   English

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

[英]Weird date-time format on x-axis plot

I'm currently plotting several datasets of one day in R. The format of the dates in the datasets is yyyymmmdddhh. 我目前正在绘制R中一天的多个数据集。数据集中的日期格式为yyyymmmdddhh。 When I plot this, the formatting fails gloriously: on the x-axis, I now have 2016060125, 2016060150, etc. and a very weirdly shaped plot. 当我绘制此图形时,格式化非常失败:在x轴上,我现在有2016060125、2016060150等,还有一个形状很奇怪的图形。 What do I have to do to create a plot with a more "normal" date notation (eg June 1, 12:00 or just 12:00)?? 创建具有更“正常”日期符号(例如6月1日12:00或仅12:00)的绘图,我该怎么做?

Edit: the dates of these datasets are integers 编辑:这些数据集的日期是整数

The dataset looks like this: 数据集如下所示:

> 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

I want to plot the date on the x-axis and the Q on the y-axis 我想在x轴上绘制日期,在y轴上绘制Q

Create a minimal verifiable example with your data: 使用您的数据创建一个最小的可验证示例:

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)

So, now we have a dataframe 'df' 所以,现在我们有了一个数据框“ df”

With the dataframe 'df' you can convert your date_int column to a date format with hours and update the dataframe: 使用数据框“ df”,您可以将date_int列转换为带小时的日期格式,并更新数据框:

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

Finally, 最后,

plot(df)

You will see a nice plot! 您会看到一个不错的情节! Like the following: 如下所示:

结果图

Ps.: Please note that you need to use abbreviations specified on "Date and Times in R" (eg "%Y%m%d%H" in this case) 附言:请注意,您需要使用“ R中的日期和时间”中指定的缩写(例如,在这种情况下为“%Y%m%d%H”)

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

Here a lubridate answer: 这是一个lubridate答案:

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

or base R: 或以R为基数:

event_1$date <- as.POSIXct(event_1$date, format = "%Y%d%d%H")

What is happening is the dates are getting interpreted as numeric classes. 发生的事情是日期被解释为数字类。 As indicated, you need to convert. 如所示,您需要转换。 To get the formatting correct, you need to do a little more: 为了使格式正确,您需要做更多的事情:

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)

X轴数值

Now using date_x which is POSIXct: 现在使用date_x,即POSIXct:

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

x轴是日期

You will need to fix your date axis to get the format you desire: 您将需要修复日期轴以获得所需的格式:

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

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM