Binary time series R

I have a dataset where I have a binary value on a timeline. For example:

Date, Event

January-29-2014, 1 
January-29-2014, 0 
January-29-2014, 1 
January-29-2014, 1 
January-30-2014, 0 

I would like to build a graph on the timeline (by date) and in color (red bar = 1, blue bar = 0) How can I achieve this? What do you call it? e.g. binary timeline :)

Sorry and thanks for your help.

+4
source share
2 answers

You think this is what you want. Using fake data:

n = 100
x = seq(n)
y = sample(0:1, n, replace=TRUE)

DF = data.frame(Date=x, Event=y)

ones = rep(1, nrow(DF))

colors = c("blue", "red")
plot(DF$Date, ones, type="h", col=colors[DF$Event +1],
     ylim=c(0,1))

theplot

+2
source

Not sure what you want. You want to combine data by day. How is the epidemiological curve?

library(lubridate)
library(plyr)
library(ggplot2)

dat = read.table(header=TRUE, text='Date Event

January-29-2014, 1 
January-29-2014, 0 
January-29-2014, 1 
January-29-2014, 1 
January-30-2014, 0 ')

dat$Date = mdy(dat$Date)

agg = ddply(dat, 'Date', summarise, Events = sum(Event))

ggplot(data=agg, aes(x = Date, y = Events)) + geom_bar(stat='identity') + scale_x_datetime(expand=c(1,0))

Output: enter image description here

0
source

Source: https://habr.com/ru/post/1524478/


All Articles