R图二进制时间序列

R图二进制时间序列,r,plot,R,Plot,我有一个数据集,其中在时间轴上有一个二进制值。 例如: Date, Event January-29-2014, 1 January-29-2014, 0 January-29-2014, 1 January-29-2014, 1 January-30-2014, 0 我想将1,0绘制在时间轴上(按日期),并用颜色(红色条=1,蓝色条=0) 我怎样才能做到这一点? 这叫什么?e、 g.二进制时间轴绘图:) 对不起,谢谢你的帮助。我想这就是你想要的。使用假数据: n = 100 x

我有一个数据集,其中在时间轴上有一个二进制值。 例如:

Date, Event

January-29-2014, 1 
January-29-2014, 0 
January-29-2014, 1 
January-29-2014, 1 
January-30-2014, 0 
我想将1,0绘制在时间轴上(按日期),并用颜色(红色条=1,蓝色条=0) 我怎样才能做到这一点? 这叫什么?e、 g.二进制时间轴绘图:)


对不起,谢谢你的帮助。

我想这就是你想要的。使用假数据:

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))

不太确定你想要什么。是否希望按天聚合数据。像流行病学曲线

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))
输出:

是的,这正是我想要的。只有我发现我的数据似乎只有一个标题。“2014-01-31 0”是一个条目。现在我试着把它分成两列,这样我就可以按照你的建议做了。谢谢。