簡體   English   中英

R繪制二進制時間序列

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM