blehman
blehman

Reputation: 1970

Can POSIXct date objects be converted to a negative number of seconds to be graphed using ggplot?

Using the following data:

$cat spike.csv
time,ts,count
2013-04-30 23:58:55,1367366335,32
2013-04-30 23:58:57,1367366337,664
2013-04-30 23:59:03,1367366343,4892
2013-04-30 23:59:04,1367366344,5185
2013-04-30 23:59:09,1367366349,4548
2013-04-30 23:59:10,1367366350,4154
2013-04-30 23:59:22,1367366362,1750
2013-04-30 23:59:23,1367366363,1720
2013-04-30 23:59:24,1367366364,1624
2013-04-30 23:59:32,1367366372,1152
2013-04-30 23:59:33,1367366373,1217
2013-04-30 23:59:59,1367366399,704
2013-05-01 00:00:00,1367366400,642
2013-05-01 00:00:01,1367366401,688
2013-05-01 00:00:02,1367366402,682
2013-05-01 00:00:03,1367366403,660
2013-05-01 00:00:09,1367366409,594
2013-05-01 00:00:10,1367366410,554
2013-05-01 00:02:09,1367366529,259
2013-05-01 00:02:10,1367366530,281
2013-05-01 00:02:11,1367366531,242
2013-05-01 00:02:12,1367366532,280
2013-05-01 00:02:25,1367366545,252
2013-05-01 00:02:26,1367366546,273

Using R, I create POSIXct objects for the time column.

>spike<-read.delim("NEED/stack.csv",sep=",")
>spike$time<-as.POSIXct(spike$time, format='%Y-%m-%d %H:%M:%S')

Then I graph the data using ggplot:

>ggplot(spike,aes(x=time,y=count))+
   geom_point(size=3)+
   geom_point(size=2,color="cyan")+
   geom_line()+
   theme(axis.text.x = element_text(siz=10))+
   xlab("Time (mins)")

enter image description here

I'd like the x-axis to have breaks specifically at one minute intervals and for values less than 00:00, I'd like to see negative values in mins. Suggested breaks: -00:02, -00:01, 00:00, 00:01, 00:002. Any suggestions would be greatly appreciated. Thoughts?

Upvotes: 2

Views: 386

Answers (3)

agstudy
agstudy

Reputation: 121598

As mentioned by other, you try to to represent intervals or durations as a datetime. ggplot2 don't know how to deal with difftime ( maybe I am wrong) ,So my idea is to plot your data as is ( count vs time) and format the datetime axis using a custom formatter.

Within the formatter , I compute the intervals to a certain origin and a format the time according to the sign of the intervals.

enter image description here

## origin :a reference date 
## forma  :you can give here any date format 
negative_date_format <- function (origin,forma = "%H:%M"){
  function(x) {
    x <- difftime(x,origin,units='secs')
    y <- DF$time[13] + abs(x)
    mapply(format,y, ifelse(sign(x) >= 0, forma , 
             paste0("[",forma,"]"))) ## or paste0("-",forma) to get -00:01 
  }
}

ggplot(DF,aes(x=time,y=count))+
  geom_point(size=3)+
  geom_point(size=2,color="cyan")+
  geom_line()+
  theme(axis.text.x = element_text(siz=15))+
  scale_x_datetime(labels = negative_date_format(origin=DF$time[13],forma = "%H:%M"))+
  xlab("Time (mins)")

Upvotes: 0

Roland
Roland

Reputation: 132959

You should be aware, that you don't want to plot times but time differences/durations, which is something completely different.

spike$timediff <- as.numeric(difftime(spike$time,
                                      as.POSIXct("2013-05-01 00:00:00", format='%Y-%m-%d %H:%M:%S'),
                                      units="mins"))

ggplot(spike,aes(x=timediff,y=count))+
  geom_point(size=3)+
  geom_point(size=2,color="cyan")+
  geom_line()+
  theme(axis.text.x = element_text(siz=10))+
  xlab("Time (mins)")

Upvotes: 1

Thomas
Thomas

Reputation: 44555

You can construct the sequence of xlab values with something like:

x <- format(as.POSIXct(seq(-3600,0,60),origin="2012-12-31 0:00:00"),"%H:%M")
negx <- rev(paste0("-",x))
c(negx[1:length(negx)-1],x)

Upvotes: 0

Related Questions