[R] cumulative frequency plots for factors

Jim Lemon jim at bitwrit.com.au
Tue Oct 16 13:50:00 CEST 2007


Dieter Vanderelst wrote:
> Dear list,
> 
> I have a data frame with a number of events (factor) and the times at which they occurred (continuous variable):
> 
> event time
> A 10
> A 12
> B 15
> A 17
> C 13
> ...
> 
> Is it possible in R to make a plot against time of the cumulative frequency of occurrence of each event? This would be, a raising line for each factor.
> 
Hi Dieter,
Henrique's solution is very elegant, but I thought you might find this 
one useful as well.

event_codes<-LETTERS[1:5]
sploogle<-data.frame(event=sample(event_codes,50,TRUE),
  time=sample(10:100,50))
sploogle<-sploogle[order(sploogle$time),]
sploogle$event_count<-rep(NA,50)
for(event_code in event_codes)
  sploogle$event_count[sploogle$event==event_code]<-
   1:sum(sploogle$event==event_code)
plot(sploogle$time[sploogle$event==event_codes[1]],
  sploogle$event_count[sploogle$event==event_codes[1]],
  main="Cumulative event count by time",xlim=c(1,max(sploogle$time)),
  ylim=c(1,max(sploogle$event_count)),type="b")
for(i in 2:5)
  points(sploogle$time[sploogle$event==event_codes[i]],
   sploogle$event_count[sploogle$event==event_codes[i]],
   type="b",col=i,pch=i)
legend(1,max(sploogle$event_count),event_codes,pch=1:5,col=1:5,lty=1)

Jim



More information about the R-help mailing list