user534498
user534498

Reputation: 3984

How to plot binary states time-series in R?

My data is some time-stamped binary states. E.g.,

31/01/2014 00:00:04, 1

31/01/2014 00:00:09, 0

31/01/2014 00:00:13, 1

The state will always form 1 to 0 and then from 0 to 1.

I want to plot a horizontal line at level 1 (y-axis), from time 00:04 to 00:09.

And plot a horizontal line at level 0 from time 00:09 to 00:13.

A vertical line shall be at the time of transaction.

Is there any way to plot this?

Thanks.

Upvotes: 2

Views: 1559

Answers (2)

user20650
user20650

Reputation: 25914

EDIT: changed geom_segment to geom_step

# data
a <- as.POSIXct(Sys.time())
mydf <- data.frame(time=seq(a,a+50,by=10),state=0:1)

library(ggplot2)

ggplot(mydf) +   geom_step( aes(x=time , y=state ))

enter image description here

Upvotes: 2

codeola
codeola

Reputation: 838

plot(yourData$time, yourData$state, type="s") plots the stairs you want. Capital type="S" does it towards the other side.

Upvotes: 1

Related Questions