简体   繁体   English

R绘制二进制时间序列

[英]R plot binary timeseries

I have a dataset where I have a binary value on a timeline. 我有一个数据集,我在时间轴上有一个二进制值。 So 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 plot the 1,0 on a timeline(by Date) and in color (red bar = 1,blue bar = 0) How can I achieve this ? 我想在时间轴(按日期)和颜色(红色条形= 1,蓝色条形= 0)上绘制1,0如何实现此目的? How do you call this ? 你怎么称呼这个? eg binary timeline plotting :) 例如二进制时间线绘图:)

Sorry and thanks for your help. 对不起,谢谢你的帮助。

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

剧情

Not really sure what you want. 不太确定你想要什么。 Do you want aggregate data by day. 您希望白天汇总数据吗? Like an 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: 输出: 在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM