Aaron M Aaron M - 1 month ago 13
R Question

No leading zero in hour using scale_x_datetime

Is it possible to strip the leading zero from the hour as displayed on the x axis, while using ggplot2's scale_x_datetime?

Answer Source

We can use date_format from scales package:

library(ggplot2)
library(scales)

# dummy data
set.seed(1)
df1 <- data.frame(
  x = as.POSIXct(runif(10, 0, 24 * 60 * 60), origin = "2017/04/20"),
  y = 1:10
)

# use scales::date_format
ggplot(df, aes(x = x, y = y)) + 
  geom_point() + 
  scale_x_datetime(labels = date_format("%l:%M"))

?strptime:

%k The 24-hour clock time with single digits preceded by a blank.
%l The 12-hour clock time with single digits preceded by a blank.

enter image description here