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