Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 860
Aaron M Avatar asked Oct 16 '25 07:10

Aaron M


1 Answers

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

like image 122
zx8754 Avatar answered Oct 18 '25 20:10

zx8754



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!