Plotting datetime

Note: I'm a newbie

I have a dataset (rows number =69) with a datetime column in the form:
e.g.: "2018-06-07 08:16:11 CEST"

I've converted it like this:
dataset$DateTime <- as.POSIXct(dataset$DateTime, format='%Y-%d-%m %H:%M:%S')

I'd like to plot it with x=date time and y=number of rows

first problem: i dont know how to do that
second problem: when I try to plot(dataset$DateTime) the graph that i get mixes up the day with the month
so for example in the case above, month will be 06(june) instead of 07(july), and day will be 07, even though i did set the format they way i wrote above.

And in any case, coming back to problem 1, even if day and month were correct i still wouldnt get the graph i want, because in x i have number of rows, in y i have datetime

Thank you in advance, dont talk too much technical please :stuck_out_tongue:

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

Thank you for your help, I think i'm going to do a different thing so never mind the question, but next time I'll post something I'll make sure to add data and code. Have a good day

1 Like

Perhaps this thread will be of interest to you?