Search code examples
rdatetimeggplot2axis-labelsposixct

How to use scale_y_datetime with difftime data?


Is there a way to use scale_y_datetime() in a horizontally stacked barplot of cumulative durations?

I have data of structured like this:

x1 = as.POSIXct("2020-08-01 12:00")
x2 = as.POSIXct("2020-08-01 16:00")
df = tibble::tibble(
  x = seq(dt_start, dt_end, length.out = 10) + rnorm(10, 0, sd = 300), 
  y = difftime(x, lag(x, 1))
) %>% 
  filter(!is.na(y))  # First lag(x, 1) is NA.

So df is:

# A tibble: 9 x 2
  x                   y            
  <dttm>              <drtn>       
1 2020-08-01 11:42:19 31.60503 mins
2 2020-08-01 12:09:29 27.17099 mins
3 2020-08-01 12:50:43 41.23540 mins
4 2020-08-01 13:10:45 20.03007 mins
5 2020-08-01 13:42:00 31.26120 mins
6 2020-08-01 14:24:41 42.67504 mins
7 2020-08-01 14:44:43 20.02577 mins
8 2020-08-01 15:15:10 30.45446 mins
9 2020-08-01 15:40:41 25.51719 mins

I plot this using a horizontally stacked barplot:

gg = ggplot(df, aes(x = 1, y = y, fill = as.factor(x))) + 
  geom_bar(stat = "identity") + 
  coord_flip()

enter image description here

Now I want to show the times in df$x on the x-axis. However, this fails:

gg + scale_y_datetime()

with the error

Error: Invalid input: time_trans works with objects of class POSIXct only

probably because df$x is a difftime object. I have tried various solutions but only got a very roundabout solution to work:

x_pos = seq(min(df$x), max(df$x), length.out = 10)
x_label = format(x_pos, "%H:%M")
gg + scale_y_continuous(breaks = as.numeric(x_pos - min(df$x)) / 60, labels = x_label)

enter image description here

This requires knowing the scale of your date range (here "mins" --> / 60) and you get no nice rounding of the timestamps. Is there a way to use scale_y_datetime()?


Solution

  • I think I'm missing the point on why you're using the time difference.. But I didn't get it from your question..

    Can't you go with this?

    library(dplyr)
    library(ggplot2)
    
    # reproducible example
    x1 <- Sys.time()
    x2 <- Sys.time() + 1000
    df <- tibble::tibble(x = seq(x1, x2, length.out = 10) + rnorm(10, 0, sd = 300))
    df <- df %>% mutate(x1 = lag(x)) %>% filter(!is.na(x1))
    
    
    # solution ?
    ggplot(df) +
        geom_rect(aes(xmin = x1, xmax = x, ymin = 0, ymax = 1, fill = factor(x)), show.legend = FALSE)
    

    enter image description here