r ggplot not recognizing date format

I have the following chart.

p1 <- ggplot(data = mydat, aes(x = time))+
  geom_line(aes(y = sumabsdiff, colour = 'sumabsdiff'))+
  geom_line(aes(y = windsize, col='windsize'))+
  scale_x_time(breaks = scales::date_breaks('1 sec'))+ #('15 secs'))+
  scale_color_manual(values=c('sumabsdiff' = 'black',
                              "windsize" = "red"))+
  theme(legend.position = "top")

enter image description here

As you can see, the date is all messed up even though time seems perfectly fine to me.

> mydat$time
[1] "2022-09-19 12:44:47 UTC" "2022-09-19 12:44:48 UTC" "2022-09-19 12:44:49 UTC" "2022-09-19 12:44:50 UTC"
[5] "2022-09-19 12:44:50 UTC" "2022-09-19 12:44:50 UTC".

Any idea why?

Data:

mydf <- structure(list(time = structure(c(1663591487.801, 1663591488.614, 
1663591489.626, 1663591490.097, 1663591490.202, 1663591490.717
), class = c("POSIXct", "POSIXt"), tzone = "UTC"), bid = c(11735.68, 
11735.18, 11734.93, 11734.43, 11734.3, 11734.43), ask = c(11737.58, 
11737.08, 11736.83, 11736.33, 11736.2, 11736.33), flags = c(6, 
6, 6, 6, 6, 6), typical = c(11736.63, 11736.13, 11735.88, 11735.38, 
11735.25, 11735.38), row = 266:271, prevrow_short = c(258L, 258L, 
260L, 261L, 262L, 265L), windsize = c(9, 10, 9, 9, 9, 7), diff = c(-0.119999999998981, 
-0.5, -0.25, -0.5, -0.130000000001019, 0.130000000001019), absdiff = c(0.119999999998981, 
0.5, 0.25, 0.5, 0.130000000001019, 0.130000000001019), sumabsdiff = c(3.60999999999694, 
4.10999999999694, 3.72999999999593, 3.85999999999694, 3.61999999999898, 
2.13000000000102), positive = c(FALSE, FALSE, FALSE, FALSE, FALSE, 
TRUE), meanpos = c(0.444444444444444, 0.4, 0.333333333333333, 
0.222222222222222, 0.222222222222222, 0.285714285714286), posdiff = c(0, 
0, 0, 0, 0, 0.130000000001019), negdiff = c(0.119999999998981, 
0.5, 0.25, 0.5, 0.130000000001019, 0), sumposdiff_short = c(1.36999999999898, 
1.36999999999898, 1.23999999999796, 0.869999999998981, 0.869999999998981, 
0.630000000001019), sumnegdiff_short = c(2.23999999999796, 2.73999999999796, 
2.48999999999796, 2.98999999999796, 2.75, 1.5), power_short = c(0.37950138504159, 
0.333333333333333, 0.332439678283999, 0.225388601036184, 0.240331491712493, 
0.295774647887661), market_open = c(FALSE, FALSE, FALSE, FALSE, 
FALSE, FALSE), timediff = c(0.219000101089478, 0.812999963760376, 
1.01199984550476, 0.470999956130981, 0.105000019073486, 0.515000104904175
), avgspeed = c(2.71247733209185, 2.42072135038066, 2.44698207323042, 
2.3255814641193, 2.8019926211971, 2.14658085742225), relative_positive_diff = c(0.37950138504159, 
0.333333333333333, 0.332439678283999, 0.225388601036184, 0.240331491712493, 
0.295774647887661), timesec = structure(c(1663591487, 1663591488, 
1663591489, 1663591490, 1663591490, 1663591490), class = c("POSIXct", 
"POSIXt"), tzone = "UTC")), pandas.index = <environment>, row.names = 266:271, class = "data.frame")

By the way, the time actually includes milliseconds, perhaps that is the cause

>Solution :

?scale_x_time:

 These are the default scales for the three date/time class. These
 will usually be added automatically. To override manually, use
 scale_*_date for dates (class 'Date'), scale_*_datetime for
 datetimes (class 'POSIXct'), and scale_*_time for times (class
 'hms').

Your time variable is class POSIXt, not hms, so you should be using scale_x_datetime instead.

ggplot(data = mydf, aes(x = time))+
  geom_line(aes(y = sumabsdiff, colour = 'sumabsdiff'))+
  geom_line(aes(y = windsize, col='windsize'))+
  scale_x_datetime(breaks = "1 secs") +
  scale_color_manual(values=c('sumabsdiff' = 'black',
                              "windsize" = "red"))+
  theme(legend.position = "top")

ggplot with x-axis set to POSIX class

You can format the axis labels with date_labels= and %-codes (listed in ?strptime):

ggplot(data = mydf, aes(x = time))+
  geom_line(aes(y = sumabsdiff, colour = 'sumabsdiff'))+
  geom_line(aes(y = windsize, col='windsize'))+
  # scale_x_datetime(breaks = scales::date_breaks('1 sec'))+ #('15 secs'))+
  scale_x_datetime(breaks = "1 sec", date_labels = "%H:%M:%S") +
  scale_color_manual(values=c('sumabsdiff' = 'black',
                              "windsize" = "red"))+
  theme(legend.position = "top")

date labels

Leave a Reply