1

I would like to add concise date and time info to my plot in R.

I am adding this plot to a research paper and when it's shrunk to fit the template it loses some of it's info.

My actual datetime range is 20/07/2017 18:15 - 23/07/2017 21:15

I'd like to abbreviate the date to days such as Thur 18:15 and Sun 21:15 with 5 days and times in between.

I can create the correct range in POSIXLT format but it's too big for my needs.

my.date <- seq(as.POSIXlt(strptime('20/07/2017 18:15',"%d/%m/%Y %H:%M"),tz="GMT"), as.POSIXlt(strptime('23/07/2017 21:15',"%d/%m/%Y %H:%M"),tz="GMT"),length.out = 7)

Is there a better way to achieve this datetime rage?

TheGoat
  • 2,587
  • 3
  • 25
  • 58

1 Answers1

1

The key for this problem is convert the POSIX object into an character string with the desired format. The format function is used here: format(my.date, "%a %H:%M")

Here is a simple example:

my.date <- seq(strptime('20/07/2017 18:15',"%d/%m/%Y %H:%M"), 
               strptime('23/07/2017 21:15',"%d/%m/%Y %H:%M"), length.out = 7)

#x axis labels in the desired format
labels<-format(my.date, "%a %H:%M")

#simple example with base graphics
y<-2:8
plot(my.date,y, axes=FALSE)
#draw x and y axis
axis(1, at=my.date, labels=labels)
axis(2, at=y)
Dave2e
  • 22,192
  • 18
  • 42
  • 50