0

I'm currently plotting several datasets of one day in R. The format of the dates in the datasets is 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. What do I have to do to create a plot with a more "normal" date notation (e.g. June 1, 12:00 or just 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

  • 2016060125, 2016060150? year = 2016, month = 06, day = 1 and hours are equal to 25 and 50? Last time I checked there are only 24 hours per day so your hours should either run from 00 to 23 or 01 to 24. – James Thomas Durant Nov 22 '17 at 15:27
  • Well, the strange thing is that my day does have 24 hours in the data set, but only on the x-axis of my plot it gets this way... – Maaike300694 Nov 22 '17 at 15:31
  • `df$datetime <- strptime(df$datetime, "%Y%m%d%H")` First, you need to format your dates and then you can plot them. If you want to get a precise answer, provide a [minimal, reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – M-- Nov 22 '17 at 15:31
  • what does `head(df$datetime)` return? – James Thomas Durant Nov 22 '17 at 15:33
  • Edited question, example of dataset provided. – Maaike300694 Nov 22 '17 at 15:43

3 Answers3

0

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'

With the dataframe 'df' you can convert your date_int column to a date format with hours and update the dataframe:

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:

result plot

Ps.: Please note that you need to use abbreviations specified on "Date and Times in R" (e.g. "%Y%m%d%H" in this case)

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

Luis Martins
  • 1,572
  • 12
  • 11
0

Here a lubridate answer:

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

or base R:

event_1$date <- as.POSIXct(event_1$date, format = "%Y%d%d%H")
J_F
  • 9,956
  • 2
  • 31
  • 55
0

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)

xaxis numeric

Now using date_x which is POSIXct:

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

x-axis is date

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

enter image description here