从R中不规则区间的分类数据绘制频率

时间:2014-02-05 19:00:27

标签: r time-series

我希望有人可以帮我解决R中的以下问题,这似乎应该有一个直截了当的答案:

#create data frame
df<- data.frame(x = c(1,10,10,40,50,75,100,500,550,600), y = c("black", "black", "red", "black", "blue", "red", "red", "black", "black", "blue"))
 df
 x    y
 1  black
10  black
10  red
40  black
50  blue
75  red
100 red
500 black
550 black
600 blue
 #Where x = days from time 0, y = event

如何以固定间隔(例如每30天)绘制每个变量频率的时间序列,并用连接线绘制每个点(即不是hist())?例如。 for(x,y)将为plot (0-30, black) = 2, (0-60, black) = 1, (60-90, black) = 0,依此类推每个y变量。

我使用tally.events()(epicalc)使用不同的数据集取得了成功,它可以完美地用于我想要的时间数据,但不适用于不是'date'的值。其他数据集如下:

 positive.df 
   resultz    datefix
13       P 2009-02-20
25       P 2009-03-23
45       P 2009-04-27
75       P 2009-07-22
82       P 2009-08-06
94       P 2009-08-19
tally.events(positive.df$datefix, breaks = c("month"), graph = TRUE, line.col ="auto", legend = TRUE, legend.site = "topright")

1 个答案:

答案 0 :(得分:0)

试试这个:

sq <- seq(0, 600, 30)
x_cut <- cut(df$x, sq)
xt <- xtabs(~ x_cut + y, df)
matplot(xt, col = levels(df$y), type = "o", pch = c(1, 3, 5), xaxt = "n")
axis(1,  seq_along(sq[-1]), sq[-1])

给出:

enter image description here