[英]R plot binary timeseries
我有一個數據集,我在時間軸上有一個二進制值。 例如:
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如何實現此目的? 你怎么稱呼這個? 例如二進制時間線繪圖:)
對不起,謝謝你的幫助。
認為這是你想要的。 使用假數據:
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))
輸出:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.