Plotting datetime

Hi! Welcome!

For the first problem (correctly reading the date times), my first thought is: were you expecting R to print the dates according to the format you told it? (this is the "is it plugged in" question, so I apologize if it seems trivial!) R reads the dates according to that format, but by default it prints the dates as YYYY-MM-DD.

If I run your exact line of code as.POSIXct(dataset$DateTime, format='%Y-%d-%m %H:%M:%S') on the example text you gave "2018-06-07 08:16:11 CEST", the date is read correctly (though in the wrong time zone, since you need to provide time zone info separately):

# Date is read correctly as July 6, 2018, but time zone is my computer's time zone
as.POSIXct("2018-06-07 08:16:11 CEST", format = '%Y-%d-%m %H:%M:%S')
#> [1] "2018-07-06 08:16:11 PDT"

# Date is read correctly as July 6, 2018, with correct time zone
as.POSIXct("2018-06-07 08:16:11 CEST", tz = "Europe/Brussels", format='%Y-%d-%m %H:%M:%S')
#> [1] "2018-07-06 08:16:11 CEST"

# To prove that R understands the date correctly,
# we can ask for the datetime to be output as text in "MonthName Day, Year" format:
format(
  as.POSIXct("2018-06-07 08:16:11 CEST", tz = "Europe/Brussels", format='%Y-%d-%m %H:%M:%S'),
  format = "%B %e, %Y"
)
#> [1] "July  6, 2018"

Created on 2018-07-12 by the reprex package (v0.2.0).

For the plotting question, it's really difficult to answer this sort of thing without seeing your data and your code. This is why we usually ask people to provide a reproducible example of their problem. See this FAQ: FAQ: Tips for writing R-related questions. For your problem, it would be very helpful to have:

  • Your sample data
  • The code you tried for converting the data and making the plot (it's OK if it doesn't work!)

Check the link above for explanations of how to provide this info.

For plots specifically, it can also be really helpful to have an example of the kind of plot you're imagining (this can be a link to a plot you've seen somewhere else, or even just a quick sketch).

3 Likes