Ggplot - Time

About

Ggplot supports:

  • the date class
  • the PosixCt class (DateTime)
  • the hms class (Only the time part)

Scale

GGplot - Scale (Data Transformation): for date_breaks, and date_format you need package scales:

library(scales)
scale_x_datetime(breaks = date_breaks("1 sec"), labels = date_format("%S"))
scale_x_datetime(breaks = date_breaks("1 sec"), labels = date_format("%OS3"))
scale_x_datetime(breaks = date_breaks("4 sec"), labels = date_format("%Hh %M:%S"))

Example

scale_y_datetime(
    labels = date_format("%H:%M"),
    breaks = date_breaks("2 hour"),
    expand = c(0, 0),
    limits = c(
      as.POSIXct(paste(Sys.Date() - 1, "18:00:00")),
      as.POSIXct(paste(Sys.Date(), "18:00:00")))) +
  scale_x_datetime(
    breaks = date_breaks(
      paste(round(interval(min(tt.df$date), now()) / months(1)) / 9, "months")),
    labels = ifelse(length(tt.years) > 1, date_format("%b %y"), date_format("%b")),
    expand = c(0, 0))

Powered by ComboStrap