0
votes

Does anyone know a function/package that only shows dates when it is the first hour of the day presented in the axis? i.e., I would like to only show the date at 23:30 and 00:00, because they are the first our of July 21st and July 22nd, respectively.

1

I used ggplot and set the scale_x_datetime as follows:

scale_x_datetime(limits = c(ymd_hm('2020-07-21 23:24',
tz = tz_EDT),ymd_hm('2020-07-22 00:25',tz = tz_EDT)), 
date_breaks = '10 mins',date_labels = '%H:%M\n%b/%d',expand = c(0, 0))

I tried label_date_short from the scales package, but I personally don't like the format. I would like to only separate time and date(day/month/year or year/month/day), not time, day, month, and last year.

2

For the figure above, the code was as follows:

scale_x_datetime(limits = c(ymd_hm('2020-07-21 23:25',
tz = tz_EDT),ymd_hm('2020-07-22 00:25',tz = tz_EDT)),
date_breaks = '10 mins',labels = label_date_short(),expand = c(0, 0))
1

1 Answers

0
votes

My understanding is that R doesn't handle time data all too well, rather it prefers datetime data. This makes sense, as the same time is repeated each day whereas a datetime shouldn't be repeated, therefore you are less likely to duplicate a time data point.

Anyway moving onto your example, you can manually specify the 2 values that you want to show up on the x-axis. You need to specify them in datetime format, and then which format (ie YYYMMDD, YYMMDD-HHMM, HHMM etc) you want them to show in.

I recommend using the lubridate package for handling dates and (date)times, if you are not familiar check out ?lubridate, and especially the function we use in this example: ?ymd_hm. Basically, ymd_hm() converts a character spelled out as "year-month-day hour-minute", into a datetime object. The datetime object is printed as 'year-month-day hour:min:sec', even though we don't specify any 'seconds' data - don't worry about this, as by default seconds are zero (ie top of the minute).

I made some sample data including your datetime range:

library(tidyverse) # for the tibble() and pipes (%>%) functions 
library(lubridate)

tibble(
  datetime = seq(ymd_hm('2020-07-21 23:24'), ymd_hm('2020-07-22 00:25'), 'min'), # sample datetime sequence, with 1 min intervals
  value = rnorm(62, 1, 1) # random data
) %>%
  {. ->> my_data}

Then, when you build your ggplot, you will use scale_x_datetime and manually specify the breaks. The breaks need to be in datetime format, so we use ymd_hm() again. We specify the full datetime here. Then, you can specify the format of the axis labels to be displayed in using date_labels. This is where we tell scale_x_datetime to only show hour and minute, by using %H:%M notation (see https://www.stat.berkeley.edu/~s133/dates.html for a list of date and time formats).

my_data %>%
  ggplot(aes(datetime, value))+
  geom_line()+
  scale_x_datetime(
    breaks = c(ymd_hm(c('2020-07-21 23:30', '2020-07-22 00:00'))),
    date_labels = '%H:%M'
    )

enter image description here

Note: the position of the tick marks will always be datetime format, but how they are labelled can be changed. As an example, you could re-label the time points to a character vector such as:

my_data %>%
  ggplot(aes(datetime, value))+
  geom_line()+
  scale_x_datetime(
    breaks = c(ymd_hm(c('2020-07-21 23:30', '2020-07-22 00:00'))),
    # date_labels = '%H:%M'
    date_labels = c('t1', 't2')
    )

enter image description here